/// <summary> /// Deletes recipe /// </summary> /// <param name="recipeID">the recipe that is being deleted</param> public void DeleteRecipe(int recipeID) { try { using (CakeRecipesDBEntities context = new CakeRecipesDBEntities()) { for (int i = 0; i < GetAllRecipes().Count; i++) { if (GetAllRecipes().ToList()[i].RecipeID == recipeID) { // Remove all recipe ingredients before the recipe int selectedRecipeIngrediantAmountCount = GetAllSelectedRecipeIngrediantAmount(recipeID).Count; for (int j = 0; j < selectedRecipeIngrediantAmountCount; j++) { tblIngredientAmount ingAmountToDelete = (from ss in context.tblIngredientAmounts where ss.RecipeID == recipeID select ss).First(); context.tblIngredientAmounts.Remove(ingAmountToDelete); context.SaveChanges(); } tblRecipe recipeToDelete = (from r in context.tblRecipes where r.RecipeID == recipeID select r).First(); context.tblRecipes.Remove(recipeToDelete); context.SaveChanges(); break; } } } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); } }
/// <summary> /// Opens the Add ingredient amount to recipe window /// </summary> /// <param name="addIngrediwntOpen">Window that we open</param> /// <param name="ingredientEdit">ingredient that we are showing</param> /// <param name="recipeIDEdit">recipe that we are showing</param> public AddIngredientToRecipeViewModel(AddIngredientAmountToRecipe addIngredientAmountOpen, tblIngredient ingredientEdit, int recipeIDEdit) { ItemAmount = new tblIngredientAmount(); addIngredientAmountToRecipe = addIngredientAmountOpen; IngredientList = ingrediantsData.GetAllIngredients().ToList(); Ingredient = ingredientEdit; RecipeID = recipeIDEdit; IngrediantAmountList = recipeData.GetAllSelectedRecipeIngrediantAmount(recipeIDEdit).ToList(); }
/// <summary> /// Checks if the ingredient was previoulsy added /// </summary> /// <param name="ingredientAmount">Ingredient we are checking</param> /// <returns>The id of the ingredient that was added or not</returns> public int IsIngredientAdded(tblIngredientAmount ingredientAmount) { for (int i = 0; i < GetAllSelectedRecipeIngrediantAmount(ingredientAmount.RecipeID).Count; i++) { if (ingredientAmount.IngredientID == GetAllSelectedRecipeIngrediantAmount(ingredientAmount.RecipeID)[i].IngredientID && ingredientAmount.RecipeID == GetAllSelectedRecipeIngrediantAmount(ingredientAmount.RecipeID)[i].RecipeID) { return(GetAllSelectedRecipeIngrediantAmount(ingredientAmount.RecipeID)[i].IngredientAmountID); } } return(0); }
/// <summary> /// Adds ingredients to recipes to the database /// </summary> /// <param name="ingredientAmount">The ingredient Amount ID we are adding or editing</param> /// <returns>The new or edited ingredient amount</returns> public tblIngredientAmount AddIngredientAmount(tblIngredientAmount ingredientAmount) { // Delete ingredient if it was added earlier if (IsIngredientAdded(ingredientAmount) != 0) { DeleteIngredientAmount(IsIngredientAdded(ingredientAmount)); } try { using (CakeRecipesDBEntities context = new CakeRecipesDBEntities()) { if (ingredientAmount.IngredientAmountID == 0) { tblIngredientAmount newIngredientAmount = new tblIngredientAmount { RecipeID = ingredientAmount.RecipeID, IngredientID = ingredientAmount.IngredientID, Amount = ingredientAmount.Amount }; context.tblIngredientAmounts.Add(newIngredientAmount); context.SaveChanges(); ingredientAmount.IngredientAmountID = newIngredientAmount.IngredientAmountID; return(ingredientAmount); } else { tblIngredientAmount ingredientAmountEdit = (from ss in context.tblIngredientAmounts where ss.IngredientAmountID == ingredientAmount.IngredientAmountID select ss).First(); ingredientAmountEdit.RecipeID = ingredientAmount.RecipeID; ingredientAmountEdit.IngredientID = ingredientAmount.IngredientID; ingredientAmountEdit.Amount = ingredientAmount.Amount; context.SaveChanges(); return(ingredientAmount); } } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
/// <summary> /// Deletes ingredient amount /// </summary> /// <param name="ingredientAmountID">the ingredientAmount that is being deleted</param> public void DeleteIngredientAmount(int ingredientAmountID) { try { using (CakeRecipesDBEntities context = new CakeRecipesDBEntities()) { for (int i = 0; i < GetAllRecipeIngrediant().Count; i++) { if (GetAllRecipeIngrediant().ToList()[i].IngredientAmountID == ingredientAmountID) { tblIngredientAmount ingAmountToDelete = (from ss in context.tblIngredientAmounts where ss.IngredientAmountID == ingredientAmountID select ss).First(); context.tblIngredientAmounts.Remove(ingAmountToDelete); context.SaveChanges(); break; } } } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); } }