Exemplo n.º 1
0
 public async Task <ActionResult <GetProductReviewModel> > PostReview(Guid id, [FromBody] SendProductReviewModel model)
 {
     return(GetResult(await ProductReviewService.SaveReviewAsync(id, model)));
 }
Exemplo n.º 2
0
        public async Task <Result <GetProductReviewModel> > SaveReviewAsync(Guid productId, SendProductReviewModel model)
        {
            if (!CustomerAuthorizationService.CustomerIdIsAuthorized(productId))
            {
                return(Result <GetProductReviewModel> .Forbidden());
            }

            if (await ProductRepository.GetFirstOrDefaultAsync(predicate: p => p.Id == productId) == null)
            {
                return(Result <GetProductReviewModel> .BadRequest($"Product with Id {productId} does not exist."));
            }

            if (await CustomerRepository.GetFirstOrDefaultAsync(predicate: c => c.Id == model.CustomerId) == null)
            {
                return(Result <GetProductReviewModel> .BadRequest($"Customer with Id {model.CustomerId} does not exist."));
            }

            if (await ProductReviewRepository.GetFirstOrDefaultAsync(predicate: fp => fp.CustomerId == model.CustomerId && fp.ProductId == productId) != null)
            {
                return(Result <GetProductReviewModel> .BadRequest($"Customer with id {model.CustomerId} already has a review for product with id {productId}."));
            }

            var review = Mapper.Map <ProductReview>(model);

            review.ProductId = productId;

            await ProductReviewRepository.InsertAsync(review);

            await UnitOfWork.SaveChangesAsync();

            return(Result <GetProductReviewModel> .Success(Mapper.Map <GetProductReviewModel>(review)));
        }