mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-29 16:38:37 +00:00
Implement photos with user
This commit is contained in:
parent
c415691d72
commit
632e430b36
3 changed files with 57 additions and 37 deletions
|
@ -72,14 +72,13 @@ namespace LBPUnion.ProjectLighthouse.Controllers
|
|||
}
|
||||
|
||||
[HttpGet("photos/by")]
|
||||
public async Task<IActionResult> UserPhotos([FromQuery] string user, [FromQuery] int pageStart, [FromQuery] int pageSize)
|
||||
public async Task<IActionResult> 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<Photo> photos = await this.database.Photos.Where
|
||||
(p => p.CreatorId == userFromQuery.UserId)
|
||||
List<Photo> 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<IActionResult> 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<Photo> 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));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -33,37 +33,15 @@ 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<PhotoSubject>? subjects;
|
||||
|
||||
/// <summary>
|
||||
/// Only use when parsing from XML.
|
||||
/// </summary>
|
||||
[NotMapped]
|
||||
[XmlArray("subjects")]
|
||||
[XmlArrayItem("subject")]
|
||||
public List<PhotoSubject> Subjects { get; set; }
|
||||
public List<PhotoSubject> Subjects {
|
||||
get {
|
||||
if (this.subjects != null) return this.subjects;
|
||||
|
||||
[NotMapped]
|
||||
[XmlIgnore]
|
||||
public string[] PhotoSubjectIds {
|
||||
get => this.PhotoSubjectCollection.Split(",");
|
||||
set => this.PhotoSubjectCollection = string.Join(',', value);
|
||||
}
|
||||
|
||||
public string PhotoSubjectCollection { get; set; }
|
||||
|
||||
public int CreatorId { get; set; }
|
||||
|
||||
[ForeignKey(nameof(CreatorId))]
|
||||
public User Creator { get; set; }
|
||||
|
||||
public List<PhotoSubject> GetSubjects()
|
||||
{
|
||||
List<PhotoSubject> response = new();
|
||||
|
||||
using Database database = new();
|
||||
|
@ -82,12 +60,28 @@ namespace LBPUnion.ProjectLighthouse.Types
|
|||
|
||||
return response;
|
||||
}
|
||||
set => this.subjects = value;
|
||||
}
|
||||
|
||||
[NotMapped]
|
||||
[XmlIgnore]
|
||||
public string[] PhotoSubjectIds {
|
||||
get => this.PhotoSubjectCollection.Split(",");
|
||||
set => this.PhotoSubjectCollection = string.Join(',', value);
|
||||
}
|
||||
|
||||
public string PhotoSubjectCollection { get; set; }
|
||||
|
||||
public int CreatorId { get; set; }
|
||||
|
||||
[ForeignKey(nameof(CreatorId))]
|
||||
public User Creator { get; set; }
|
||||
|
||||
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);
|
||||
|
|
|
@ -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; }
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue