using System.Linq.Expressions; using FastEndpoints; using Hutopy.Web.Features.Contents.Data; using Hutopy.Web.Features.Contents.Handlers.Models; 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> { 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 .Select(x => x.ToResponse()) .ToListAsync(cancellationToken: ct); await SendAsync(posts, cancellation: ct); } }