Implement pages for profile photos

This commit is contained in:
jvyden 2021-11-07 15:33:13 -05:00
commit c415691d72
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278

View file

@ -1,4 +1,5 @@
#nullable enable #nullable enable
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -71,13 +72,18 @@ namespace LBPUnion.ProjectLighthouse.Controllers
} }
[HttpGet("photos/by")] [HttpGet("photos/by")]
public async Task<IActionResult> UserPhotos([FromQuery] string user) public async Task<IActionResult> UserPhotos([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(p => p.CreatorId == userFromQuery.UserId).Take(10).ToListAsync(); 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))
.ToListAsync();
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));
} }