Ejemplo n.º 1
0
        public static void Initialize(RecipeBookContext context)
        {
            context.Database.EnsureCreated();

            // Look for any Recipes.
            if (context.Recipes.Any())
            {
                return;   // DB has been seeded
            }

            var recipes = new Recipe[]
            {
                new Recipe {
                    Name = "Tom Yom Goong", Description = "A Thai seafood soup.", RecipeCategory = RecipeClassification.Seafood
                },
            };

            foreach (Recipe s in recipes)
            {
                context.Recipes.Add(s);
            }

            var ingredients = new Ingredient[]
            {
                new Ingredient {
                    Name = "Thai Chili", PortionName = "Chili"
                },
                new Ingredient {
                    Name = "Galanggal", PortionName = "Slices"
                },
                new Ingredient {
                    Name = "Oyster Mushrooms", PortionName = "Cups"
                }
            };

            foreach (Ingredient i in ingredients)
            {
                context.Ingredients.Add(i);
            }

            for (int i = 0; i < ingredients.Count(); i++)
            {
                context.IngredientPortions.Add(new IngredientPortion {
                    RecipeId = recipes.First().Id, IngredientId = ingredients[i].Id, IngredientPortionSize = i, Recipe = recipes.First(), Ingredient = ingredients[i]
                });
            }
            context.SaveChanges();
        }
Ejemplo n.º 2
0
 public DeleteModel(RecipeBook.Data.RecipeBookContext context)
 {
     _context = context;
 }
Ejemplo n.º 3
0
 public EditModel(RecipeBook.Data.RecipeBookContext context)
 {
     _context = context;
 }
Ejemplo n.º 4
0
 public DetailsModel(RecipeBook.Data.RecipeBookContext context)
 {
     _context = context;
 }
Ejemplo n.º 5
0
        public static void Seed(RecipeBookContext recipeBookContext, ILoggerFactory loggerFactory,
                                IWebHostEnvironment hostingEnv)
        {
            var logger = loggerFactory.CreateLogger(typeof(RecipeBookContextSeed));

            try
            {
                if (recipeBookContext.Authors.Any())
                {
                    logger.LogInformation("The database has already been seeded");
                    return;
                }

                var author = new Author {
                    IdentityUsername = AuthorizationConstants.DEFAULT_USER_USERNAME
                };
                recipeBookContext.Authors.Add(author);
                recipeBookContext.SaveChanges();

                var recipe = new Recipe
                {
                    Name        = "Sesame seed burger buns",
                    Description = "Homemade Hamburger Buns",
                    DatePosted  = DateTime.Today,
                    // TODO: Implement photo saving
                    Photo    = "",
                    Rating   = 4.5f,
                    Course   = Course.Mains,
                    Author   = author,
                    AuthorId = author.Id
                };
                recipeBookContext.Recipes.Add(recipe);
                recipeBookContext.SaveChanges();

                var instructions = GetSeedDataFromJson <Instruction>(hostingEnv, path: "Data/SeedData/recipeInstructions.json",
                                                                     logger);
                foreach (var instruction in instructions)
                {
                    instruction.RecipeId = recipe.Id;
                }
                recipeBookContext.Instructions.AddRange(instructions);
                recipeBookContext.SaveChanges();

                var ingredients = GetSeedDataFromJson <Ingredient>(hostingEnv, path: "Data/SeedData/recipeIngredients.json",
                                                                   logger);
                foreach (var ingredient in ingredients)
                {
                    ingredient.RecipeId = recipe.Id;
                }
                recipeBookContext.Ingredients.AddRange(ingredients);
                recipeBookContext.SaveChanges();

                var comments = GetSeedDataFromJson <Comment>(hostingEnv, path: "Data/SeedData/recipeComments.json",
                                                             logger);
                foreach (var comment in comments)
                {
                    comment.RecipeId   = recipe.Id;
                    comment.AuthorId   = author.Id;
                    comment.DatePosted = DateTime.Today;
                }
                recipeBookContext.Comments.AddRange(comments);
                recipeBookContext.SaveChanges();

                var categories = new List <Category>()
                {
                    new Category {
                        CategoryName = "Healthy"
                    },
                    new Category {
                        CategoryName = "Cakes and baking"
                    },
                    new Category {
                        CategoryName = "Cheap and healthy"
                    }
                };
                recipeBookContext.Categories.AddRange(categories);
                recipeBookContext.SaveChanges();

                var categoryRecipe = new CategoryRecipe()
                {
                    CategoryId = categories.Single(category => category.CategoryName == "Cakes and baking").Id,
                    RecipeId   = recipe.Id
                };
                recipeBookContext.CategoryRecipe.AddRange(categoryRecipe);
                recipeBookContext.SaveChanges();
            }
            catch (Exception exception)
            {
                logger.LogError($"Failed to seed RecipeBookDb {exception.Message}");
                throw;
            }
        }
Ejemplo n.º 6
0
 public IndexModel(RecipeBook.Data.RecipeBookContext context)
 {
     _context = context;
 }