Esempio n. 1
0
        public void AddReview(Domain.Review review)
        {
            var model = new ReviewModel(review);

            _context.Reviews.Add(model);

            var movie = _context.Movies.FirstOrDefault(x => x.Id == review.Movie.Id);

            // Throw error if no movie found
            if (movie == null || movie.Reviews == null)
            {
                throw new ArgumentException("No movie found");
            }

            var amountOfReviews = movie.ReviewIds.Select(x => x).ToList().Count;

            if (amountOfReviews == 0)
            {
                amountOfReviews = 1;
            }

            // Calculate new rating
            var average    = movie.Rating * (amountOfReviews - 1);
            var newAverage = average + review.Rating.Value;
            var newRating  = newAverage / amountOfReviews;

            movie.ReviewIds.Add(review.Id);
            movie.Rating = newRating;

            _context.SaveChanges();
        }
        public void GivenANewProductWhenTheProductHasAReviewThenTheReviewIsPersisted()
        {
            var review = new Review(Names.FullName(), Strings.GenerateAlphaNumericString(), Strings.GenerateAlphaNumericString(maxLength: 1000), Numbers.Int(1, 5));
            _product.AddReview(review);

            _productRepository.Save(_product);

            var persitedProduct = _collection.Get(_product.Id);

            Assert.That(persitedProduct.Reviews.First().Author, Is.EqualTo(review.Author));
        }
Esempio n. 3
0
 public ReviewModel(Domain.Review review)
 {
     Id    = review.Id;
     Movie = review.Movie.Model == null
         ? null
         : new MovieModel(review.Movie.Model);
     Author = review.Author.Model == null
         ? null
         : new UserModel(review.Author.Model);
     Comments   = review.Comments.Select(x => new CommentModel(x.Model)).ToList();
     CommentIds = review.Comments.Select(x => x.Id).ToList();
     Content    = review.Content.Value;
     Rating     = review.Rating.Value;
     CreatedAt  = review.CreatedAt;
     MovieId    = review.Movie.Id;
     AuthorId   = review.Author.Id;
 }
Esempio n. 4
0
        public void SetUp()
        {
            _title = Strings.GenerateAlphaNumericString();
            _description = Strings.GenerateAlphaNumericString();
            _cost = Numbers.Double();
            _reviewTitle = Strings.GenerateAlphaNumericString();
            _reviewContent = Strings.GenerateAlphaNumericString();
            _reviewAuthor = Names.FullName();
            _reviewScore = Numbers.Int(1, 5);

            _review = new Review(_reviewAuthor, _reviewTitle, _reviewContent, _reviewScore);
            _product = new Product(_title, _description, _cost);
        }
Esempio n. 5
0
 public void AddReview(Review review)
 {
     _reviews.Add(review);
 }
Esempio n. 6
0
 public void WriteReview(long movieId, Domain.Review review) => _reviewRepository.AddReview(review);