Add a Creator property on Content
This commit is contained in:
44
src/Web/Features/Contents/Handlers/GetContentsByCreator.cs
Normal file
44
src/Web/Features/Contents/Handlers/GetContentsByCreator.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user