82 lines
2.1 KiB
C#
82 lines
2.1 KiB
C#
using Hutopy.Web.Common.Security;
|
|
using Hutopy.Web.Features.Contents.Data;
|
|
|
|
namespace Hutopy.Web.Features.Contents.Handlers;
|
|
|
|
[PublicAPI]
|
|
public record CreateCreatorRequest(
|
|
Guid SlugReservationId,
|
|
Guid CreatorId);
|
|
|
|
[UsedImplicitly]
|
|
public sealed class CreateCreatorRequestValidator : Validator<CreateCreatorRequest>
|
|
{
|
|
public CreateCreatorRequestValidator()
|
|
{
|
|
RuleFor(r => r.SlugReservationId)
|
|
.NotNull()
|
|
.NotEmpty()
|
|
.WithMessage("You should specify a valid Name");
|
|
|
|
RuleFor(r => r.CreatorId)
|
|
.NotNull()
|
|
.NotEmpty()
|
|
.WithMessage("You should specify a valid CreatorId");
|
|
}
|
|
}
|
|
|
|
[PublicAPI]
|
|
public sealed class CreateCreatorHandler(
|
|
ContentDbContext context)
|
|
: Endpoint<CreateCreatorRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/api/creators");
|
|
Options(o => o.WithTags("Creators"));
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
CreateCreatorRequest req,
|
|
CancellationToken ct)
|
|
{
|
|
await using var transaction = await context.Database.BeginTransactionAsync(ct);
|
|
|
|
try
|
|
{
|
|
var slug = await context
|
|
.Slugs
|
|
.SingleAsync(s => s.Id == req.SlugReservationId, ct);
|
|
|
|
if (slug.Active == false
|
|
&& slug.ReservedUntil >= DateTime.Now
|
|
&& slug.CreatedBy == User.GetUserId())
|
|
{
|
|
await SendErrorsAsync(500, ct);
|
|
return;
|
|
}
|
|
|
|
slug.Active = true;
|
|
|
|
await context.Creators.AddAsync(
|
|
new Creator
|
|
{
|
|
Id = req.CreatorId,
|
|
CreatedBy = User.GetUserId(),
|
|
Slugs = slug
|
|
},
|
|
ct);
|
|
|
|
await context.SaveChangesAsync(ct);
|
|
|
|
await transaction.CommitAsync(ct);
|
|
|
|
await SendOkAsync(ct);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
await transaction.RollbackAsync(ct);
|
|
}
|
|
}
|
|
}
|