public CustomerReview GetById(string id)
 {
     using (ICustomerReviewRepository repository = _repositoryFactory())
     {
         CustomerReviewEntity entity = repository.GetCustomerReview(id);
         return(entity.ToModel(AbstractTypeFactory <CustomerReview> .TryCreateInstance()));
     }
 }
 public float Count(CustomerReviewEntity review)
 {
     using (var repository = _repositoryFactory())
     {
         var   reviews = repository.GetProductReviews(review.ProductId);
         float rating  = (float)(reviews.Sum(c => (int)c.Rating) + (int)review.Rating) / (reviews.Length + 1);
         return(rating);
     }
 }
        public void Patch_ShouldThrowArgumentNullException_IfInputModelIsNull()
        {
            //arrange
            var sourceEntity = randomizer.Create <CustomerReviewEntity>();
            CustomerReviewEntity destinationEntity = null;

            //act
            Action act = () => sourceEntity.Patch(destinationEntity);

            //assert
            act.Should().Throw <ArgumentNullException>();
        }
        public void GetProductRating_ShouldBeEquals(float expected, float review1Rating, float review2Rating)
        {
            // arrange
            var fakeProductId = "fake_product_id";
            var review1       = new CustomerReviewEntity {
                ProductId = fakeProductId, Rating = review1Rating
            };
            var review2 = new CustomerReviewEntity {
                ProductId = fakeProductId, Rating = review2Rating
            };
            var repositoryResult = new List <CustomerReviewEntity>
            {
                review1,
                review2
            };

            _repositoryFactory.Setup(repository => repository.CustomerReviews).Returns(repositoryResult.AsQueryable());

            // act
            var result = _customerReviewService.GetProductRating("fake_product_id");

            // assert
            Assert.Equal(expected, result);
        }
示例#5
0
 private static CustomerReview ConvertEntityToModel(CustomerReviewEntity entity)
 {
     return(entity.ToModel(AbstractTypeFactory <CustomerReview> .TryCreateInstance()));
 }
        private void CalculateRating(ICustomerReviewRepository customerReviewRepository, CustomerReviewEntity review)
        {
            var reviews = customerReviewRepository
                          .CustomerReviews
                          .Where(r => r.ProductId == review.ProductId && r.Id != review.Id)
                          .ToList();

            reviews.Add(review);

            var productRatingValue = RateCalculationUtil.CalculateProductRating(reviews);

            using (var productRatingRepository = _productRatingRepositoryFactory())
                using (var changeTracker = GetChangeTracker(productRatingRepository))
                {
                    var productRating = productRatingRepository.GetProductRatingByProductId(review.ProductId);

                    if (productRating == null)
                    {
                        productRating = AbstractTypeFactory <ProductRatingEntity> .TryCreateInstance();

                        productRating.ProductId = review.ProductId;
                        productRating.Rating    = productRatingValue;
                        productRatingRepository.Add(productRating);
                    }
                    else
                    {
                        changeTracker.Attach(productRating);
                        productRating.Rating = productRatingValue;
                    }

                    CommitChanges(productRatingRepository);
                }
        }