added comment and post endpoints

This commit is contained in:
2025-10-17 11:01:38 +02:00
parent 4aaed0dfc1
commit bf0d8dbf60
2 changed files with 67 additions and 0 deletions

View File

@@ -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<DeleteCommentRequest>
{
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);
}
}

View File

@@ -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<DeletePostRequest>
{
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);
}
}