2024-08-09 21:55:16 +03:30
|
|
|
|
using AppException = Netina.Common.Models.Exception.AppException;
|
2024-01-01 16:18:49 +03:30
|
|
|
|
|
2024-04-16 20:01:34 +03:30
|
|
|
|
namespace Netina.Repository.Handlers.Reviews;
|
2024-01-01 16:18:49 +03:30
|
|
|
|
|
2024-09-01 12:28:42 +03:30
|
|
|
|
public class CreateReviewCommandHandler(IRepositoryWrapper repositoryWrapper,ICurrentUserService currentUserService)
|
|
|
|
|
|
: IRequestHandler<CreateReviewCommand, Guid>
|
2024-01-01 16:18:49 +03:30
|
|
|
|
{
|
2024-09-01 12:28:42 +03:30
|
|
|
|
public async Task<Guid> Handle(CreateReviewCommand request, CancellationToken cancellationToken)
|
2024-01-01 16:18:49 +03:30
|
|
|
|
{
|
2024-09-01 12:28:42 +03:30
|
|
|
|
Guid userId = request.UserId;
|
|
|
|
|
|
if (userId == default)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Guid.TryParse(currentUserService.UserId, out userId))
|
|
|
|
|
|
throw new AppException("User id is wrong", ApiResultStatusCode.BadRequest);
|
|
|
|
|
|
}
|
2024-01-01 16:18:49 +03:30
|
|
|
|
var review = Review.Create(request.Title, request.Comment, request.Rate, request.IsBuyer, request.ProductId,
|
2024-09-01 12:28:42 +03:30
|
|
|
|
userId);
|
2024-08-09 21:55:16 +03:30
|
|
|
|
var product = await repositoryWrapper.SetRepository<Product>()
|
2024-01-01 16:18:49 +03:30
|
|
|
|
.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-01-01 16:18:49 +03:30
|
|
|
|
|
2024-08-09 21:55:16 +03:30
|
|
|
|
repositoryWrapper.SetRepository<Review>().Add(review);
|
|
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
2024-01-01 16:18:49 +03:30
|
|
|
|
|
2024-09-01 12:28:42 +03:30
|
|
|
|
return review.Id;
|
2024-01-01 16:18:49 +03:30
|
|
|
|
}
|
|
|
|
|
|
}
|