/// <summary> /// adds a dish to the menu of a restaurant /// 'add all ingredients when dish is added'? -> dish can't be added if all ingredients it requires does not exist /// 'recipe can only have one portion of an ingredient' /// </summary> /// <param name="dish">dish to be added</param> /// <exception cref="ArgumentException">dish with a name already exists or when not all dish ingredients exist for a dish</exception> public void AddDish(Dish dish) { var allDishIngredientsExist = new Func <Dish, bool>(d => d.IngredientIds .Select(ingredientId => ingredientId) .All(ingredientId => _ingredientRepository.Exists(ingredientId))); if (!allDishIngredientsExist(dish)) { throw new ArgumentException($"not all dish ingredients exist in a {dish}"); } if (_dishRepository.ExistsByName(dish.Name)) { throw new ArgumentException($"dish with name '{dish.Name}' already exists"); } dish.IngredientIds = dish.IngredientIds.Distinct().ToList(); // 'recipe can only have one portion of an ingredient' _dishRepository.Add(dish); }
public async Task <IActionResult> Save(IngredientFormViewModel vm) { if (!ModelState.IsValid) { return(View("IngredientForm", vm)); } Ingredient ingredient = vm.ToIngredient(); if (vm.Id == null) { // add await _ingredientRepo.Create(ingredient); } else { // edit try { await _ingredientRepo.Update(ingredient); } catch (DbUpdateConcurrencyException) { if (!_ingredientRepo.Exists(ingredient.Id)) { return(NotFound()); } else { throw; } } } return(RedirectToAction(nameof(Index))); }
public bool AllIngredientsExist(IEnumerable <Ingredient> ingredients) { return(ingredients .All(ingredient => _ingredientRepository.Exists(ingredient.Id))); }