added comment and post endpoints
This commit is contained in:
33
BlogPlatform/Endpoints/Comment/DeleteCommentEndpoint.cs
Normal file
33
BlogPlatform/Endpoints/Comment/DeleteCommentEndpoint.cs
Normal 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);
|
||||
}
|
||||
}
|
34
BlogPlatform/Endpoints/Post/DeletePostEndpoint.cs
Normal file
34
BlogPlatform/Endpoints/Post/DeletePostEndpoint.cs
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user