Implement pages for slots/by endpoint

This commit is contained in:
jvyden 2021-11-14 18:53:53 -05:00
commit b641a6be81
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278

View file

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Levels; using LBPUnion.ProjectLighthouse.Types.Levels;
using LBPUnion.ProjectLighthouse.Types.Settings;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -23,24 +24,44 @@ namespace LBPUnion.ProjectLighthouse.Controllers
} }
[HttpGet("slots/by")] [HttpGet("slots/by")]
public async Task<IActionResult> SlotsBy([FromQuery] string u) public async Task<IActionResult> SlotsBy([FromQuery] string u, [FromQuery] int pageStart, [FromQuery] int pageSize)
{ {
Token? token = await this.database.TokenFromRequest(this.Request); Token? token = await this.database.TokenFromRequest(this.Request);
if (token == null) return this.BadRequest(); if (token == null) return this.BadRequest();
GameVersion gameVersion = token.GameVersion; GameVersion gameVersion = token.GameVersion;
User user = await this.database.Users.FirstOrDefaultAsync(dbUser => dbUser.Username == u);
string response = Enumerable.Aggregate string response = Enumerable.Aggregate
( (
this.database.Slots.Where(s => s.GameVersion <= gameVersion) this.database.Slots.Where(s => s.GameVersion <= gameVersion)
.Include(s => s.Creator) .Include(s => s.Creator)
.Include(s => s.Location) .Include(s => s.Location)
.Where(s => s.Creator.Username == u), .Where(s => s.Creator.Username == user.Username)
.Skip(pageStart - 1)
.Take(Math.Min(pageSize, ServerSettings.EntitledSlots)),
string.Empty, string.Empty,
(current, slot) => current + slot.Serialize() (current, slot) => current + slot.Serialize()
); );
return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", 1)); return this.Ok
(
LbpSerializer.TaggedStringElement
(
"slots",
response,
new Dictionary<string, object>
{
{
"hint_start", pageStart + Math.Min(pageSize, ServerSettings.EntitledSlots)
},
{
"total", user.UsedSlots
},
}
)
);
} }
[HttpGet("s/user/{id:int}")] [HttpGet("s/user/{id:int}")]