示例#1
0
        public IActionResult Process(RecipeInputModel model)
        {
            var sessionUId = HttpContext.Session.GetInt32("_Userid");

            // controller action method to process new recipes
            if (!ModelState.IsValid || model.Ingredients == null)
            {
                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 ingredients = _db.Ingredients.Where(i => recipeIngredientsIds.Contains(i.Id)).ToList();
                    ViewData["ingredients"] = ingredients;
                }

                var dbCategories = _db.Categories.OrderBy(s => s.Name).ToList();
                ViewData["categories"] = dbCategories;
                if (model.Ingredients == null)
                {
                    ModelState.AddModelError("ingDiv", "Ingredients are required");
                }

                return(View("New"));
            }

            var catId  = _db.Categories.First(u => u.Name == model.Category).Id;
            var recipe = new Recipe();

            if (model.Photo != null) // user provided photo
            {
                var size   = model.Photo.Length;
                var stream = new MemoryStream();
                if (size > 0)
                {
                    using (stream)
                    {
                        model.Photo.CopyTo(stream);
                    }
                }

                recipe = new Recipe
                {
                    Name        = model.Name,
                    Description = model.Description,
                    Instruction = model.Instruction,
                    CategoryId  = catId,
                    Photo       = stream.ToArray(),
                    UploaderId  = sessionUId.Value,
                    CreatedAt   = DateTime.Now
                };
                _db.Recipes.Add(recipe);
            }
            else // user did not provide photo
            {
                recipe = new Recipe
                {
                    Name        = model.Name,
                    Description = model.Description,
                    Instruction = model.Instruction,
                    CategoryId  = catId,
                    UploaderId  = sessionUId.Value,
                    CreatedAt   = DateTime.Now
                };
                _db.Recipes.Add(recipe);
            }

            _db.SaveChanges();

            foreach (var ingredientItem in model.Ingredients)
            {
                var ingredient   = new Ingredient.Ingredient();
                var dbIngredient = _db.Ingredients.SingleOrDefault(i => i.Name == ingredientItem.Ingredient);
                if (dbIngredient == null)
                {
                    ingredient = new Ingredient.Ingredient
                    {
                        Name      = ingredientItem.Ingredient,
                        CreatedAt = DateTime.Now
                    };
                    _db.Ingredients.Add(ingredient);
                    _db.SaveChanges();
                }
                else
                {
                    ingredient = dbIngredient;
                }

                // checking for duplicate recipe ingredients
                var dupe = _db.RecipeIngredients.FirstOrDefault(ri =>
                                                                ri.RecipeId == recipe.Id && ri.IngredientId == ingredient.Id);
                if (dupe == null)
                {
                    var recipeIngredient = new RecipeIngredients
                    {
                        IngredientId = ingredient.Id,
                        RecipeId     = recipe.Id,
                        Amount       = ingredientItem.Amount,
                        AmountType   = ingredientItem.AmountType
                    };
                    _db.RecipeIngredients.Add(recipeIngredient);
                    _db.SaveChanges();
                }
            }

            return(RedirectToAction("Page", new { recipeId = recipe.Id }));
        }
示例#2
0
        public IActionResult Update(RecipeInputModel model)
        {
            if (!ModelState.IsValid || model.Ingredients == null)
            {
                return(RedirectToAction("Edit", new { recipeId = model.RecipeId }));
            }

            var category = _db.Categories.SingleOrDefault(c => c.Name == model.Category);
            int categoryId;

            var dbRecipe = _db.Recipes.SingleOrDefault(r => r.Id == model.RecipeId);

            if (dbRecipe == null)
            {
                return(RedirectToAction("Index"));
            }

            // checking if the user specified a category
            if (category == null)
            {
                categoryId = dbRecipe.CategoryId;
            }
            else
            {
                categoryId = category.Id;
            }

            // updating selected recipe
            dbRecipe.Name        = model.Name;
            dbRecipe.Description = model.Description;
            dbRecipe.Instruction = model.Instruction;
            dbRecipe.CategoryId  = categoryId;
            dbRecipe.UpdatedAt   = DateTime.Now;
            if (model.Photo != null)
            {
                var stream = new MemoryStream();
                if (model.Photo.Length > 0)
                {
                    using (stream)
                    {
                        model.Photo.CopyTo(stream);
                    }

                    dbRecipe.Photo = stream.ToArray();
                }
            }

            _db.SaveChanges();

            // removing ingredients have been removed
            var deleteIngredients =
                from recipeIngredients in _db.RecipeIngredients
                where recipeIngredients.RecipeId == model.RecipeId
                select recipeIngredients;

            foreach (var ingredient in deleteIngredients)
            {
                _db.RecipeIngredients.Remove(ingredient);
            }

            _db.SaveChanges();

            // adding back ingredients that are still there, checking for duplicates
            foreach (var ingredientItem in model.Ingredients)
            {
                var ingredient   = new Ingredient.Ingredient();
                var dbIngredient = _db.Ingredients.SingleOrDefault(i => i.Name == ingredientItem.Ingredient);
                if (dbIngredient == null)
                {
                    ingredient = new Ingredient.Ingredient
                    {
                        Name      = ingredientItem.Ingredient,
                        CreatedAt = DateTime.Now
                    };
                    _db.Ingredients.Add(ingredient);
                    _db.SaveChanges();
                }
                else
                {
                    ingredient = dbIngredient;
                }

                var dupe = _db.RecipeIngredients.FirstOrDefault(ri =>
                                                                ri.RecipeId == dbRecipe.Id && ri.IngredientId == ingredient.Id);
                if (dupe == null)
                {
                    var recipeIngredient = new RecipeIngredients
                    {
                        IngredientId = ingredient.Id,
                        RecipeId     = dbRecipe.Id,
                        Amount       = ingredientItem.Amount,
                        AmountType   = ingredientItem.AmountType
                    };
                    _db.RecipeIngredients.Add(recipeIngredient);
                    _db.SaveChanges();
                }
            }

            return(RedirectToAction("Page", new { recipeId = dbRecipe.Id }));
        }