api/NetinaCMS.Repository/Handlers/Blogs/DeleteBlogCommandHandler.cs

28 lines
949 B
C#

using NetinaCMS.Repository.Repositories.Base.Contracts;
namespace NetinaCMS.Repository.Handlers.Blogs;
public class DeleteBlogCommandHandler :IRequestHandler<DeleteBlogCommand,bool>
{
private readonly IRepositoryWrapper _repositoryWrapper;
public DeleteBlogCommandHandler(IRepositoryWrapper repositoryWrapper)
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<bool> Handle(DeleteBlogCommand request, CancellationToken cancellationToken)
{
var ent = await _repositoryWrapper.SetRepository<Blog>()
.TableNoTracking
.FirstOrDefaultAsync(b => b.Id == request.Id, cancellationToken);
if (ent == null)
throw new AppException("Blog is not found", ApiResultStatusCode.NotFound);
_repositoryWrapper.SetRepository<Blog>()
.Delete(ent);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
return true;
}
}