Adds Content deletion
This commit is contained in:
@@ -8,6 +8,8 @@ public class Content
|
|||||||
public Guid CreatedBy { get; init; }
|
public Guid CreatedBy { get; init; }
|
||||||
public Creator? Creator { get; set; }
|
public Creator? Creator { get; set; }
|
||||||
public DateTimeOffset CreatedAt { get; init; }
|
public DateTimeOffset CreatedAt { get; init; }
|
||||||
|
public Guid? DeletedBy { get; set; }
|
||||||
|
public DateTimeOffset? DeletedAt { get; set; }
|
||||||
|
|
||||||
[MaxLength(128)] public required string Title { get; set; }
|
[MaxLength(128)] public required string Title { get; set; }
|
||||||
[MaxLength(2048)] public required string Description { get; set; }
|
[MaxLength(2048)] public required string Description { get; set; }
|
||||||
|
|||||||
59
src/Web/Features/Contents/Handlers/DeleteContent.cs
Normal file
59
src/Web/Features/Contents/Handlers/DeleteContent.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,6 +36,8 @@ public class GetContent(
|
|||||||
CreatedAt = c.CreatedAt,
|
CreatedAt = c.CreatedAt,
|
||||||
ColorMenu = c.Creator.Colors.Menu,
|
ColorMenu = c.Creator.Colors.Menu,
|
||||||
ColorAccent = c.Creator.Colors.Accent,
|
ColorAccent = c.Creator.Colors.Accent,
|
||||||
|
DeletedBy = c.DeletedBy,
|
||||||
|
DeletedAt = c.DeletedAt,
|
||||||
Title = c.Title,
|
Title = c.Title,
|
||||||
Description = c.Description,
|
Description = c.Description,
|
||||||
Urls = c.Urls,
|
Urls = c.Urls,
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ public class GetContentsByCreatorHandler(
|
|||||||
c."Menu" as ColorMenu,
|
c."Menu" as ColorMenu,
|
||||||
c."Accent" as ColorAccent,
|
c."Accent" as ColorAccent,
|
||||||
content."CreatedAt",
|
content."CreatedAt",
|
||||||
|
content."DeletedBy",
|
||||||
|
content."DeletedAt",
|
||||||
content."Title",
|
content."Title",
|
||||||
content."Description",
|
content."Description",
|
||||||
content."Urls"
|
content."Urls"
|
||||||
@@ -45,6 +47,7 @@ public class GetContentsByCreatorHandler(
|
|||||||
LEFT JOIN "Content"."Images" AS i ON creator."Id" = i."CreatorId"
|
LEFT JOIN "Content"."Images" AS i ON creator."Id" = i."CreatorId"
|
||||||
LEFT JOIN "Content"."Colors" AS c ON creator."Id" = c."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)
|
if (req.LastId.HasValue)
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ public class ContentModel
|
|||||||
public required string? ColorMenu { get; init; }
|
public required string? ColorMenu { get; init; }
|
||||||
public required string? ColorAccent { get; init; }
|
public required string? ColorAccent { get; init; }
|
||||||
public required DateTimeOffset CreatedAt { 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 Title { get; init; }
|
||||||
public required string Description { get; init; }
|
public required string Description { get; init; }
|
||||||
public required string[]? Urls { get; init; }
|
public required string[]? Urls { get; init; }
|
||||||
|
|||||||
264
src/Web/Features/Contents/Migrations/20240816212531_AddsSoftDelete.Designer.cs
generated
Normal file
264
src/Web/Features/Contents/Migrations/20240816212531_AddsSoftDelete.Designer.cs
generated
Normal file
@@ -0,0 +1,264 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using Hutopy.Web.Features.Contents.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Hutopy.Web.Features.Contents.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(ContentDbContext))]
|
||||||
|
[Migration("20240816212531_AddsSoftDelete")]
|
||||||
|
partial class AddsSoftDelete
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasDefaultSchema("Content")
|
||||||
|
.HasAnnotation("ProductVersion", "8.0.4")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||||
|
|
||||||
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("Hutopy.Web.Features.Contents.Data.Content", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("CreatedAt")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||||
|
|
||||||
|
b.Property<Guid>("CreatedBy")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b.Property<Guid?>("DeletedBy")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("DeletedAt")
|
||||||
|
.HasColumnType("timestamp with time zone");
|
||||||
|
|
||||||
|
b.Property<string>("Description")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(2048)
|
||||||
|
.HasColumnType("character varying(2048)");
|
||||||
|
|
||||||
|
b.Property<string>("Title")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(128)
|
||||||
|
.HasColumnType("character varying(128)");
|
||||||
|
|
||||||
|
b.Property<string[]>("Urls")
|
||||||
|
.HasColumnType("text[]");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("CreatedBy");
|
||||||
|
|
||||||
|
b.ToTable("Contents", "Content");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Hutopy.Web.Features.Contents.Data.Creator", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("CreatedAt")
|
||||||
|
.HasColumnType("timestamp with time zone");
|
||||||
|
|
||||||
|
b.Property<Guid>("CreatedBy")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Creators", "Content");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Hutopy.Web.Features.Contents.Data.Subscription", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("CreatedBy")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b.Property<Guid>("CreatorId")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("CreatedAt")
|
||||||
|
.HasColumnType("timestamp with time zone");
|
||||||
|
|
||||||
|
b.HasKey("CreatedBy", "CreatorId");
|
||||||
|
|
||||||
|
b.HasIndex("CreatorId");
|
||||||
|
|
||||||
|
b.ToTable("Subscriptions", "Content");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Hutopy.Web.Features.Contents.Data.Content", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("Hutopy.Web.Features.Contents.Data.Creator", "Creator")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("CreatedBy")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Creator");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Hutopy.Web.Features.Contents.Data.Creator", b =>
|
||||||
|
{
|
||||||
|
b.OwnsOne("Hutopy.Web.Features.Contents.Data.About", "About", b1 =>
|
||||||
|
{
|
||||||
|
b1.Property<Guid>("CreatorId")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b1.Property<string>("Description")
|
||||||
|
.HasMaxLength(2048)
|
||||||
|
.HasColumnType("character varying(2048)");
|
||||||
|
|
||||||
|
b1.Property<string>("Title")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.HasKey("CreatorId");
|
||||||
|
|
||||||
|
b1.ToTable("Creators", "Content");
|
||||||
|
|
||||||
|
b1.WithOwner()
|
||||||
|
.HasForeignKey("CreatorId");
|
||||||
|
});
|
||||||
|
|
||||||
|
b.OwnsOne("Hutopy.Web.Features.Contents.Data.Colors", "Colors", b1 =>
|
||||||
|
{
|
||||||
|
b1.Property<Guid>("CreatorId")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b1.Property<string>("Accent")
|
||||||
|
.HasMaxLength(9)
|
||||||
|
.HasColumnType("character varying(9)");
|
||||||
|
|
||||||
|
b1.Property<string>("BannerBottom")
|
||||||
|
.HasMaxLength(9)
|
||||||
|
.HasColumnType("character varying(9)");
|
||||||
|
|
||||||
|
b1.Property<string>("BannerTop")
|
||||||
|
.HasMaxLength(9)
|
||||||
|
.HasColumnType("character varying(9)");
|
||||||
|
|
||||||
|
b1.Property<string>("Menu")
|
||||||
|
.HasMaxLength(9)
|
||||||
|
.HasColumnType("character varying(9)");
|
||||||
|
|
||||||
|
b1.HasKey("CreatorId");
|
||||||
|
|
||||||
|
b1.ToTable("Colors", "Content");
|
||||||
|
|
||||||
|
b1.WithOwner()
|
||||||
|
.HasForeignKey("CreatorId");
|
||||||
|
});
|
||||||
|
|
||||||
|
b.OwnsOne("Hutopy.Web.Features.Contents.Data.Images", "Images", b1 =>
|
||||||
|
{
|
||||||
|
b1.Property<Guid>("CreatorId")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b1.Property<string>("Banner")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.Property<string>("Logo")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.HasKey("CreatorId");
|
||||||
|
|
||||||
|
b1.ToTable("Images", "Content");
|
||||||
|
|
||||||
|
b1.WithOwner()
|
||||||
|
.HasForeignKey("CreatorId");
|
||||||
|
});
|
||||||
|
|
||||||
|
b.OwnsOne("Hutopy.Web.Features.Contents.Data.Socials", "Socials", b1 =>
|
||||||
|
{
|
||||||
|
b1.Property<Guid>("CreatorId")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b1.Property<string>("FacebookUrl")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.Property<string>("InstagramUrl")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.Property<string>("LinkedInUrl")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.Property<string>("RedditUrl")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.Property<string>("TikTokUrl")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.Property<string>("WebsiteUrl")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.Property<string>("XUrl")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.Property<string>("YoutubeUrl")
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.HasColumnType("character varying(255)");
|
||||||
|
|
||||||
|
b1.HasKey("CreatorId");
|
||||||
|
|
||||||
|
b1.ToTable("Socials", "Content");
|
||||||
|
|
||||||
|
b1.WithOwner()
|
||||||
|
.HasForeignKey("CreatorId");
|
||||||
|
});
|
||||||
|
|
||||||
|
b.Navigation("About")
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Colors")
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Images")
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Socials")
|
||||||
|
.IsRequired();
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Hutopy.Web.Features.Contents.Data.Subscription", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("Hutopy.Web.Features.Contents.Data.Creator", "Creator")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("CreatorId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Creator");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Hutopy.Web.Features.Contents.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AddsSoftDelete : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<Guid>(
|
||||||
|
name: "DeletedBy",
|
||||||
|
schema: "Content",
|
||||||
|
table: "Contents",
|
||||||
|
type: "uuid",
|
||||||
|
nullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<DateTimeOffset>(
|
||||||
|
name: "DeletedAt",
|
||||||
|
schema: "Content",
|
||||||
|
table: "Contents",
|
||||||
|
type: "timestamp with time zone",
|
||||||
|
nullable: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "DeletedBy",
|
||||||
|
schema: "Content",
|
||||||
|
table: "Contents");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "DeletedAt",
|
||||||
|
schema: "Content",
|
||||||
|
table: "Contents");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,6 +37,12 @@ namespace Hutopy.Web.Features.Contents.Migrations
|
|||||||
b.Property<Guid>("CreatedBy")
|
b.Property<Guid>("CreatedBy")
|
||||||
.HasColumnType("uuid");
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b.Property<Guid?>("DeletedBy")
|
||||||
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("DeletedAt")
|
||||||
|
.HasColumnType("timestamp with time zone");
|
||||||
|
|
||||||
b.Property<string>("Description")
|
b.Property<string>("Description")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasMaxLength(2048)
|
.HasMaxLength(2048)
|
||||||
|
|||||||
Reference in New Issue
Block a user