public static void SaveIngredients(Ingredient ing, string UserID) { pantrypartyEntities ORM = new pantrypartyEntities(); UserIngredient NewUserIngredient = new UserIngredient(); NewUserIngredient.UserID = UserID; NewUserIngredient.IngredientID = ing.Name; // check for distinct values before arbitrarily saving to DB if (ORM.Ingredients.Find(ing.Name) == null) { ORM.Ingredients.Add(ing); ORM.SaveChanges(); } // check for distinct values before arbitrarily saving to DB List <UserIngredient> CheckList = ORM.AspNetUsers.Find(UserID).UserIngredients.Where(x => x.IngredientID == ing.Name).ToList(); if (CheckList.Count == 0) { ORM.AspNetUsers.Find(UserID).UserIngredients.Add(NewUserIngredient); ORM.SaveChanges(); } }
// Checks if the ingredients for each returned recipe are in the DB // If not, adds them and the Recipe-Ingredient relationship public static void SaveNewRecipeIngredient(JObject IngArray, Recipe ThisRecipe) { pantrypartyEntities ORM = new pantrypartyEntities(); if (ORM.Recipes.Find(ThisRecipe.ID) == null) { ORM.Recipes.Add(ThisRecipe); ORM.SaveChanges(); } for (int i = 0; i < IngArray["extendedIngredients"].Count(); i++) { if (ORM.Ingredients.Find(IngArray["extendedIngredients"][i]["name"].ToString()) == null) { Ingredient newIngredient = new Ingredient { Name = IngArray["extendedIngredients"][i]["name"].ToString() }; ORM.Ingredients.Add(newIngredient); ORM.SaveChanges(); } RecipeIngredient ObjToCheck = new RecipeIngredient(); ObjToCheck.RecipeID = ThisRecipe.ID; ObjToCheck.IngredientID = IngArray["extendedIngredients"][i]["name"].ToString(); if (ORM.RecipeIngredients.Where(x => x.RecipeID == ObjToCheck.RecipeID).ToList().Count == 0) { ORM.Recipes.Find(ThisRecipe.ID).RecipeIngredients.Add(ObjToCheck); ORM.SaveChanges(); } } }
// Checks if Recipe exists in DB, if not, adds to DB, and adds User->Recipe relationship public static void SaveRecipes(Recipe ThisRecipe, string UserID) { pantrypartyEntities ORM = new pantrypartyEntities(); AspNetUser CurrentUser = ORM.AspNetUsers.Find(UserID); UserRecipe ToAdd = new UserRecipe(); ToAdd.UserID = UserID; ToAdd.RecipeID = ThisRecipe.ID; if (ORM.Recipes.Find(ThisRecipe.ID) == null) { ORM.Recipes.Add(ThisRecipe); ORM.SaveChanges(); } if (!ORM.UserRecipes.Where(x => x.UserID == UserID).Distinct().ToList().Contains(ToAdd)) { ORM.UserRecipes.Add(ToAdd); ORM.SaveChanges(); } }