Moved features to a specific folder

This commit is contained in:
Jonathan Bourdon
2024-07-18 22:22:44 -04:00
parent 98c85265f1
commit b34a775a4b
23 changed files with 32 additions and 37 deletions

View File

@@ -0,0 +1,38 @@
using FastEndpoints;
using Hutopy.Web.Features.Contents.Data;
using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Features.Contents.Handlers;
public sealed class GetContentsRequest
{
public Guid ContentId { get; set; }
}
public class GetContents(
ContentDbContext context)
: Endpoint<GetContentsRequest, Content>
{
public override void Configure()
{
Get("/api/contents/{ContentId:guid}");
Options(o => o.WithTags("Contents"));
AllowAnonymous();
}
public override async Task HandleAsync(
GetContentsRequest req,
CancellationToken ct)
{
var content = await context
.Contents
.FirstOrDefaultAsync(
c => c.Id == req.ContentId,
cancellationToken: ct);
if (content is null)
await SendNotFoundAsync(cancellation: ct);
else
await SendAsync(content, cancellation: ct);
}
}

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 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);
}
}

View File

@@ -0,0 +1,39 @@
using FastEndpoints;
using Hutopy.Web.Common;
using Hutopy.Web.Features.Contents.Data;
namespace Hutopy.Web.Features.Contents.Handlers;
public record struct PostContentRequest(
string? Title,
string? Description,
string? Uri);
public class PostContent(
ContentDbContext context)
: Endpoint<PostContentRequest>
{
public override void Configure()
{
Post("/api/contents");
Options( o => o.WithTags("Contents"));
}
public override async Task HandleAsync(
PostContentRequest req,
CancellationToken ct)
{
await context.Contents.AddAsync(
new Content
{
Id = GuidHelper.GenerateUuidV7(),
CreatedBy = User.GetUserId(),
Title = req.Title,
Description = req.Description,
Uri = req.Uri
},
ct);
await context.SaveChangesAsync(ct);
}
}