Add a Creator property on Content

This commit is contained in:
Jonathan Bourdon
2024-08-03 23:23:37 -04:00
parent 0340904847
commit e560dfccc1
4 changed files with 18 additions and 24 deletions

View File

@@ -0,0 +1,44 @@
using FastEndpoints;
using Hutopy.Web.Features.Contents.Data;
using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Features.Contents.Handlers;
public sealed class GetContentsByCreatorRequest
{
public Guid UserId { get; set; }
[BindFrom("page_size")] public int PageSize { get; set; } = 10;
[BindFrom("last_id")] public Guid? LastId { get; set; }
}
public class GetContentsByCreatorHandler(
ContentDbContext context)
: Endpoint<GetContentsByCreatorRequest, List<Content>>
{
public override void Configure()
{
Get("/api/contents/creator/{UserId:guid}");
Options(o => o.WithTags("Contents"));
AllowAnonymous();
}
public override async Task HandleAsync(
GetContentsByCreatorRequest req,
CancellationToken ct)
{
var query = context
.Contents
.Where(c => c.CreatedBy == req.UserId);
query = query.OrderByDescending(c => c.CreatedAt);
if (req.LastId is not null)
query = query.Where(c => c.Id < req.LastId.Value);
query = query.Take(req.PageSize);
var posts = await query.ToListAsync(cancellationToken: ct);
await SendAsync(posts, cancellation: ct);
}
}