public RecipeResult CreateRecipe(AuthIdentity identity, Recipe recipe) { using (var session = GetSession()) { using (var transaction = session.BeginTransaction()) { // Create Recipe var dbRecipe = new Models.Recipes { Title = recipe.Title, Description = recipe.Description, CookTime = recipe.CookTime, PrepTime = recipe.PrepTime, Credit = recipe.Credit, CreditUrl = recipe.CreditUrl, DateEntered = recipe.DateEntered, ImageUrl = recipe.ImageUrl, Rating = recipe.AvgRating, ServingSize = recipe.ServingSize, Steps = recipe.Method }; session.Save(dbRecipe); // Create Ingredients short displayOrder = 0; recipe.Ingredients.ForEach(i => { var dbIngredient = new RecipeIngredients { Recipe = dbRecipe, Ingredient = Models.Ingredients.FromId(i.Ingredient.Id), IngredientForm = (i.Form != null ? IngredientForms.FromId(i.Form.FormId) : null), Qty = (i.Amount != null ? (float?) i.Amount.SizeHigh : null), QtyLow = (i.Amount != null ? (float?) i.Amount.SizeLow : null), Unit = (i.Amount != null ? i.Amount.Unit : Units.Unit), Section = i.Section, DisplayOrder = ++displayOrder }; session.Save(dbIngredient); }); // Create RecipeMetadata var dbMetadata = new RecipeMetadata { Recipe = dbRecipe, DietGlutenFree = recipe.Tags.HasTag(RecipeTag.GlutenFree), DietNoAnimals = recipe.Tags.HasTag(RecipeTag.NoAnimals), DietNomeat = recipe.Tags.HasTag(RecipeTag.NoMeat), DietNoPork = recipe.Tags.HasTag(RecipeTag.NoPork), DietNoRedMeat = recipe.Tags.HasTag(RecipeTag.NoRedMeat), MealBreakfast = recipe.Tags.HasTag(RecipeTag.Breakfast), MealDessert = recipe.Tags.HasTag(RecipeTag.Dessert), MealDinner = recipe.Tags.HasTag(RecipeTag.Dinner), MealLunch = recipe.Tags.HasTag(RecipeTag.Lunch), NutritionLowCalorie = recipe.Tags.HasTag(RecipeTag.LowCalorie), NutritionLowCarb = recipe.Tags.HasTag(RecipeTag.LowCarb), NutritionLowFat = recipe.Tags.HasTag(RecipeTag.LowFat), NutritionLowSodium = recipe.Tags.HasTag(RecipeTag.LowSodium), NutritionLowSugar = recipe.Tags.HasTag(RecipeTag.LowSugar), SkillCommon = recipe.Tags.HasTag(RecipeTag.Common), SkillEasy = recipe.Tags.HasTag(RecipeTag.Easy), SkillQuick = recipe.Tags.HasTag(RecipeTag.Quick) }; session.Save(dbMetadata); transaction.Commit(); return new RecipeResult { RecipeCreated = true, NewRecipeId = dbRecipe.RecipeId }; } } }
private static void CreateIngredients(Recipe recipe, Recipes databaseRecipe, ISession session) { short displayOrder = 0; recipe.Ingredients.ForEach( i => { var databaseIngredient = new RecipeIngredients { Recipe = databaseRecipe, Ingredient = Ingredients.FromId(i.Ingredient.Id), IngredientForm = i.Form != null ? IngredientForms.FromId(i.Form.FormId) : null, Qty = i.Amount != null ? (float?)i.Amount.SizeHigh : null, QtyLow = i.Amount != null ? i.Amount.SizeLow : null, Unit = i.Amount != null ? i.Amount.Unit : Units.Unit, Section = i.Section, DisplayOrder = ++displayOrder }; session.Save(databaseIngredient); }); }