public int Insert(Ingredient ingredient) { if (this.cookbookDbContext.ingredients.Any(i => i.Name == ingredient.Name)) { throw new RecordAlreadyExistException("Record already exist."); } DAL.Ingredient newIngredient = new DAL.Ingredient { Name = ingredient.Name }; this.cookbookDbContext.ingredients.Add(newIngredient); return(newIngredient.IngredientId); }
public Ingredient GetIngredientByName(string ingredientName) { DAL.Ingredient ingredient = this.cookbookDbContext.ingredients .FirstOrDefault(i => i.Name == ingredientName); if (ingredient is null) { throw new RecordNotFoundException($"No record found."); } return(new Ingredient { IngredientId = ingredient.IngredientId, Name = ingredient.Name }); }
public void RelateIngredientsToRecipe(IEnumerable <Ingredient> ingredients, int recipeId) { foreach (var ingredient in ingredients) { DAL.Ingredient retrievedIngredient = this.cookbookDbContext.ingredients.FirstOrDefault(i => i.Name == ingredient.Name); if (retrievedIngredient is null) { throw new RecordNotFoundException("Record not found."); } this.cookbookDbContext.recipeIngredients.Add(new DAL.RecipeIngredient { IngredientId = retrievedIngredient.IngredientId, RecipeId = recipeId }); } }
public void AddIngredient(int preparedRecipeId, string ingredientName) { DAL.Ingredient retrievedIngredient = this.cookbookDbContext.ingredients.FirstOrDefault(i => i.Name == ingredientName); DAL.PreparedRecipe retrievedPreparedRecipe = this.cookbookDbContext.preparedRecipes.FirstOrDefault(pr => pr.PreparedRecipeId == preparedRecipeId); if (!this.cookbookDbContext.recipeIngredients.Any(ri => ri.IngredientId == retrievedIngredient.IngredientId && ri.Recipe.RecipeId == retrievedPreparedRecipe.RecipeId)) { throw new RecordNotFoundException("Record not found."); } this.cookbookDbContext.preparedRecipeIngredients.Add(new DAL.PreparedRecipeIngredient { IngredientId = retrievedIngredient.IngredientId, PreparedRecipeId = preparedRecipeId }); }