Esempio n. 1
0
        public async Task <IActionResult> GetRecipe(int id)
        {
            Recipe recipe = _appDb.Recipes.Find(id);


            var sender = await _userManager.FindByIdAsync(recipe.UserId);

            RecipeVM recipeVM = new RecipeVM();

            recipeVM.RecipeId           = recipe.RecipeId;
            recipeVM.RecipeName         = recipe.RecipeName;
            recipeVM.RecipeCategory     = recipe.RecipeCategory;
            recipeVM.RecipePortions     = recipe.RecipePortions;
            recipeVM.RecipeTime         = recipe.RecipeTime;
            recipeVM.RecipeInstructions = recipe.RecipeInstructions;
            recipeVM.Ingridients        = _appDb.Ingridients.Where(r => id == r.RecipeId).ToList();
            recipeVM.Rating             = _ratingServices.GetRecipeRating(recipe.RecipeId);
            recipeVM.Sender             = sender;
            recipeVM.PostedOn           = recipe.PostedOn;
            recipeVM.Images             = _appDb.Images.Where(i => i.RecipeId == recipe.RecipeId && i.UserId == recipe.UserId).ToList();
            recipeVM.Comments           = _appDb.Comments.Where(c => c.RecipeId == id).ToList();

            foreach (var comment in recipeVM.Comments)
            {
                comment.Likes    = _appDb.CommentLikes.Where(c => c.CommentId == comment.CommentID && c.isLike == true).ToList().Count;
                comment.Dislikes = _appDb.CommentLikes.Where(c => c.CommentId == comment.CommentID && c.isDislike == true).ToList().Count;
                if (_appDb.CommentLikes.Where(c => c.CommentId == comment.CommentID && c.isLike == true && c.UserId == _userManager.GetUserId(User)).Any() == true)
                {
                    comment.Reacted = "like-pressed";
                }
                if (_appDb.CommentLikes.Where(c => c.CommentId == comment.CommentID && c.isDislike == true && c.UserId == _userManager.GetUserId(User)).Any() == true)
                {
                    comment.Reacted = "dislike-pressed";
                }
            }

            return(View("RecipePage", recipeVM));
        }