diff --git a/ProjectLighthouse/Controllers/PhotosController.cs b/ProjectLighthouse/Controllers/PhotosController.cs index 0ea9d167..4e9ffceb 100644 --- a/ProjectLighthouse/Controllers/PhotosController.cs +++ b/ProjectLighthouse/Controllers/PhotosController.cs @@ -72,14 +72,13 @@ namespace LBPUnion.ProjectLighthouse.Controllers } [HttpGet("photos/by")] - public async Task UserPhotos([FromQuery] string user, [FromQuery] int pageStart, [FromQuery] int pageSize) + public async Task UserPhotosBy([FromQuery] string user, [FromQuery] int pageStart, [FromQuery] int pageSize) { User? userFromQuery = await this.database.Users.FirstOrDefaultAsync(u => u.Username == user); // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (user == null) return this.NotFound(); - List photos = await this.database.Photos.Where - (p => p.CreatorId == userFromQuery.UserId) + List photos = await this.database.Photos.Where(p => p.CreatorId == userFromQuery.UserId) .OrderByDescending(s => s.Timestamp) .Skip(pageStart - 1) .Take(Math.Min(pageSize, 30)) @@ -87,5 +86,27 @@ namespace LBPUnion.ProjectLighthouse.Controllers string response = photos.Aggregate(string.Empty, (s, photo) => s + photo.Serialize(0)); return this.Ok(LbpSerializer.StringElement("photos", response)); } + + [HttpGet("photos/with")] + public async Task UserPhotosWith([FromQuery] string user, [FromQuery] int pageStart, [FromQuery] int pageSize) + { + User? userFromQuery = await this.database.Users.FirstOrDefaultAsync(u => u.Username == user); + // ReSharper disable once ConditionIsAlwaysTrueOrFalse + if (user == null) return this.NotFound(); + + List photos = new(); + foreach (Photo photo in this.database.Photos) + { + photos.AddRange(photo.Subjects.Where(subject => subject.User.UserId == userFromQuery.UserId).Select(_ => photo)); + } + + string response = photos.OrderByDescending + (s => s.Timestamp) + .Skip(pageStart - 1) + .Take(Math.Min(pageSize, 30)) + .Aggregate(string.Empty, (s, photo) => s + photo.Serialize(0)); + + return this.Ok(LbpSerializer.StringElement("photos", response)); + } } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Photo.cs b/ProjectLighthouse/Types/Photo.cs index e6c0f576..d2c6d453 100644 --- a/ProjectLighthouse/Types/Photo.cs +++ b/ProjectLighthouse/Types/Photo.cs @@ -33,20 +33,35 @@ namespace LBPUnion.ProjectLighthouse.Types [XmlElement("plan")] public string PlanHash { get; set; } -// [XmlIgnore] -// public int SlotId { get; set; } -// -// [XmlIgnore] -// [ForeignKey(nameof(SlotId))] -// public Slot Slot { get; set; } + private List? subjects; - /// - /// Only use when parsing from XML. - /// [NotMapped] [XmlArray("subjects")] [XmlArrayItem("subject")] - public List Subjects { get; set; } + public List Subjects { + get { + if (this.subjects != null) return this.subjects; + + List response = new(); + + using Database database = new(); + + foreach (string idStr in this.PhotoSubjectIds) + { + if (string.IsNullOrEmpty(idStr)) continue; + + if (!int.TryParse(idStr, out int id)) throw new InvalidCastException(idStr + " is not a valid number."); + + PhotoSubject? photoSubject = database.PhotoSubjects.Include(p => p.User).FirstOrDefault(p => p.PhotoSubjectId == id); + if (photoSubject == null) continue; + + response.Add(photoSubject); + } + + return response; + } + set => this.subjects = value; + } [NotMapped] [XmlIgnore] @@ -62,32 +77,11 @@ namespace LBPUnion.ProjectLighthouse.Types [ForeignKey(nameof(CreatorId))] public User Creator { get; set; } - public List GetSubjects() - { - List response = new(); - - using Database database = new(); - - foreach (string idStr in this.PhotoSubjectIds) - { - if (string.IsNullOrEmpty(idStr)) continue; - - if (!int.TryParse(idStr, out int id)) throw new InvalidCastException(idStr + " is not a valid number."); - - PhotoSubject? photoSubject = database.PhotoSubjects.Include(p => p.User).FirstOrDefault(p => p.PhotoSubjectId == id); - if (photoSubject == null) continue; - - response.Add(photoSubject); - } - - return response; - } - public string Serialize(int slotId) { string slot = LbpSerializer.TaggedStringElement("slot", LbpSerializer.StringElement("id", slotId), "type", "user"); - string subjects = this.GetSubjects().Aggregate(string.Empty, (s, subject) => s + subject.Serialize()); + string subjectsAggregate = this.Subjects.Aggregate(string.Empty, (s, subject) => s + subject.Serialize()); string photo = LbpSerializer.StringElement("id", this.PhotoId) + LbpSerializer.StringElement("small", this.SmallHash) + @@ -95,7 +89,7 @@ namespace LBPUnion.ProjectLighthouse.Types LbpSerializer.StringElement("large", this.LargeHash) + LbpSerializer.StringElement("plan", this.PlanHash) + LbpSerializer.StringElement("author", this.CreatorId) + - LbpSerializer.StringElement("subjects", subjects) + + LbpSerializer.StringElement("subjects", subjectsAggregate) + slot; return LbpSerializer.TaggedStringElement("photo", photo, "timestamp", Timestamp * 1000); diff --git a/ProjectLighthouse/Types/User.cs b/ProjectLighthouse/Types/User.cs index 17737855..c148f7d4 100644 --- a/ProjectLighthouse/Types/User.cs +++ b/ProjectLighthouse/Types/User.cs @@ -37,7 +37,12 @@ namespace LBPUnion.ProjectLighthouse.Types } [NotMapped] - public int PhotosWithMeCount { get; set; } + public int PhotosWithMeCount { + get { + using Database database = new(); + return Enumerable.Sum(database.Photos, photo => photo.Subjects.Count(subject => subject.User.UserId == this.UserId)); + } + } public bool CommentsEnabled { get; set; }