Exemplo n.º 1
0
        public IActionResult Page(int recipeId)
        {
            var recipeInfo = new RecipeInfo();

            recipeInfo.RecipeIngredients = new List <RecipeIngredients>();
            recipeInfo.Ingredients       = new List <Ingredient.Ingredient>();
            recipeInfo.Category          = new Category();
            recipeInfo.IngredientsList   = new List <string>();
            recipeInfo.Recipe            = _db.Recipes.FirstOrDefault(r => r.Id == recipeId);
            recipeInfo.CurrentUser       = HttpContext.Session.GetInt32("_Userid").Value;

            if (recipeInfo.Recipe == null)
            {
                return(View("Index"));
            }

            if (recipeInfo.Recipe.Photo != null)
            {
                recipeInfo.Image = Convert.ToBase64String(recipeInfo.Recipe.Photo);
            }

            var recipeIngredients = _db.RecipeIngredients
                                    .Where(r => r.RecipeId == recipeId)
                                    .Select(i => new { i.IngredientId, i.Amount, i.AmountType })
                                    .ToArray();
            var ingIds = new List <int>();

            for (var i = 0; i < recipeIngredients.Length; i++)
            {
                recipeInfo.RecipeIngredients.Add(
                    new RecipeIngredients
                {
                    IngredientId = recipeIngredients[i].IngredientId,
                    Amount       = recipeIngredients[i].Amount,
                    AmountType   = recipeIngredients[i].AmountType
                });
                ingIds.Add(recipeIngredients[i].IngredientId);
            }

            recipeInfo.Category = _db.Categories.SingleOrDefault(c => c.Id == recipeInfo.Recipe.CategoryId);

            var ingredients = _db.Ingredients
                              .Where(i => ingIds.Contains(i.Id));

            foreach (var ingredient in ingredients)
            {
                recipeInfo.Ingredients.Add(
                    new Ingredient.Ingredient
                {
                    Id   = ingredient.Id,
                    Name = ingredient.Name
                });
            }

            for (var i = 0; i < recipeInfo.Ingredients.Count; i++)
            {
                var fullIngredient = "";
                if (recipeInfo.RecipeIngredients[i].Amount != "N/A")
                {
                    fullIngredient = recipeInfo.RecipeIngredients[i].Amount + " ";
                    if (recipeInfo.RecipeIngredients[i].AmountType != "N/A")
                    {
                        fullIngredient = fullIngredient + recipeInfo.RecipeIngredients[i].AmountType + " of ";
                    }
                }

                fullIngredient = fullIngredient + recipeInfo.Ingredients[i].Name;
                recipeInfo.IngredientsList.Add(fullIngredient);
            }

            var comments = _db.Comments.Where(c => c.RecipeId == recipeInfo.Recipe.Id)
                           .OrderByDescending(u => u.CreatedAt).ToList();
            var commentInfoList = new List <CommentInfo>();

            foreach (var comment in comments)
            {
                var user = _db.Users.FirstOrDefault(u => u.Id == comment.UserId);
                if (user != null)
                {
                    var commentInfo = new CommentInfo
                    {
                        CommentObj = comment,
                        Commenter  = user.DisplayName
                    };

                    commentInfoList.Add(commentInfo);
                }
            }

            var rating   = 0;
            var dbRating = _db.RecipeRatings.Where(r => r.RecipeId == recipeInfo.Recipe.Id).ToList();

            recipeInfo.RatingCount = dbRating.Count;
            if (dbRating.Count > 0)
            {
                // averaging the rating
                foreach (var rate in dbRating)
                {
                    rating += rate.Rating;
                }

                recipeInfo.Rating = rating / recipeInfo.RatingCount;
            }
            else
            {
                recipeInfo.Rating = rating;
            }


            ViewData["CommentInfo"] = commentInfoList;
            var recipeOwner = _db.Users.FirstOrDefault(u => u.Id == recipeInfo.Recipe.UploaderId);

            if (recipeOwner != null)
            {
                ViewData["RecipeOwner"] = recipeOwner.DisplayName;
            }

            var savedRecipe =
                _db.SavedRecipes.FirstOrDefault(sr => sr.RecipeId == recipeId && sr.UserId == recipeInfo.CurrentUser);

            if (savedRecipe != null)
            {
                ViewData["AlreadySaved"] = true;
            }
            else
            {
                ViewData["AlreadySaved"] = false;
            }


            return(View(recipeInfo));
        }
Exemplo n.º 2
0
        public IActionResult Edit(int recipeId)
        {
            var sessionUId = HttpContext.Session.GetInt32("_Userid");
            var user       = _db.Users.FirstOrDefault(u => u.Id == sessionUId);

            // this block will grab the recipe items, only this user has entered before
            if (user != null)
            {
                var userRecipesIds       = _db.Recipes.Where(r => r.UploaderId == user.Id).Select(r => r.Id).ToList();
                var recipeIngredientsIds =
                    _db.RecipeIngredients.Where(ri => userRecipesIds.Contains(ri.RecipeId))
                    .Select(ri => ri.IngredientId).ToList();
                var viewIngredients = _db.Ingredients.Where(i => recipeIngredientsIds.Contains(i.Id)).ToList();
                ViewData["ingredients"] = viewIngredients;
            }

            // data to populate edit form with old values
            var recipeInfo = new RecipeInfo();

            recipeInfo.Categories        = _db.Categories.OrderBy(s => s.Name).ToList();
            recipeInfo.RecipeIngredients = new List <RecipeIngredients>();
            recipeInfo.Ingredients       = new List <Ingredient.Ingredient>();
            recipeInfo.Category          = new Category();
            recipeInfo.IngredientsList   = new List <string>();
            recipeInfo.Recipe            = _db.Recipes.FirstOrDefault(r => r.Id == recipeId);

            var recipeIngredients = _db.RecipeIngredients
                                    .Where(r => r.RecipeId == recipeId)
                                    .Select(i => new { i.IngredientId, i.Amount, i.AmountType })
                                    .ToArray();
            var ingIds = new List <int>();

            for (var i = 0; i < recipeIngredients.Length; i++)
            {
                recipeInfo.RecipeIngredients.Add(
                    new RecipeIngredients
                {
                    IngredientId = recipeIngredients[i].IngredientId,
                    Amount       = recipeIngredients[i].Amount,
                    AmountType   = recipeIngredients[i].AmountType
                });
                ingIds.Add(recipeIngredients[i].IngredientId);
            }

            recipeInfo.Category = _db.Categories.SingleOrDefault(c => c.Id == recipeInfo.Recipe.CategoryId);

            var ingredients = _db.Ingredients
                              .Where(i => ingIds.Contains(i.Id));

            foreach (var ingredient in ingredients)
            {
                recipeInfo.Ingredients.Add(
                    new Ingredient.Ingredient
                {
                    Id   = ingredient.Id,
                    Name = ingredient.Name
                });
            }

            // formatting the recipe ingredient as
            // "[amount] [unit of measure] of [ingredient]" (1 cup of flour) or
            // "[amount] [ingredient]" (1 chicken breast) or
            // "[ingredient]" (basil)
            for (var i = 0; i < recipeInfo.Ingredients.Count; i++)
            {
                var fullIngredient = "";
                if (recipeInfo.RecipeIngredients[i].Amount != "N/A")
                {
                    fullIngredient = recipeInfo.RecipeIngredients[i].Amount + " ";
                    if (recipeInfo.RecipeIngredients[i].AmountType != "N/A")
                    {
                        fullIngredient = fullIngredient + recipeInfo.RecipeIngredients[i].AmountType + " of ";
                    }
                }

                fullIngredient = fullIngredient + recipeInfo.Ingredients[i].Name;
                recipeInfo.IngredientsList.Add(fullIngredient);
            }

            return(View(recipeInfo));
        }