Moved features to a specific folder
This commit is contained in:
44
src/Web/Features/Contents/Handlers/GetContentsByUser.cs
Normal file
44
src/Web/Features/Contents/Handlers/GetContentsByUser.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 GetContentsByUserRequest
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
[BindFrom("max_items")] public int MaxItems { 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);
|
||||
|
||||
if (req.LastId is not null)
|
||||
query = query.OrderByDescending(c => c.Id).Where(c => c.Id < req.LastId.Value);
|
||||
else
|
||||
query = query.OrderByDescending(c => c.Id);
|
||||
|
||||
query = query.Take(req.MaxItems);
|
||||
|
||||
var posts = await query.ToListAsync(cancellationToken: ct);
|
||||
|
||||
await SendAsync(posts, cancellation: ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user