diff --git a/BlogPlatform/Endpoints/Comment/DeleteCommentEndpoint.cs b/BlogPlatform/Endpoints/Comment/DeleteCommentEndpoint.cs new file mode 100644 index 0000000..f8f03c1 --- /dev/null +++ b/BlogPlatform/Endpoints/Comment/DeleteCommentEndpoint.cs @@ -0,0 +1,33 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace BlogPlatform.Endpoints.Comment; + +public class DeleteCommentRequest +{ + public int Id { get; set; } +} + +public class DeleteCommentEndpoint(BlogPlatformDbContext database) : Endpoint +{ + public override void Configure() + { + Delete("/api/comments/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(DeleteCommentRequest req, CancellationToken ct) + { + var comment = await database.Comments.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (comment == null) + { + await Send.NotFoundAsync(ct); + return; + } + + database.Comments.Remove(comment); + await database.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } +} \ No newline at end of file diff --git a/BlogPlatform/Endpoints/Post/DeletePostEndpoint.cs b/BlogPlatform/Endpoints/Post/DeletePostEndpoint.cs new file mode 100644 index 0000000..b859e61 --- /dev/null +++ b/BlogPlatform/Endpoints/Post/DeletePostEndpoint.cs @@ -0,0 +1,34 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace BlogPlatform.Endpoints.Post; + +public class DeletePostRequest +{ + public int Id { get; set; } +} + +public class DeletePostEndpoint(BlogPlatformDbContext database) : Endpoint +{ + public override void Configure() + { + Delete("/api/posts/{@Id}", x => new {x.Id}); + AllowAnonymous(); + } + + public override async Task HandleAsync(DeletePostRequest req, CancellationToken ct) + { + var post = await database.Posts.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (post == null) + { + await Send.NotFoundAsync(ct); + return; + } + + database.Posts.Remove(post); + await database.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } +} \ No newline at end of file