Пример #1
0
        //Create a new recipe
        public void CreateRecipe(RecipeDto recipeDto)
        {
            var recipeIngredients = new List <RecipeIngredient>();

            //Create ingrenient if it doesnt exist in DB when creating a recipe
            foreach (var ingredient in recipeDto.RecipeIngredients)
            {
                var ingredientExist = _db.Ingredients.FirstOrDefault(i => i.Id == ingredient.IngredientId);
                if (ingredientExist == null)
                {
                    //If ingrediens doesn't exist add it to DB
                    var newIngredient = new Ingredient()
                    {
                        Name      = ingredient.Name,
                        UnitPrice = ingredient.UnitPrice
                    };
                    _db.Ingredients.Add(newIngredient);
                    _db.SaveChanges();

                    ingredient.IngredientId = newIngredient.Id;
                }

                //Adding ingredients to RecipeIngredients
                //foreach (var ingredient in recipeDto.RecipeIngredients)
                //{
                var recipeIngredient2 = new RecipeIngredient()
                {
                    RecipeId     = ingredient.RecipeId,
                    IngredientId = ingredient.IngredientId,
                    Value        = ingredient.Value,
                    Measure      = ingredient.Measure
                };
                recipeIngredients.Add(recipeIngredient2);
            }

            //Adding a new recipe
            var newRecipe = new Recipe()
            {
                Name              = recipeDto.Name,
                Description       = recipeDto.Description,
                CategoryId        = recipeDto.CategoryId,
                Difficulty        = recipeDto.Difficulty,
                RecipeIngredients = recipeIngredients
            };

            _db.Recipes.Add(newRecipe);
            //Save recipe to DB
            _db.SaveChanges();
        }
Пример #2
0
        public ActionResult Edit([Bind(Include = "Name, ShortDescription, IngredientString, Preparation, CookingInstructions, ServingInstructions, NutritionInformation")] Recipe newRecipe)
        {
            if (newRecipe == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (TryUpdateModel(newRecipe))
            {
                try
                {
                    _db.Entry(newRecipe).State = EntityState.Modified;
                    _db.SaveChanges();

                    return(RedirectToAction("List"));
                }
                catch (DataException /* dex */)
                {
                    //TODO: Log the error (uncomment dex variable name and add a line here to write a log.
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                }
            }
            return(View(newRecipe));
        }