45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
using FastEndpoints;
|
|
using Hutopy.Web.Features.Contents.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Hutopy.Web.Features.Contents.Handlers;
|
|
|
|
public sealed class GetContentsByUserRequest
|
|
{
|
|
public Guid UserId { get; set; }
|
|
[BindFrom("page_size")] public int PageSize { get; set; } = 10;
|
|
[BindFrom("last_id")] public Guid? LastId { get; set; }
|
|
}
|
|
|
|
public class GetContentsByUser(
|
|
ContentDbContext context)
|
|
: Endpoint<GetContentsByUserRequest, List<Content>>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/contents/user/{UserId:guid}");
|
|
Options(o => o.WithTags("Contents"));
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
GetContentsByUserRequest 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);
|
|
}
|
|
}
|