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")]
|
[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);
|
User? userFromQuery = await this.database.Users.FirstOrDefaultAsync(u => u.Username == user);
|
||||||
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
|
||||||
if (user == null) return this.NotFound();
|
if (user == null) return this.NotFound();
|
||||||
|
|
||||||
List<Photo> photos = await this.database.Photos.Where
|
List<Photo> photos = await this.database.Photos.Where(p => p.CreatorId == userFromQuery.UserId)
|
||||||
(p => p.CreatorId == userFromQuery.UserId)
|
|
||||||
.OrderByDescending(s => s.Timestamp)
|
.OrderByDescending(s => s.Timestamp)
|
||||||
.Skip(pageStart - 1)
|
.Skip(pageStart - 1)
|
||||||
.Take(Math.Min(pageSize, 30))
|
.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));
|
string response = photos.Aggregate(string.Empty, (s, photo) => s + photo.Serialize(0));
|
||||||
return this.Ok(LbpSerializer.StringElement("photos", response));
|
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,20 +33,35 @@ namespace LBPUnion.ProjectLighthouse.Types
|
||||||
[XmlElement("plan")]
|
[XmlElement("plan")]
|
||||||
public string PlanHash { get; set; }
|
public string PlanHash { get; set; }
|
||||||
|
|
||||||
// [XmlIgnore]
|
private List<PhotoSubject>? subjects;
|
||||||
// public int SlotId { get; set; }
|
|
||||||
//
|
|
||||||
// [XmlIgnore]
|
|
||||||
// [ForeignKey(nameof(SlotId))]
|
|
||||||
// public Slot Slot { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Only use when parsing from XML.
|
|
||||||
/// </summary>
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[XmlArray("subjects")]
|
[XmlArray("subjects")]
|
||||||
[XmlArrayItem("subject")]
|
[XmlArrayItem("subject")]
|
||||||
public List<PhotoSubject> Subjects { get; set; }
|
public List<PhotoSubject> Subjects {
|
||||||
|
get {
|
||||||
|
if (this.subjects != null) return this.subjects;
|
||||||
|
|
||||||
|
List<PhotoSubject> 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]
|
[NotMapped]
|
||||||
[XmlIgnore]
|
[XmlIgnore]
|
||||||
|
@ -62,32 +77,11 @@ namespace LBPUnion.ProjectLighthouse.Types
|
||||||
[ForeignKey(nameof(CreatorId))]
|
[ForeignKey(nameof(CreatorId))]
|
||||||
public User Creator { get; set; }
|
public User Creator { get; set; }
|
||||||
|
|
||||||
public List<PhotoSubject> GetSubjects()
|
|
||||||
{
|
|
||||||
List<PhotoSubject> 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)
|
public string Serialize(int slotId)
|
||||||
{
|
{
|
||||||
string slot = LbpSerializer.TaggedStringElement("slot", LbpSerializer.StringElement("id", slotId), "type", "user");
|
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) +
|
string photo = LbpSerializer.StringElement("id", this.PhotoId) +
|
||||||
LbpSerializer.StringElement("small", this.SmallHash) +
|
LbpSerializer.StringElement("small", this.SmallHash) +
|
||||||
|
@ -95,7 +89,7 @@ namespace LBPUnion.ProjectLighthouse.Types
|
||||||
LbpSerializer.StringElement("large", this.LargeHash) +
|
LbpSerializer.StringElement("large", this.LargeHash) +
|
||||||
LbpSerializer.StringElement("plan", this.PlanHash) +
|
LbpSerializer.StringElement("plan", this.PlanHash) +
|
||||||
LbpSerializer.StringElement("author", this.CreatorId) +
|
LbpSerializer.StringElement("author", this.CreatorId) +
|
||||||
LbpSerializer.StringElement("subjects", subjects) +
|
LbpSerializer.StringElement("subjects", subjectsAggregate) +
|
||||||
slot;
|
slot;
|
||||||
|
|
||||||
return LbpSerializer.TaggedStringElement("photo", photo, "timestamp", Timestamp * 1000);
|
return LbpSerializer.TaggedStringElement("photo", photo, "timestamp", Timestamp * 1000);
|
||||||
|
|
|
@ -37,7 +37,12 @@ namespace LBPUnion.ProjectLighthouse.Types
|
||||||
}
|
}
|
||||||
|
|
||||||
[NotMapped]
|
[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; }
|
public bool CommentsEnabled { get; set; }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue