Example #1
0
 /// <summary>
 /// This method invokes method for adding ingredient to recipe.
 /// </summary>
 public void AddIngredientExecute()
 {
     if (String.IsNullOrEmpty(Ingredient.IngredientName) || String.IsNullOrEmpty(Ingredient.Quantity.ToString()) || Ingredient.Quantity == 0)
     {
         MessageBox.Show("Please fill all fields.", "Notification");
     }
     else if (IngredientList != null && (IngredientList.Where(x => x.IngredientName == Ingredient.IngredientName).Any() ||
                                         AddedIngredientList.Where(x => x.IngredientName == Ingredient.IngredientName).Any()))
     {
         MessageBox.Show("You already added this ingredient.", "Notification");
     }
     else
     {
         try
         {
             ingredients.AddIngredient(Ingredient, out int id);
             Ingredient.IngredientId = id;
             //add to collection of added ingredients
             AddedIngredientList.Add(Ingredient);
             //remove from collection of deleted ingredients
             DeletedIngredientList.Remove(Ingredient);
             //invokes method to update a list of ingredients
             IngredientList      = ingredients.GetRecipeIngredients(Recipe);
             Ingredient          = new vwIngredient();
             Ingredient.RecipeId = Recipe.RecipeId;
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.ToString());
         }
     }
 }
 public AddIngredientsViewModel(AddIngredientsView addIngredientsOpen, vwRecipe recipeCreated)
 {
     addIngredients      = addIngredientsOpen;
     recipe              = recipeCreated;
     Ingredient          = new vwIngredient();
     ingredient.RecipeId = recipeCreated.RecipeId;
 }
Example #3
0
 /// <summary>
 /// This method add ingredient of recipe.
 /// </summary>
 /// <param name="ingredient">Ingredient to be added.</param>
 /// <returns>True if created, false if not.</returns>
 public bool AddIngredient(vwIngredient ingredientToAdd, out int ingredientId)
 {
     try
     {
         using (RecipesDBEntities context = new RecipesDBEntities())
         {
             tblIngredient ingredient = new tblIngredient
             {
                 IngredientName = ingredientToAdd.IngredientName,
                 Quantity       = ingredientToAdd.Quantity,
                 RecipeId       = ingredientToAdd.RecipeId
             };
             context.tblIngredients.Add(ingredient);
             context.SaveChanges();
             ingredientId = ingredient.IngredientId;
             return(true);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         ingredientId = 0;
         return(false);
     }
 }
Example #4
0
 public EditIngredientsViewModel(EditIngredientsView editIngredientsOpen, vwRecipe recipeCreated)
 {
     editIngredients     = editIngredientsOpen;
     Recipe              = recipeCreated;
     Ingredient          = new vwIngredient();
     ingredient.RecipeId = recipeCreated.RecipeId;
     IngredientList      = ingredients.GetRecipeIngredients(Recipe);
 }
Example #5
0
 /// <summary>
 /// This method deletes ingredient.
 /// </summary>
 /// <param name="ingredient">Ingredient to be deleted.</param>
 public bool DeleteIngredient(vwIngredient ingredient)
 {
     try
     {
         using (RecipesDBEntities context = new RecipesDBEntities())
         {
             tblIngredient ingredientToDelete = context.tblIngredients.Where(x => x.IngredientId == ingredient.IngredientId).FirstOrDefault();
             context.tblIngredients.Remove(ingredientToDelete);
             context.SaveChanges();
             return(true);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(false);
     }
 }
 public void RemoveIngredientExecute()
 {
     try
     {
         if (Ingredient != null)
         {
             //invokes method to delete ingredient
             ingredients.DeleteIngredient(Ingredient);
             //invokes method to update list of ingredients
             IngredientList      = ingredients.GetRecipeIngredients(Recipe);
             Ingredient          = new vwIngredient();
             Ingredient.RecipeId = Recipe.RecipeId;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
 }
Example #7
0
 /// <summary>
 /// This method edits data of ingredient.
 /// </summary>
 /// <param name="ingredientToEdit">Ingredient to be edited.</param>
 /// <returns>True if edited, false if not.</returns>
 public bool EditIngredient(vwIngredient ingredientToEdit)
 {
     try
     {
         using (RecipesDBEntities context = new RecipesDBEntities())
         {
             tblIngredient ingredient = context.tblIngredients.Where(x => x.IngredientId == ingredientToEdit.IngredientId).FirstOrDefault();
             ingredient.IngredientName = ingredientToEdit.IngredientName;
             ingredient.Quantity       = ingredientToEdit.Quantity;
             context.SaveChanges();
             return(true);
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(false);
     }
 }