Adds delete and restore for a creator's page

This commit is contained in:
2025-04-15 15:57:40 -04:00
parent 43f37177af
commit adfaaf3595
9 changed files with 187 additions and 39 deletions

View File

@@ -1,4 +1,5 @@
using Hutopy.Web.Features.Contents.Data;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Features.Contents.Data;
namespace Hutopy.Web.Features.Contents.Handlers;
@@ -13,6 +14,9 @@ public class GetCreatorBySlugResponse(
Guid id,
Guid createdBy,
DateTimeOffset createdAt,
Guid? deletedBy,
DateTimeOffset? deletedAt,
bool isDeleted,
bool verified,
bool acceptDonation,
string name,
@@ -25,6 +29,9 @@ public class GetCreatorBySlugResponse(
public Guid Id { get; } = id;
public Guid CreatedBy { get; } = createdBy;
public DateTimeOffset CreatedAt { get; } = createdAt;
public Guid? DeletedBy { get; } = deletedBy;
public DateTimeOffset? DeletedAt { get; } = deletedAt;
public bool IsDeleted { get; } = isDeleted;
public bool Verified { get; } = verified;
public bool AcceptDonation { get; } = acceptDonation;
public string Name { get; } = name;
@@ -67,6 +74,7 @@ public class GetCreatorBySlugHandler(
var creator = await context
.Creators
.IgnoreQueryFilters()
.Where(c => EF.Functions.ILike(c.Slug, creatorName))
.AsNoTracking()
.Select(c => new GetCreatorBySlugResponse
@@ -74,6 +82,9 @@ public class GetCreatorBySlugHandler(
c.Id,
c.CreatedBy,
c.CreatedAt,
c.DeletedBy,
c.DeletedAt,
c.IsDeleted,
c.Verified,
c.AcceptDonation,
c.Name,
@@ -85,6 +96,15 @@ public class GetCreatorBySlugHandler(
.SingleOrDefaultAsync(ct);
if (creator is null)
{
await SendNotFoundAsync(ct);
return;
}
bool isOwner = User.Identity?.IsAuthenticated == true
&& User.GetUserId() == creator.CreatedBy;
if (creator.IsDeleted && !isOwner)
{
await SendNotFoundAsync(ct);
}

View File

@@ -9,13 +9,17 @@ public sealed class GetCreatorProfileResponse
public Guid Id { get; set; }
public Guid CreatedBy { get; set; }
public DateTimeOffset CreatedAt { get; set; }
public string Title { get; set; }
public string Name { get; set; }
public Guid? DeletedBy { get; set; }
public DateTimeOffset? DeletedAt { get; set; }
public bool IsDeleted { get; set; }
public required string Name { get; set; }
public required string Slug { get; set; }
public string? Title { get; set; }
public bool Verified { get; set; }
public bool AcceptDonation { get; set; }
public Images Images { get; set; }
public PresentationInfos PresentationInfos { get; set; }
public Socials Socials { get; set; }
public required Images Images { get; set; }
public required PresentationInfos PresentationInfos { get; set; }
public required Socials Socials { get; set; }
}
[PublicAPI]
@@ -27,7 +31,6 @@ public class GetCreatorProfileHandler(
{
Get("/api/creators/profile");
Options((o => o.WithTags("Creators")));
AllowAnonymous();
}
public override async Task HandleAsync(
@@ -35,6 +38,7 @@ public class GetCreatorProfileHandler(
{
var creator = await context
.Creators
.IgnoreQueryFilters()
.Where(c => c.Id == HttpContext.User.GetUserId())
.AsNoTracking()
.Select(c => new GetCreatorProfileResponse
@@ -42,7 +46,11 @@ public class GetCreatorProfileHandler(
Id = c.Id,
CreatedBy = c.CreatedBy,
CreatedAt = c.CreatedAt,
DeletedBy = c.DeletedBy,
DeletedAt = c.DeletedAt,
IsDeleted = c.IsDeleted,
Name = c.Name,
Slug = c.Slug,
Title = c.Title,
Verified = c.Verified,
AcceptDonation = c.AcceptDonation,

View File

@@ -5,17 +5,17 @@ namespace Hutopy.Web.Features.Contents.Handlers;
[PublicAPI]
public record RemoveCreatorRequest(
Guid CreatorId);
string CreatorSlug);
[UsedImplicitly]
public sealed class RemoveCreatorRequestValidator : Validator<RemoveCreatorRequest>
{
public RemoveCreatorRequestValidator()
{
RuleFor(r => r.CreatorId)
RuleFor(r => r.CreatorSlug)
.NotNull()
.NotEmpty()
.WithMessage("You should specify a valid CreatorId");
.WithMessage("You should specify a valid CreatorSlug");
}
}
@@ -26,7 +26,7 @@ public sealed class RemoveCreatorHandler(
{
public override void Configure()
{
Delete("/api/creators");
Delete("/api/creators/@{CreatorSlug}");
Options(o => o.WithTags("Creators"));
}
@@ -34,33 +34,30 @@ public sealed class RemoveCreatorHandler(
RemoveCreatorRequest req,
CancellationToken ct)
{
await using var transaction = await context.Database.BeginTransactionAsync(ct);
try
var creatorSlug = req.CreatorSlug.ToLower();
var creator = await context
.Creators
.Where(c => EF.Functions.ILike(c.Slug, creatorSlug))
.SingleOrDefaultAsync(cancellationToken: ct);
if (creator is null)
{
var creator = await context
.Creators
.Where(c => c.Id == req.CreatorId)
.SingleOrDefaultAsync(cancellationToken: ct);
if (creator is null)
{
await SendNotFoundAsync(ct);
return;
}
creator.DeletedAt = DateTimeOffset.UtcNow;
creator.DeletedBy = User.GetUserId();
await context.SaveChangesAsync(ct);
await transaction.CommitAsync(ct);
await SendOkAsync(ct);
await SendNotFoundAsync(ct);
return;
}
catch (Exception e)
if (creator.CreatedBy != User.GetUserId())
{
await transaction.RollbackAsync(ct);
await SendUnauthorizedAsync(ct);
return;
}
creator.DeletedAt = DateTimeOffset.UtcNow;
creator.DeletedBy = User.GetUserId();
await context.SaveChangesAsync(ct);
await SendOkAsync(ct);
}
}

View File

@@ -0,0 +1,64 @@
using Hutopy.Web.Common.Security;
using Hutopy.Web.Features.Contents.Data;
namespace Hutopy.Web.Features.Contents.Handlers;
[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(
ContentDbContext 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)
{
var creatorSlug = req.CreatorSlug.ToLower();
var creator = await context
.Creators
.IgnoreQueryFilters()
.Where(c => EF.Functions.ILike(c.Slug, creatorSlug))
.SingleOrDefaultAsync(cancellationToken: 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);
}
}