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

28 lines
1.1 KiB
C#
Raw Normal View History

2024-08-09 21:55:16 +03:30
using AppException = Netina.Common.Models.Exception.AppException;
2024-04-16 20:01:34 +03:30
namespace Netina.Repository.Handlers.Reviews;
2024-08-09 21:55:16 +03:30
public class CreateReviewCommandHandler(IRepositoryWrapper repositoryWrapper)
: IRequestHandler<CreateReviewCommand, ReviewSDto>
{
public async Task<ReviewSDto> Handle(CreateReviewCommand request, CancellationToken cancellationToken)
{
var review = Review.Create(request.Title, request.Comment, request.Rate, request.IsBuyer, request.ProductId,
request.UserId);
2024-08-09 21:55:16 +03:30
var product = await repositoryWrapper.SetRepository<Product>()
.TableNoTracking
.FirstOrDefaultAsync(p => p.Id == request.ProductId, cancellationToken);
if (product == null)
throw new AppException("Product not found",ApiResultStatusCode.NotFound);
product.AddRate(request.Rate);
2024-08-09 21:55:16 +03:30
repositoryWrapper.SetRepository<Product>().Update(product);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
2024-08-09 21:55:16 +03:30
repositoryWrapper.SetRepository<Review>().Add(review);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return review.AdaptToSDto();
}
}