65 lines
1.5 KiB
C#
65 lines
1.5 KiB
C#
using Hutopy.Infrastructure.Security;
|
|
using Hutopy.Modules.Creators.Data;
|
|
|
|
namespace Hutopy.Modules.Creators.Features;
|
|
|
|
[PublicAPI]
|
|
public record RestoreCreatorRequest(
|
|
string CreatorSlug);
|
|
|
|
[UsedImplicitly]
|
|
public sealed class RestoreCreatorRequestValidator : Validator<RestoreCreatorRequest>
|
|
{
|
|
public RestoreCreatorRequestValidator()
|
|
{
|
|
RuleFor(r => r.CreatorSlug)
|
|
.NotNull()
|
|
.NotEmpty()
|
|
.WithMessage("You should specify a valid CreatorSlug");
|
|
}
|
|
}
|
|
|
|
[PublicAPI]
|
|
public sealed class RestoreCreatorHandler(
|
|
CreatorsDbContext context)
|
|
: Endpoint<RestoreCreatorRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Put("/api/creators/@{CreatorSlug}/restore");
|
|
Options(o => o.WithTags("Creators"));
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
RestoreCreatorRequest req,
|
|
CancellationToken ct)
|
|
{
|
|
string creatorSlug = req.CreatorSlug.ToLower();
|
|
|
|
Creator? creator = await context
|
|
.Creators
|
|
.IgnoreQueryFilters()
|
|
.Where(c => EF.Functions.ILike(c.Slug, creatorSlug))
|
|
.SingleOrDefaultAsync(ct);
|
|
|
|
if (creator is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
if (creator.CreatedBy != User.GetUserId())
|
|
{
|
|
await SendUnauthorizedAsync(ct);
|
|
return;
|
|
}
|
|
|
|
creator.DeletedAt = null;
|
|
creator.DeletedBy = null;
|
|
|
|
await context.SaveChangesAsync(ct);
|
|
|
|
await SendOkAsync(ct);
|
|
}
|
|
}
|