public async Task ReviewAddsNewActivityReviewToDbContext()
        {
            await this.AddTestingDestinationToDb();

            await this.AddTestingActivityToDb();

            await this.AddTestingUserToDb();

            var activityReviewInputModel = new ActivityReviewInputModel()
            {
                Id      = TestActivityId,
                Name    = TestActivityName,
                Rating  = TestReviewRating,
                Content = TestReviewContent,
            };

            await this.ActivitiesServiceMock.Review(TestActivityId, TestUserName, activityReviewInputModel);

            var reviewId     = this.DbContext.Reviews.Last().Id;
            var reviewsDbSet = this.DbContext.ActivityReviews.OrderBy(r => r.CreatedOn);

            Assert.Collection(reviewsDbSet,
                              elem1 =>
            {
                Assert.Equal(reviewsDbSet.Last().ActivityId, activityReviewInputModel.Id);
                Assert.Equal(reviewsDbSet.Last().ReviewId, reviewId);
            });
        }
        public async Task ReviewThrowsArgumentExceptionIfUserHasAlreadyReviewedRestaurant()
        {
            await this.AddTestingDestinationToDb();

            await this.AddTestingActivityToDb();

            await this.AddTestingUserToDb();

            var activityReviewInputModel = new ActivityReviewInputModel()
            {
                Id      = TestActivityId,
                Name    = TestActivityName,
                Rating  = TestReviewRating,
                Content = TestReviewContent,
            };

            await this.ActivitiesServiceMock.Review(TestActivityId, TestUserName, activityReviewInputModel);

            var secondActivityReviewInputModel = new ActivityReviewInputModel
            {
                Id      = TestActivityId,
                Name    = TestActivityName,
                Rating  = SecondTestReviewRating,
                Content = TestReviewContent,
            };

            var exception = await Assert.ThrowsAsync <ArgumentException>(() =>
                                                                         this.ActivitiesServiceMock.Review(TestActivityId, TestUserName, secondActivityReviewInputModel));

            Assert.Equal(string.Format(ServicesDataConstants.ActivityReviewAlreadyAdded, TestUserName, TestActivityId, TestActivityName), exception.Message);
        }
        public async Task <IActionResult> Review(int id, ActivityReviewInputModel activityReviewInputModel)
        {
            var username = this.User.Identity.Name;

            await this.activitiesService.Review(id, username, activityReviewInputModel);

            return(this.RedirectToAction(nameof(this.Details), new { id }));
        }
        public async Task Review(int activityId, string username, ActivityReviewInputModel activityReviewInputModel)
        {
            var user = await this.usersRepository.All().Where(u => u.UserName == username).FirstOrDefaultAsync();

            if (user == null)
            {
                throw new NullReferenceException(string.Format(ServicesDataConstants.NullReferenceUsername, username));
            }

            var activity = await this.activitiesRepository.All().FirstOrDefaultAsync(a => a.Id == activityId);

            if (activity == null)
            {
                throw new NullReferenceException(string.Format(ServicesDataConstants.NullReferenceActivityId, activityId));
            }

            if (this.activityReviewsRepository.All().Any(r => r.ActivityId == activityId && r.Review.User == user))
            {
                throw new ArgumentException(string.Format(ServicesDataConstants.ActivityReviewAlreadyAdded, user.UserName, activity.Id, activity.Name));
            }

            var review = new Review
            {
                User    = user,
                Rating  = activityReviewInputModel.Rating,
                Content = activityReviewInputModel.Content,
            };

            this.reviewsRepository.Add(review);
            await this.reviewsRepository.SaveChangesAsync();

            var activityReview = new ActivityReview
            {
                Activity = activity,
                Review   = review,
            };

            this.activityReviewsRepository.Add(activityReview);
            await this.activityReviewsRepository.SaveChangesAsync();

            await this.UpdateActivityAverageRating(activity);
        }
        public async Task UpdateRestaurantAverageRatingCalculatesRatingCorrectly()
        {
            await this.AddTestingDestinationToDb();

            await this.AddTestingActivityToDb();

            await this.AddTestingUserToDb();

            this.DbContext.Users.Add(new UnravelTravelUser {
                Id = Guid.NewGuid().ToString(), UserName = "******"
            });
            await this.DbContext.SaveChangesAsync();

            var activityReviewInputModel = new ActivityReviewInputModel()
            {
                Id      = TestActivityId,
                Name    = TestActivityName,
                Rating  = TestReviewRating,
                Content = TestReviewContent,
            };

            await this.ActivitiesServiceMock.Review(TestActivityId, TestUserName, activityReviewInputModel);

            var secondActivityReviewInputModel = new ActivityReviewInputModel()
            {
                Id      = TestActivityId,
                Name    = TestActivityName,
                Rating  = SecondTestReviewRating,
                Content = TestReviewContent,
            };

            await this.ActivitiesServiceMock.Review(TestActivityId, "Ivan", secondActivityReviewInputModel);

            var expected = (TestReviewRating + SecondTestReviewRating) / 2;
            var actual   = this.DbContext.Activities.Find(TestActivityId).AverageRating;

            Assert.Equal(expected, actual);
        }