using Hutopy.Web.Features.Contents.Data; using Hutopy.Web.Features.Contents.Handlers.Models; namespace Hutopy.Web.Features.Contents.Handlers; [PublicAPI] public sealed class GetCreatorByAliasRequest { public required string Name { get; set; } } [UsedImplicitly] public sealed class GetCreatorByAliasRequestValidator : Validator { public GetCreatorByAliasRequestValidator() { RuleFor(r => r.Name) .NotNull().WithMessage("You should specify the Name") .NotEmpty().WithMessage("You should specify a valid/not empty Name"); } } [PublicAPI] public class GetCreatorByAliasHandler( ContentDbContext context) : Endpoint { public override void Configure() { Get("/api/creators/@{Name}"); Options((o => o.WithTags("Creators"))); AllowAnonymous(); } public override async Task HandleAsync( GetCreatorByAliasRequest req, CancellationToken ct) { var creatorName = req.Name.ToLower(); var creator = await context .Creators .Where(c => EF.Functions.ILike(c.Name, creatorName)) .FirstOrDefaultAsync(ct); if (creator is null) { await SendNotFoundAsync(ct); } else { var model = new CreatorModel( creator.Id, creator.CreatedBy, creator.CreatedAt, creator.Name, creator.Title, creator.Socials, creator.Colors, creator.Images); await SendAsync(model, cancellation: ct); } } }