Exemplo n.º 1
0
        public IActionResult SearchCategories(int categoryId)
        {
            if (categoryId == 0)
            {
                return(View("Index"));
            }

            // controller action method handles searching by category
            var dbRecipes = _db.Recipes.Where(r => r.CategoryId == categoryId);

            var recipeItems = new List <RecipeItems>();

            foreach (var recipe in dbRecipes)
            {
                var recipeOwner = _db.Users.FirstOrDefault(u => u.Id == recipe.UploaderId);
                if (recipeOwner == null)
                {
                    return(View("Index"));
                }

                var recipeItem = new RecipeItems
                {
                    RecipeId          = recipe.Id,
                    RecipeOwner       = recipeOwner.DisplayName,
                    RecipeOwnerId     = recipe.UploaderId,
                    RecipeName        = recipe.Name,
                    RecipeDescription = recipe.Description,
                    RecipeInstruction = recipe.Instruction,
                    RecipePhoto       = recipe.Photo,
                    RecipeCreatedDate = recipe.CreatedAt,
                    RecipeUpdatedDate = recipe.UpdatedAt,
                    RecipeRating      = _db.RecipeRatings.Where(rr => rr.RecipeId == recipe.Id).Select(rr => rr.Rating)
                                        .FirstOrDefault()
                };

                if (recipeItem.RecipePhoto != null)
                {
                    recipeItem.RecipePhoto64 = Convert.ToBase64String(recipeItem.RecipePhoto);
                }

                if (recipeItem.RecipeDescription.Length > 68)
                {
                    recipeItem.RecipeDescShort = recipeItem.RecipeDescription.Substring(0, 68) + "...";
                }
                else
                {
                    recipeItem.RecipeDescShort = recipeItem.RecipeDescription;
                }

                recipeItems.Add(recipeItem);
            }

            var dbCategories    = _db.Categories.OrderBy(s => Guid.NewGuid()).Take(8).ToList();
            var allDbCategories = _db.Categories.OrderBy(s => s.Name).ToList();

            ViewData["categories"]    = dbCategories;
            ViewData["allCategories"] = allDbCategories;
            ViewData["currentSearch"] =
                _db.Categories.Where(c => c.Id == categoryId).Select(c => c.Name).FirstOrDefault();

            return(View("Index", recipeItems));
        }
Exemplo n.º 2
0
        public IActionResult Search(RecipeSearchInputModel model)
        {
            // Look for recipes where the search term is in the name
            var dbRecipes = _db.Recipes.Where(r => r.Name.Contains(model.SearchQuery)).ToList();

            // LOOKING FOR RECIPES THAT USE THE SEARCH TERM AS AN INGREDIENT
            // get id of this ingredient
            var dbIngredients = _db.Ingredients.Where(i => i.Name.Contains(model.SearchQuery)).ToList();

            // use the id to select recipe_ingredients
            var dbRecipeIngredients = new List <RecipeIngredients>();

            foreach (var ingredient in dbIngredients)
            {
                var temp = _db.RecipeIngredients.Where(ri => ri.IngredientId == ingredient.Id).ToList();
                foreach (var tmp in temp)
                {
                    dbRecipeIngredients.Add(tmp);
                }
            }

            // use the recipe_ingredients recipe_id to select more recipes that include that ingredient
            var recipeIds = new List <int>();

            foreach (var recipeIngredient in dbRecipeIngredients)
            {
                recipeIds.Add(recipeIngredient.RecipeId);
            }

            // add them to the recipes that use the search term in their name
            foreach (var id in recipeIds)
            {
                var flag = false;
                foreach (var recipe in dbRecipes)
                {
                    if (recipe.Id == id)
                    {
                        flag = true;
                    }
                }

                if (!flag)
                {
                    dbRecipes.Add(_db.Recipes.FirstOrDefault(r => r.Id == id));
                }
            }

            // create a recipe item for each recipe
            var recipeItems = new List <RecipeItems>();

            foreach (var recipe in dbRecipes)
            {
                var recipeOwner = _db.Users.FirstOrDefault(u => u.Id == recipe.UploaderId);
                if (recipeOwner == null)
                {
                    return(View("Index"));
                }

                var recipeItem = new RecipeItems
                {
                    RecipeId          = recipe.Id,
                    RecipeOwner       = recipeOwner.DisplayName,
                    RecipeOwnerId     = recipe.UploaderId,
                    RecipeName        = recipe.Name,
                    RecipeDescription = recipe.Description,
                    RecipeInstruction = recipe.Instruction,
                    RecipePhoto       = recipe.Photo,
                    RecipeCreatedDate = recipe.CreatedAt,
                    RecipeUpdatedDate = recipe.UpdatedAt,
                    RecipeRating      = _db.RecipeRatings.Where(rr => rr.RecipeId == recipe.Id).Select(rr => rr.Rating)
                                        .FirstOrDefault()
                };

                if (recipeItem.RecipePhoto != null)
                {
                    recipeItem.RecipePhoto64 = Convert.ToBase64String(recipeItem.RecipePhoto);
                }

                // this is a shortened version of the recipe description for the preview card
                if (recipeItem.RecipeDescription.Length > 68)
                {
                    recipeItem.RecipeDescShort = recipeItem.RecipeDescription.Substring(0, 68) + "...";
                }
                else
                {
                    recipeItem.RecipeDescShort = recipeItem.RecipeDescription;
                }

                recipeItems.Add(recipeItem);
            }

            var dbCategories    = _db.Categories.OrderBy(s => Guid.NewGuid()).Take(8).ToList();
            var allDbCategories = _db.Categories.OrderBy(s => s.Name).ToList();

            ViewData["categories"]    = dbCategories;
            ViewData["allCategories"] = allDbCategories;
            ViewData["currentSearch"] = model.SearchQuery;

            return(View("Index", recipeItems));
        }
Exemplo n.º 3
0
        public IActionResult UserRecipes(int userId)
        {
            // loads up all the recipes created/saved by a specific user
            var dbUser            = _db.Users.FirstOrDefault(u => u.Id == userId);
            var dbRecipes         = _db.Recipes.Where(r => r.UploaderId == userId).ToList();
            var dbSavedRecipeObjs = _db.SavedRecipes.Where(sr => sr.UserId == userId).ToList();
            var dbSavedRecipeIds  = new List <int>();

            foreach (var savedRecipe in dbSavedRecipeObjs)
            {
                dbSavedRecipeIds.Add(savedRecipe.RecipeId);
            }

            var dbSavedRecipes = _db.Recipes.Where(r => dbSavedRecipeIds.Contains(r.Id)).ToList();

            dbRecipes = dbSavedRecipes.Concat(dbRecipes).ToList();
            var recipeItems = new List <RecipeItems>();

            foreach (var recipe in dbRecipes)
            {
                var recipeOwner = _db.Users.FirstOrDefault(u => u.Id == recipe.UploaderId);
                if (recipeOwner == null)
                {
                    return(View("Index"));
                }

                var recipeItem = new RecipeItems
                {
                    RecipeId          = recipe.Id,
                    RecipeOwner       = recipeOwner.DisplayName,
                    RecipeOwnerId     = recipe.UploaderId,
                    RecipeName        = recipe.Name,
                    RecipeDescription = recipe.Description,
                    RecipeInstruction = recipe.Instruction,
                    RecipePhoto       = recipe.Photo,
                    RecipeCreatedDate = recipe.CreatedAt,
                    RecipeUpdatedDate = recipe.UpdatedAt,
                    RecipeRating      = _db.RecipeRatings.Where(rr => rr.RecipeId == recipe.Id).Select(rr => rr.Rating)
                                        .FirstOrDefault()
                };

                if (recipeItem.RecipePhoto != null)
                {
                    recipeItem.RecipePhoto64 = Convert.ToBase64String(recipeItem.RecipePhoto);
                }

                if (recipeItem.RecipeDescription.Length > 68)
                {
                    recipeItem.RecipeDescShort = recipeItem.RecipeDescription.Substring(0, 68) + "...";
                }
                else
                {
                    recipeItem.RecipeDescShort = recipeItem.RecipeDescription;
                }

                recipeItems.Add(recipeItem);
            }

            var dbCategories    = _db.Categories.OrderBy(s => Guid.NewGuid()).Take(8).ToList();
            var allDbCategories = _db.Categories.OrderBy(s => s.Name).ToList();

            ViewData["categories"]    = dbCategories;
            ViewData["allCategories"] = allDbCategories;
            if (dbUser != null)
            {
                ViewData["currentSearch"] = dbUser.DisplayName + "'s Created/Favorited Recipes";
            }

            return(View("Index", recipeItems));
        }