using Hutopy.Web.Common; using Hutopy.Web.Features.Contents.Data; namespace Hutopy.Web.Features.Contents.Handlers; [PublicAPI] public record CreateCreatorRequest( Guid CreatorId, string Name); [UsedImplicitly] public sealed class CreateCreatorRequestValidator : Validator { public CreateCreatorRequestValidator() { RuleFor(r => r.CreatorId) .NotNull().WithMessage("You should specify the CreatorId") .NotEmpty().WithMessage("You should specify a valid/not empty CreatorId"); RuleFor(r => r.Name) .NotNull().WithMessage("You should specify the Name") .NotEmpty().WithMessage("You should specify a valid/not empty Name"); } } [PublicAPI] public sealed class CreateCreatorHandler( ContentDbContext context) : Endpoint { public override void Configure() { Post("/api/creators"); Options(o => o.WithTags("Creators")); } public override async Task HandleAsync( CreateCreatorRequest req, CancellationToken ct) { await context.Creators.AddAsync( new() { Id = req.CreatorId, CreatedBy = User.GetUserId(), Name = req.Name }, ct); await context.SaveChangesAsync(ct); await SendOkAsync(ct); } }