Пример #1
0
        public async Task <IActionResult> Create(Guid recipeId, string name, decimal quantity, string measurementValue)
        {
            var recipe = _context.Recipes.FirstOrDefault(r => r.Id == recipeId);

            if (recipe == null)
            {
                return(NotFound());
            }

            var ingredient = recipe.Ingredients.FirstOrDefault(i => i.Name == name && i.MeasurementValue == measurementValue);

            if (ingredient != null)
            {
                ingredient.Quantity += quantity;
            }
            else
            {
                recipe.Ingredients.Add(new Ingredient {
                    Name = name, Quantity = quantity, MeasurementValue = measurementValue
                });
            }

            await _context.SaveChanges();

            return(RedirectToAction("Personal", "Recipe", new { id = recipeId }));
        }
        public async Task <IActionResult> Create(Recipe recipe)
        {
            recipe.Id = Guid.NewGuid();
            _context.Recipes.Add(recipe);
            await _context.SaveChanges();

            return(RedirectToAction(nameof(Index)));
        }