54 lines
1.4 KiB
C#
54 lines
1.4 KiB
C#
using FastEndpoints;
|
|
using FluentValidation;
|
|
using Hutopy.Web.Common;
|
|
using Hutopy.Web.Features.Contents.Data;
|
|
|
|
namespace Hutopy.Web.Features.Contents.Handlers;
|
|
|
|
public record CreateCreatorRequest(
|
|
Guid CreatorId,
|
|
string Name);
|
|
|
|
public sealed class CreateCreatorRequestValidator : Validator<CreateCreatorRequest>
|
|
{
|
|
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");
|
|
}
|
|
}
|
|
|
|
public sealed class CreateCreatorHandler(
|
|
ContentDbContext context)
|
|
: Endpoint<CreateCreatorRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/api/creators");
|
|
Options(o => o.WithTags("Contents"));
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|