public async Task <IActionResult> CreateAsync( ProductReviewDto productReview, CancellationToken cancellationToken = default) { var result = await _productReviewManager.CreateProductReviewAsync(productReview, cancellationToken); return(Ok(result)); }
public async Task Add(int productId, ProductReviewDto reviewDto) { if (productId != reviewDto.ProductId) { throw new InvalidRequestData(InvalidProductIdMessage); } var review = _mapper.Map <ProductReview>(reviewDto); await _reviewManager.AddReview(review); }
public async Task <ProductReviewDto> CreateProductReviewAsync(ProductReviewDto productReview, CancellationToken cancellationToken = default) { var add = _mapper.Map <ProductReview>(productReview); _shopContext.ProductReviews.Add(add); await _shopContext.SaveChangesAsync(cancellationToken); return(_mapper.Map <ProductReviewDto>(productReview)); }
public async Task <ActionResult> SubmitReview(ProductReviewDto productReview) { AppUser user = await userManager.FindByEmailFromClaimsPrinciple(HttpContext.User); bool hasComment = !string.IsNullOrEmpty(productReview.Comment); Product product = await unitOfWork.Repository <Product>().GetByIdAsync(productReview.ProductId); if (product == null) { return(BadRequest(new ApiResponse(400, "The product you are submiting review doesn't exists!"))); } IReadOnlyList <ProductReview> currentReviews = await unitOfWork.Repository <ProductReview>() .ListAsync(new ProductReviewFindByProductId(product.Id)); bool userAlreadyHasSubmuitReview = false; int sumOfAllReviewRating = 0; foreach (var currentReview in currentReviews) { sumOfAllReviewRating += currentReview.StarRate; if (currentReview.UserId == user.Id) { //User already has submitted review userAlreadyHasSubmuitReview = true; } } if (userAlreadyHasSubmuitReview) { return(BadRequest(new ApiResponse(400, "You have already submitted review!"))); } ProductReview review = new ProductReview() { UserId = user.Id, UserEmail = user.Email, Product = product, UserName = user.DisplayName, Date = DateTime.Now, StarRate = productReview.StarRating, HasComment = hasComment, Comment = productReview.Comment }; //we calculate avarage product review by adding this review also decimal productAvarageRating = (decimal)((decimal)sumOfAllReviewRating + (decimal)review.StarRate) / ((decimal)currentReviews.Count + 1); product.AverageReviewRate = productAvarageRating; unitOfWork.Repository <ProductReview>().Add(review); unitOfWork.Repository <Product>().Update(product); await unitOfWork.Complete(); return(Ok(new ApiResponse(200, "Thank you for submiting review!"))); }
public void PostingReview_Will_Fail_When_productIdIsNotConsistent() { var productReviewDto = new ProductReviewDto { ProductId = 2, Score = 2, Title = "MyTitle", Recommend = true, Comments = "Some Comments" }; Assert.ThrowsAsync <InvalidRequestData>( () => _controller.Add(4, productReviewDto)); }