1
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
namespace Yes.Application.Admins.Articles
|
||||
{
|
||||
public record DeleteArticleCommand(int Id) : IRequest<DeleteArticleCommandResponse>;
|
||||
|
||||
public record DeleteArticleCommandResponse(int Id);
|
||||
|
||||
public class DeleteArticleCommandHandler(BlogDbContext db) : IRequestHandler<DeleteArticleCommand, DeleteArticleCommandResponse>
|
||||
{
|
||||
private readonly BlogDbContext _db = db;
|
||||
|
||||
public async Task<DeleteArticleCommandResponse> Handle(DeleteArticleCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var article = await _db.Articles.FindAsync(request.Id);
|
||||
if (article != null)
|
||||
{
|
||||
article.Delete();
|
||||
_db.Articles.Update(article);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return new DeleteArticleCommandResponse(request.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user