This commit is contained in:
2025-02-07 15:44:59 -05:00
parent 2b30479263
commit 009368ca8f
38 changed files with 1815 additions and 945 deletions

View File

@@ -1,28 +1,27 @@
using System.Net;
using FluentValidation.Results;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Features.Contents.Data;
using Npgsql;
namespace Hutopy.Web.Features.Contents.Handlers;
[PublicAPI]
public record CreateCreatorRequest(
Guid CreatorId,
string Name);
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().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");
.NotNull()
.NotEmpty()
.WithMessage("You should specify a valid CreatorId");
}
}
@@ -41,14 +40,30 @@ public sealed class CreateCreatorHandler(
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(),
Name = req.Name,
Slugs = slug,
Colors =
{
Primary = "#A30E79",
@@ -67,25 +82,13 @@ public sealed class CreateCreatorHandler(
await context.SaveChangesAsync(ct);
await transaction.CommitAsync(ct);
await SendOkAsync(ct);
}
catch (Exception e)
{
if (e.InnerException is PostgresException innerException)
{
if (innerException.ConstraintName == "IX_Creators_NormalizedName")
{
await SendResultAsync(new ProblemDetails(
[new ValidationFailure(nameof(Creator.Name), "The name is already taken.")],
(int)HttpStatusCode.Conflict));
}
}
else
{
await SendResultAsync(new ProblemDetails(
[new ValidationFailure(nameof(Creator.Name), e.Message)],
(int)HttpStatusCode.Conflict));
}
await transaction.RollbackAsync(ct);
}
}
}