public List <RecipeMealType> MapToRecipeCuisines(RecipeRequest recipeRequest) { var recipeMealTypes = new List <RecipeMealType>(); foreach (var mealTypes in recipeRequest.MealType) { if (!String.IsNullOrWhiteSpace(mealTypes)) { var recipeMealType = new RecipeMealType(); { if (CheckMealType(mealTypes)) { var mealtype = GetMealTypeId(mealTypes); recipeMealType.MealType = mealtype; recipeMealType.MealTypeId = mealtype.MealTypeId; } else { var mapToMealType = MapToMealType(mealTypes); recipeMealType.MealType = mapToMealType; recipeMealType.MealTypeId = mapToMealType.MealTypeId; } } recipeMealTypes.Add(recipeMealType); } } return(recipeMealTypes); }
// work out if the RecipeMealType already exists, if it does then use it for the mapping table, else create it private static void addMealTypes(RecipeObj recipe) { using (var context = new WoolworthsDBDataContext()) { // clear the current mappings var maps = context.RecipeMealTypeMaps.Where(r => r.RecipeID == recipe.RecipeID); context.RecipeMealTypeMaps.DeleteAllOnSubmit(maps); context.SubmitChanges(); if (recipe.MealTypes == null) { return; } foreach (var mealType in recipe.MealTypes) { // create or retrieve the RecipeCuisine.ID value var master = context.RecipeMealTypes.SingleOrDefault(c => c.Description == mealType.MealType); var recipeMealTypeID = 0; if (master == null) { var toCreate = new RecipeMealType() { Description = mealType.MealType }; context.RecipeMealTypes.InsertOnSubmit(toCreate); context.SubmitChanges(); recipeMealTypeID = toCreate.ID; } else { recipeMealTypeID = master.ID; } // add to the mapping table var map = new RecipeMealTypeMap() { RecipeMealTypeID = recipeMealTypeID, RecipeID = recipe.RecipeID }; context.RecipeMealTypeMaps.InsertOnSubmit(map); context.SubmitChanges(); } } }
// work out if the RecipeMealType already exists, if it does then use it for the mapping table, else create it private static void addMealTypes(RecipeObj recipe) { using (var context = new WoolworthsDBDataContext()) { // clear the current mappings var maps = context.RecipeMealTypeMaps.Where(r => r.RecipeID == recipe.RecipeID); context.RecipeMealTypeMaps.DeleteAllOnSubmit(maps); context.SubmitChanges(); if (recipe.MealTypes == null) return; foreach (var mealType in recipe.MealTypes) { // create or retrieve the RecipeCuisine.ID value var master = context.RecipeMealTypes.SingleOrDefault(c => c.Description == mealType.MealType); var recipeMealTypeID = 0; if (master == null) { var toCreate = new RecipeMealType() { Description = mealType.MealType }; context.RecipeMealTypes.InsertOnSubmit(toCreate); context.SubmitChanges(); recipeMealTypeID = toCreate.ID; } else { recipeMealTypeID = master.ID; } // add to the mapping table var map = new RecipeMealTypeMap() { RecipeMealTypeID = recipeMealTypeID, RecipeID = recipe.RecipeID }; context.RecipeMealTypeMaps.InsertOnSubmit(map); context.SubmitChanges(); } } }