Adds Content deletion

This commit is contained in:
2024-08-16 18:14:48 -04:00
parent d1557f92c6
commit 8b702d16d6
8 changed files with 382 additions and 1 deletions

View File

@@ -0,0 +1,59 @@
using Hutopy.Web.Common;
using Hutopy.Web.Features.Contents.Data;
namespace Hutopy.Web.Features.Contents.Handlers;
[PublicAPI]
public record DeleteContentRequest(
Guid ContentId);
[PublicAPI]
public sealed class DeleteContentRequestValidator : Validator<DeleteContentRequest>
{
public DeleteContentRequestValidator()
{
RuleFor(r => r.ContentId)
.NotNull().WithMessage("You should specify the ContentId")
.NotEmpty().WithMessage("You should specify a valid/not empty ContentId");
}
}
public sealed class DeleteContent(
ContentDbContext context)
: Endpoint<DeleteContentRequest>
{
public override void Configure()
{
Delete("/api/contents/{ContentId}");
Options(o => o.WithTags("Contents"));
}
public override async Task HandleAsync(
DeleteContentRequest req,
CancellationToken ct)
{
var content = await context.Contents.FindAsync(
[req.ContentId],
ct);
if (content is null)
{
await SendNotFoundAsync(ct);
return;
}
var userId = HttpContext.User.GetUserId();
if (content.CreatedBy != userId)
{
await SendForbiddenAsync(ct);
return;
}
content.DeletedAt = DateTimeOffset.UtcNow;
content.DeletedBy = userId;
await context.SaveChangesAsync(ct);
await SendOkAsync(ct);
}
}

View File

@@ -36,6 +36,8 @@ public class GetContent(
CreatedAt = c.CreatedAt,
ColorMenu = c.Creator.Colors.Menu,
ColorAccent = c.Creator.Colors.Accent,
DeletedBy = c.DeletedBy,
DeletedAt = c.DeletedAt,
Title = c.Title,
Description = c.Description,
Urls = c.Urls,

View File

@@ -37,6 +37,8 @@ public class GetContentsByCreatorHandler(
c."Menu" as ColorMenu,
c."Accent" as ColorAccent,
content."CreatedAt",
content."DeletedBy",
content."DeletedAt",
content."Title",
content."Description",
content."Urls"
@@ -44,7 +46,8 @@ public class GetContentsByCreatorHandler(
INNER JOIN "Content"."Creators" AS creator ON content."CreatedBy" = creator."Id"
LEFT JOIN "Content"."Images" AS i ON creator."Id" = i."CreatorId"
LEFT JOIN "Content"."Colors" AS c ON creator."Id" = c."CreatorId"
WHERE content."CreatedBy" = '{req.CreatorId}'
WHERE content."CreatedBy" = '{req.CreatorId}'
AND content."DeletedBy" IS NULL
""");
if (req.LastId.HasValue)

View File

@@ -10,6 +10,8 @@ public class ContentModel
public required string? ColorMenu { get; init; }
public required string? ColorAccent { get; init; }
public required DateTimeOffset CreatedAt { get; init; }
public Guid? DeletedBy { get; init; }
public DateTimeOffset? DeletedAt { get; init; }
public required string Title { get; init; }
public required string Description { get; init; }
public required string[]? Urls { get; init; }