49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using FastEndpoints;
|
|
using FluentValidation;
|
|
using Hutopy.Web.Features.Contents.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Hutopy.Web.Features.Contents.Handlers;
|
|
|
|
public sealed class GetCreatorByAliasRequest
|
|
{
|
|
public required string Name { get; set; }
|
|
}
|
|
|
|
public sealed class GetCreatorByAliasRequestValidator
|
|
: Validator<GetCreatorByAliasRequest>
|
|
{
|
|
public GetCreatorByAliasRequestValidator()
|
|
{
|
|
RuleFor(r => r.Name)
|
|
.NotNull().WithMessage("You should specify the Name")
|
|
.NotEmpty().WithMessage("You should specify a valid/not empty Name");
|
|
}
|
|
}
|
|
|
|
public class GetCreatorByAliasHandler(
|
|
ContentDbContext context)
|
|
: Endpoint<GetCreatorByAliasRequest, Creator>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/creators/@{Name}");
|
|
Options((o => o.WithTags("Creators")));
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
GetCreatorByAliasRequest req,
|
|
CancellationToken ct)
|
|
{
|
|
var creator = await context
|
|
.Creators
|
|
.SingleOrDefaultAsync(
|
|
c => EF.Functions.Like(c.Name, req.Name),
|
|
cancellationToken: ct);
|
|
|
|
if (creator is null) await SendNotFoundAsync(ct);
|
|
else await SendAsync(creator, cancellation: ct);
|
|
}
|
|
}
|