Api/Netina.Repository/Handlers/Reviews/DeleteReviewCommandHandler.cs

24 lines
922 B
C#

using Microsoft.EntityFrameworkCore;
namespace Netina.Repository.Handlers.Reviews;
public class DeleteReviewCommandHandler : IRequestHandler<DeleteReviewCommand,bool>
{
private readonly IRepositoryWrapper _repositoryWrapper;
public DeleteReviewCommandHandler(IRepositoryWrapper repositoryWrapper)
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<bool> Handle(DeleteReviewCommand request, CancellationToken cancellationToken)
{
var review = await _repositoryWrapper.SetRepository<Review>().TableNoTracking
.FirstOrDefaultAsync(r => r.Id == request.Id, cancellationToken);
if (review == null)
throw new AppException("Review not found", ApiResultStatusCode.NotFound);
_repositoryWrapper.SetRepository<Review>().Delete(review);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
return true;
}
}