Files
DS-Cristiano/BlogPlatform/BlogPlatform/Endpoints/Comment/DeleteCommentEndpoint.cs
2025-10-17 11:44:43 +02:00

35 lines
873 B
C#

using BlogPlatform.DTO.Comment.Response;
using FastEndpoints;
namespace BlogPlatform.Endpoints.Comment;
public class DeleteCommentRequest
{
public int Id { get; set; }
}
public class DeleteCommentEndpoint(BlogPlatformDbContext db) : Endpoint<DeleteCommentRequest>
{
public override void Configure()
{
Delete("/api/comments/{@id}", x=> new{id=x.Id});
}
public override async Task HandleAsync(DeleteCommentRequest req, CancellationToken ct)
{
Models.Comment? comment = await db.Comments.FindAsync(req.Id);
if (comment == null)
{
await Send.StringAsync("You must add a comment on a Post that exist and of a User that exists", 400);
return;
}
db.Comments.Remove(comment);
await db.SaveChangesAsync(ct);
await Send.OkAsync(ct);
}
}