// clear the current list of ingredients for a given recipe and add the ones based on the file to ingest private static void addIngredients(RecipeObj recipe) { // clear the current list using (var context = new WoolworthsDBDataContext()) { var ingredToDel = context.RecipeIngredients.Where(r => r.RecipeID == recipe.RecipeID); context.RecipeIngredients.DeleteAllOnSubmit(ingredToDel); context.SubmitChanges(); } if (recipe.Ingredients == null) return; using (var context = new WoolworthsDBDataContext()) { foreach (var obj in recipe.Ingredients) { var ingred = new RecipeIngredient() { Description = obj.Ingredient, Number = obj.IngredientID, ProductArticle = obj.ProductID, RecipeID = recipe.RecipeID }; context.RecipeIngredients.InsertOnSubmit(ingred); } context.SubmitChanges(); } }
// clear the current list of methods for a given recipe and add the ones based on the file to ingest private static void addMethods(RecipeObj recipe) { // clear the current list using (var context = new WoolworthsDBDataContext()) { var methodToDel = context.RecipeMethods.Where(r => r.RecipeID == recipe.RecipeID); context.RecipeMethods.DeleteAllOnSubmit(methodToDel); context.SubmitChanges(); } if (recipe.Methods == null) return; using (var context = new WoolworthsDBDataContext()) { foreach (var obj in recipe.Methods) { var method = new RecipeMethod() { Description = obj.Step, Number = obj.StepID, RecipeID = recipe.RecipeID }; context.RecipeMethods.InsertOnSubmit(method); } context.SubmitChanges(); } }
// update a given recipe private void updateRecipe(RecipeObj recipe) { // go through the recipe metadata and create/update if necessary createRecipeRecord(recipe); // ingredients addIngredients(recipe); // methods addMethods(recipe); // cuisines addCuisines(recipe); // mealtypess addMealTypes(recipe); // facts addFacts(recipe); // divisions addDivisions(recipe); }
// work out if the cuisine already exists, if it does then use it for the mapping table, else create it private void addCuisines(RecipeObj recipe) { using (var context = new WoolworthsDBDataContext()) { // clear the current mappings var maps = context.RecipeCuisineMaps.Where(r => r.RecipeID == recipe.RecipeID); context.RecipeCuisineMaps.DeleteAllOnSubmit(maps); context.SubmitChanges(); if (recipe.Cuisines == null) return; foreach (var cuisine in recipe.Cuisines) { // create or retrieve the RecipeCuisine.ID value var master = context.RecipeCuisines.SingleOrDefault(c => c.Description == cuisine.Cuisine); var recipeCuisineID = 0; if (master == null) { var toCreate = new RecipeCuisine() { Description = cuisine.Cuisine }; context.RecipeCuisines.InsertOnSubmit(toCreate); context.SubmitChanges(); recipeCuisineID = toCreate.ID; } else { recipeCuisineID = master.ID; } // add to the mapping table var map = new RecipeCuisineMap() { RecipeCuisineID = recipeCuisineID, RecipeID = recipe.RecipeID }; context.RecipeCuisineMaps.InsertOnSubmit(map); context.SubmitChanges(); } } }
// adds or returns the actual Recipe record (id) private int createRecipeRecord(RecipeObj recipe) { using (var context = new WoolworthsDBDataContext()) { var existing = context.Recipes.SingleOrDefault(r => r.ID == recipe.RecipeID); // if the recipe already exists and the action isn't to update then just return the recipe identifier if (existing != null) //Always update, regardless of action (well, it should be "i" or "u" -- && recipe.Action.ToLower() == "u") { // if the recipe exists and the instruction is to update then updates existing.CookingTime = recipe.CookingTime; existing.Description = recipe.Description; existing.Recipe1 = recipe.Recipe; existing.ImageURL = downloadRemoteImageFile(recipe.ImageURL, ImgDir, recipe.Recipe); existing.OriginImageUrl = recipe.ImageURL; existing.PrepTime = recipe.PreparationTime; existing.Serves = recipe.Serves; existing.Tip = recipe.Tip; existing.Title = recipe.Title; existing.TotalTime = recipe.TotalTime; context.SubmitChanges(); return existing.ID; } else { // create the record regardless of whether it was an insert or update record var newRecipe = new WoolworthsDAL.Recipe { ID = recipe.RecipeID, CookingTime = recipe.CookingTime, Recipe1 = recipe.Recipe, Description = recipe.Description, ImageURL = downloadRemoteImageFile(recipe.ImageURL, ImgDir, recipe.Recipe), OriginImageUrl = recipe.ImageURL, PrepTime = recipe.PreparationTime, Serves = recipe.Serves, Tip = recipe.Tip, Title = recipe.Title, TotalTime = recipe.TotalTime }; context.Recipes.InsertOnSubmit(newRecipe); context.SubmitChanges(); return newRecipe.ID; } } }