public async Task <double> ChangeRating(RecipeRatingViewModel recipeRatingViewModel) { ApplicationUser user = await base._userManager.GetUserAsync(HttpContext.User); user.UserRecipeRatings = this._recipeService.GetRecipeRatingByUser(user.Id); Recipe recipe = this._recipeService.GetById(recipeRatingViewModel.RecipeId); if (user.Id != recipe.UserId && (user.UserRecipeRatings == null || user.UserRecipeRatings.All(r => r.RecipeId != recipeRatingViewModel.RecipeId))) { recipeRatingViewModel.UserId = user.Id; UserRecipeRating userRecipeRating = this._mapper.Map <UserRecipeRating>(recipeRatingViewModel); recipe = this._recipeService.UpdateByRating(userRecipeRating); return(this._recipeService.CalculateRating(recipe)); } return(DefaultRating); }
public Recipe UpdateByRating(UserRecipeRating recipeRating) { this._repository.Add(recipeRating); return(this.GetById(recipeRating.RecipeId)); }
public void CalculateUserVote(Recipe recipe, string userId) { UserRecipeRating userRecipeRating = recipe.UserRecipeRatings.FirstOrDefault(ur => ur.UserId == userId); recipe.UserRating = userRecipeRating != null ? userRecipeRating.Rating : 0; }