コード例 #1
0
ファイル: DbInitializer.cs プロジェクト: rynoV/RecipEase
        private static async Task InitializeSupplies(RecipEaseContext context)
        {
            if (!context.Supplies.Any())
            {
                var supplies = new[]
                {
                    new Supplies()
                    {
                        UserId   = _testSupplierId,
                        Quantity = 5,
                        IngrName = Flour,
                        UnitName = Grams,
                    },
                    new Supplies()
                    {
                        UserId   = _testSupplierId,
                        Quantity = 100,
                        IngrName = Rice,
                        UnitName = Grams,
                    },
                    new Supplies()
                    {
                        UserId   = _testSupplierId,
                        Quantity = 10,
                        IngrName = GoatMilk,
                        UnitName = Cups,
                    }
                };

                await context.Supplies.AddRangeAsync(supplies);

                await context.SaveChangesAsync();
            }
        }
コード例 #2
0
ファイル: DbInitializer.cs プロジェクト: rynoV/RecipEase
        private static async Task InitializeRecipes(RecipEaseContext context)
        {
            if (!context.Recipe.Any())
            {
                var recipes = new[]
                {
                    new Recipe
                    {
                        Name        = "Mac N' Cheese",
                        AuthorId    = _testCustomerId,
                        Calories    = 1,
                        Carbs       = 2,
                        Cholesterol = 3,
                        Fat         = 4,
                        Protein     = 5,
                        Sodium      = 6,
                        Steps       = "The steps to make the recipe..."
                    },
                    new Recipe
                    {
                        Name        = "Pork Cutlets",
                        AuthorId    = _testCustomerId,
                        Calories    = 1,
                        Carbs       = 2,
                        Cholesterol = 3,
                        Fat         = 4,
                        Protein     = 5,
                        Sodium      = 6,
                        Steps       = "The steps to make the recipe..."
                    },
                    new Recipe
                    {
                        Name        = "Sushi",
                        AuthorId    = _testCustomerId2,
                        Calories    = 1,
                        Carbs       = 2,
                        Cholesterol = 3,
                        Fat         = 4,
                        Protein     = 5,
                        Sodium      = 6,
                        Steps       = "The steps to make the recipe..."
                    },
                };

                await context.Recipe.AddRangeAsync(recipes);

                await context.SaveChangesAsync();
            }

            var allRecipes = await context.Recipe.ToListAsync();

            if (allRecipes.Count == 3)
            {
                _recipe0Id = allRecipes[0].Id;
                _recipe1Id = allRecipes[1].Id;
                _recipe2Id = allRecipes[2].Id;
            }
        }
コード例 #3
0
ファイル: DbInitializer.cs プロジェクト: rynoV/RecipEase
        private static async Task InitializeRecipeCollections(RecipEaseContext context)
        {
            if (!context.RecipeCollection.Any())
            {
                var recipeCollections = new[]
                {
                    new RecipeCollection
                    {
                        UserId      = _testCustomerId,
                        Title       = _recColl1Title,
                        Description = "All my favourite breakfast recipes",
                        Visibility  = Visibility.Public
                    },
                    new RecipeCollection
                    {
                        UserId      = _testCustomerId,
                        Title       = _recColl2Title,
                        Description = "All my favourite seafood recipes",
                        Visibility  = Visibility.Public
                    },
                };

                var recipeInCollections = new[]
                {
                    new RecipeInCollection
                    {
                        RecipeId         = _recipe0Id,
                        CollectionTitle  = _recColl1Title,
                        CollectionUserId = _testCustomerId,
                    },
                    new RecipeInCollection
                    {
                        RecipeId         = _recipe1Id,
                        CollectionTitle  = _recColl1Title,
                        CollectionUserId = _testCustomerId,
                    },
                    new RecipeInCollection
                    {
                        RecipeId         = _recipe1Id,
                        CollectionTitle  = _recColl2Title,
                        CollectionUserId = _testCustomerId,
                    },
                    new RecipeInCollection
                    {
                        RecipeId         = _recipe2Id,
                        CollectionTitle  = _recColl2Title,
                        CollectionUserId = _testCustomerId,
                    },
                };

                await context.RecipeCollection.AddRangeAsync(recipeCollections);

                await context.RecipeInCollection.AddRangeAsync(recipeInCollections);

                await context.SaveChangesAsync();
            }
        }
コード例 #4
0
ファイル: DbInitializer.cs プロジェクト: rynoV/RecipEase
        private static async Task InitializeUses(RecipEaseContext context)
        {
            if (!context.Uses.Any())
            {
                var uses = new[]
                {
                    new Uses
                    {
                        RecipeId = _recipe0Id,
                        IngrName = Flour,
                        UnitName = Grams,
                        Quantity = 5
                    },
                    new Uses
                    {
                        RecipeId = _recipe1Id,
                        IngrName = Flour,
                        UnitName = Grams,
                        Quantity = 100
                    },
                    new Uses
                    {
                        RecipeId = _recipe2Id,
                        IngrName = Flour,
                        UnitName = Grams,
                        Quantity = 6
                    },
                    new Uses
                    {
                        RecipeId = _recipe0Id,
                        IngrName = GoatMilk,
                        UnitName = Cups,
                        Quantity = 10
                    },
                    new Uses
                    {
                        RecipeId = _recipe1Id,
                        IngrName = Rice,
                        UnitName = Cups,
                        Quantity = 4
                    },
                    new Uses
                    {
                        RecipeId = _recipe2Id,
                        IngrName = Rice,
                        UnitName = Grams,
                        Quantity = 200
                    },
                };

                await context.Uses.AddRangeAsync(uses);

                await context.SaveChangesAsync();
            }
        }
コード例 #5
0
ファイル: DbInitializer.cs プロジェクト: rynoV/RecipEase
        public static async Task Initialize(RecipEaseContext context, UserManager <User> userManager,
                                            RoleManager <IdentityRole> roleManager)
        {
            await context.Database.EnsureCreatedAsync();

            await InitializeRoles(roleManager);
            await InitializeUsers(context, userManager);

            await InitializeIngredients(context);
            await InitializeUnits(context);
            await InitializeSupplies(context);
            await InitializeShoppingList(context);

            await InitializeRecipes(context);
            await InitializeRecipeRatings(context);
            await InitializeRecipeCollections(context);
            await InitializeUses(context);
            await InitializeRecipeCategories(context);

            await context.SaveChangesAsync();
        }
コード例 #6
0
ファイル: DbInitializer.cs プロジェクト: rynoV/RecipEase
        private static async Task InitializeUnits(RecipEaseContext context)
        {
            if (!context.Unit.Any())
            {
                var units = new[]
                {
                    new Unit()
                    {
                        Name     = Grams,
                        Symbol   = "g",
                        UnitType = UnitType.Mass
                    },
                    new Unit()
                    {
                        Name     = Cups,
                        Symbol   = "cups",
                        UnitType = UnitType.Volume
                    },
                };

                await context.Unit.AddRangeAsync(units);
            }

            if (!context.UnitConversion.Any())
            {
                var conversions = new[]
                {
                    new UnitConversion {
                        ConvertsToUnitName = Grams, ConvertsFromUnitName = Cups, Ratio = 2.1
                    },
                };
                await context.UnitConversion.AddRangeAsync(conversions);
            }

            await context.SaveChangesAsync();
        }
コード例 #7
0
        public static async Task <(User user, IdentityResult result)> CreateUser(UserManager <User> userManager,
                                                                                 RecipEaseContext context,
                                                                                 AccountType accountType, string email, string password, Supplier supplierInfo = null,
                                                                                 Customer customerInfo = null)
        {
            var user = new User {
                UserName = email, Email = email
            };
            var result = await userManager.CreateAsync(user, password);

            if (!result.Succeeded)
            {
                return(user, result);
            }
            switch (accountType)
            {
            case AccountType.Customer:
                Customer customer;
                if (customerInfo != null)
                {
                    customer        = customerInfo;
                    customer.UserId = user.Id;
                }
                else
                {
                    customer = new Customer {
                        UserId = user.Id
                    };
                }
                await context.Customer.AddAsync(customer);

                await userManager.AddToRoleAsync(user, Role.Customer.ToString());

                await context.SaveChangesAsync();

                break;

            case AccountType.Supplier:
                Supplier supplier;
                if (supplierInfo != null)
                {
                    supplier        = supplierInfo;
                    supplier.UserId = user.Id;
                }
                else
                {
                    supplier = new Supplier {
                        UserId = user.Id
                    };
                }
                await context.Supplier.AddAsync(supplier);

                await userManager.AddToRoleAsync(user, Role.Supplier.ToString());

                await context.SaveChangesAsync();

                break;

            default:
                throw new Exception("Account type not provided.");
            }

            return(user, result);
        }
コード例 #8
0
ファイル: DbInitializer.cs プロジェクト: rynoV/RecipEase
        private static async Task InitializeShoppingList(RecipEaseContext context)
        {
            if (!context.ShoppingList.Any())
            {
                var shoppingLists = new[]
                {
                    new ShoppingList
                    {
                        Name       = "My Shopping List",
                        LastUpdate = DateTime.Now,
                        UserId     = _testCustomerId
                    },
                    new ShoppingList
                    {
                        Name       = "My Shopping List",
                        LastUpdate = DateTime.Now,
                        UserId     = _testCustomerId2
                    },
                    new ShoppingList
                    {
                        Name       = "My Shopping List",
                        LastUpdate = DateTime.Now,
                        UserId     = _testCustomerId3
                    },
                };
                await context.ShoppingList.AddRangeAsync(shoppingLists);
            }

            if (!context.IngrInShoppingList.Any())
            {
                var ingrInShoppingLists = new[]
                {
                    new IngrInShoppingList()
                    {
                        Quantity = 2,
                        IngrName = Flour,
                        UserId   = _testCustomerId,
                        UnitName = Grams
                    },
                    new IngrInShoppingList()
                    {
                        Quantity = 3,
                        IngrName = GoatMilk,
                        UserId   = _testCustomerId,
                        UnitName = Cups
                    },
                    new IngrInShoppingList()
                    {
                        Quantity = 100,
                        IngrName = Flour,
                        UserId   = _testCustomerId2,
                        UnitName = Grams
                    },
                    new IngrInShoppingList()
                    {
                        Quantity = 3,
                        IngrName = Rice,
                        UserId   = _testCustomerId2,
                        UnitName = Cups
                    },
                    new IngrInShoppingList()
                    {
                        Quantity = 5,
                        IngrName = Flour,
                        UserId   = _testCustomerId3,
                        UnitName = Cups
                    },
                };
                await context.IngrInShoppingList.AddRangeAsync(ingrInShoppingLists);
            }

            await context.SaveChangesAsync();
        }
コード例 #9
0
ファイル: DbInitializer.cs プロジェクト: rynoV/RecipEase
        private static async Task InitializeRecipeCategories(RecipEaseContext context)
        {
            var breakfast = "Breakfast";
            var lunch     = "Lunch";
            var dinner    = "Dinner";

            if (!context.RecipeCategory.Any())
            {
                var categories = new[]
                {
                    new RecipeCategory
                    {
                        Name = breakfast,
                        AverageCaloriesCatg = 500,
                        TotalInCatg         = 1,
                    },
                    new RecipeCategory
                    {
                        Name = lunch,
                        AverageCaloriesCatg = 600,
                        TotalInCatg         = 1
                    },
                    new RecipeCategory
                    {
                        Name = dinner,
                        AverageCaloriesCatg = 700,
                        TotalInCatg         = 1,
                    },
                };

                await context.RecipeCategory.AddRangeAsync(categories);

                await context.SaveChangesAsync();
            }

            if (!context.RecipeInCategory.Any())
            {
                var recipeInCategories = new[]
                {
                    new RecipeInCategory
                    {
                        RecipeId     = _recipe0Id,
                        CategoryName = breakfast
                    },
                    new RecipeInCategory
                    {
                        RecipeId     = _recipe1Id,
                        CategoryName = lunch
                    },
                    new RecipeInCategory
                    {
                        RecipeId     = _recipe2Id,
                        CategoryName = dinner
                    },
                };

                await context.RecipeInCategory.AddRangeAsync(recipeInCategories);

                await context.SaveChangesAsync();
            }
        }