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

26 lines
1.1 KiB
C#
Raw Normal View History

2024-08-09 21:55:16 +03:30
namespace Netina.Repository.Handlers.Reviews;
2024-02-12 22:01:15 +03:30
2024-08-09 21:55:16 +03:30
public class DeleteReviewCommandHandler(IRepositoryWrapper repositoryWrapper)
: IRequestHandler<DeleteReviewCommand, Guid>
{
public async Task<Guid> Handle(DeleteReviewCommand request, CancellationToken cancellationToken)
{
2024-08-09 21:55:16 +03:30
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);
var product = await repositoryWrapper.SetRepository<Product>()
.TableNoTracking
.FirstOrDefaultAsync(p => p.Id == review.ProductId, cancellationToken);
if (product != null)
{
product.RemoveRate(review.Rate);
repositoryWrapper.SetRepository<Product>().Update(product);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
2024-08-09 21:55:16 +03:30
repositoryWrapper.SetRepository<Review>().Delete(review);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return review.Id;
}
}