Пример #1
0
        public IActionResult Edit(AddEditRecipeViewModel addEditRecipeViewModel)
        {
            //Check if edit was successful
            if (ModelState.IsValid)
            {
                //Converts the ViewModel into a recipe object
                var editRecipe = AddEditRecipeViewModel.CreateRecipe(addEditRecipeViewModel);
                context.Recipes.Update(editRecipe);

                //Check ingredients
                //get the old ingredient list to compare
                IList<Ingredient> prevIngredients = context.Ingredients.Include(p => p.Recipe).Where(p => p.RecipeID == editRecipe.ID).ToList();

                //Go through each ingredients from before it was edited
                foreach (var ingredient in prevIngredients)
                {
                    //Two Case
                    //edited this ingredient
                    if (addEditRecipeViewModel.IngredientsID.Contains(ingredient.ID)){
                        int index = addEditRecipeViewModel.IngredientsID.IndexOf(ingredient.ID);
                        ingredient.Name = addEditRecipeViewModel.IngredientsName[index];
                        context.Update(ingredient);
                    }
                    //deleted this ingredient
                    else
                    {
                        context.Remove(ingredient);
                    }                     
                }
    
                //If added to the ingredient list with the JS, I will be under NewIngredientsName IList
                if (addEditRecipeViewModel.NewIngredientsName != null)
                {
                    foreach (var ingredient in addEditRecipeViewModel.NewIngredientsName)
                    {
                        //Created a new ingredient
                        var newIngredient = AddEditRecipeViewModel.CreateIngredient(ingredient, editRecipe.ID);
                        context.Ingredients.Add(newIngredient);
                    }
                }
                
                context.SaveChanges();
                return Redirect("/");
            }

            //reposted the form it there was errors
            return View(addEditRecipeViewModel);
        }
Пример #2
0
        public IActionResult Add(AddEditRecipeViewModel addEditRecipeViewModel)
        {
            if (ModelState.IsValid)
            {
                //Make recipe 
                var newRecipe = AddEditRecipeViewModel.CreateRecipe(addEditRecipeViewModel);
                context.Recipes.Add(newRecipe);
                context.SaveChanges();


                //Make Ingredients
                foreach (var ingredientName in addEditRecipeViewModel.NewIngredientsName)
                {
                    var newIngredient = AddEditRecipeViewModel.CreateIngredient(ingredientName, newRecipe.ID);
                    context.Ingredients.Add(newIngredient);
                }


                context.SaveChanges();
                return Redirect("/");
            }

            return View(addEditRecipeViewModel);
        }