Exemple #1
0
        public async Task <IActionResult> AddReview(ReviewViewModel reviewViewModel)
        {
            if (ModelState.IsValid)
            {
                var LoggedUser = await userManager.GetUserAsync(User);

                Recipe recipe = repository.Recipes.FirstOrDefault(r => r.RecipeID == reviewViewModel.RecipeID);
                reviewViewModel.Review.UserId = LoggedUser.UserName;
                recipe.AddReview(reviewViewModel.Review);
                double calculateAvgRate = 0;
                int    sumRate          = 0;
                foreach (var rating in recipe.Reviews)
                {
                    sumRate = sumRate + rating.RecipeRating;
                    calculateAvgRate++;
                }
                recipe.RecipeAvgRating = sumRate / calculateAvgRate;
                repository.SaveRecipe(recipe);
                return(View("DisplayPage", recipe));
            }
            else
            {
                return(View(reviewViewModel));
            }
        }