From bf0d8dbf603245bc8366769c0454c75a619b3c91 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Fri, 17 Oct 2025 11:01:38 +0200 Subject: [PATCH] added comment and post endpoints --- .../Comment/DeleteCommentEndpoint.cs | 33 ++++++++++++++++++ .../Endpoints/Post/DeletePostEndpoint.cs | 34 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 BlogPlatform/Endpoints/Comment/DeleteCommentEndpoint.cs create mode 100644 BlogPlatform/Endpoints/Post/DeletePostEndpoint.cs 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