Beispiel #1
0
        public static void SeedDishesTest(UlloContext context, ApplicationUser firstUser)
        {
            context.Database.ExecuteSqlCommand("DELETE FROM Categories DBCC CHECKIDENT ('dbo.Categories', RESEED, 1)");
            context.Database.ExecuteSqlCommand("DELETE FROM Dishes DBCC CHECKIDENT ('dbo.Dishes', RESEED, 1)");
            context.Database.ExecuteSqlCommand("DELETE FROM Posts DBCC CHECKIDENT ('dbo.Posts', RESEED, 1)");
            context.Database.ExecuteSqlCommand("DELETE FROM Pictures DBCC CHECKIDENT ('dbo.Pictures', RESEED, 1)");
            context.Database.ExecuteSqlCommand("DELETE FROM Votes");

            string[] categoryNames = { "Piadine",                                  "Primi piatti in promo", "Primi piatti",                    "Secondi piatti caldi", "Secondi piatti di pesce caldi e freddi",
                                       "Secondi piatti freddi",                    "Insalatoni",            "Insalatoni fino a 5 ingredienti", "Contorni freddi",
                                       "Contorni caldi ordinabili entro le 11:00", "Desserts" };

            List <Category> categories = new List <Category>();

            foreach (string category in categoryNames)
            {
                categories.Add(new Category {
                    Name = category, Route = IdentityModels.getNameAsRoute(category)
                });
            }
            context.Categories.AddOrUpdate(categories.ToArray());
            context.SaveChanges();

            List <Dish> dishes = new List <Dish>();

            string[] dishNames = { "Penne con Sugo di Salsiccia e Panna", "Spaghetti con Pesto alla Genovese", "Spaghetti con Aglio, Olio e Peperoncino" };
            foreach (string dish in dishNames)
            {
                Dish d = new Dish {
                    Name = dish, Route = IdentityModels.getNameAsRoute(dish), Price = 3.20M, Created = DateTime.Now
                };
                d.User = firstUser;
                d.Categories.Add(categories[1]);
                d.Pictures.Add(new Picture {
                    Name      = dish,
                    Route     = String.Format("/Media/{0}.jpg", IdentityModels.getNameAsRoute(dish)),
                    Guid      = Guid.NewGuid(),
                    Created   = DateTime.Now,
                    AssetType = Picture.assetTypeEnum.Picture
                });
                dishes.Add(d);
            }
            context.Dishes.AddOrUpdate(dishes.ToArray());
            context.SaveChanges();

            for (int i = 1; i <= 100; i++)
            {
                // int r = randomizer.Next(dishes.Count);
                Dish randomDish = dishes.ElementAt(i % dishes.Count);

                Post p = new Post {
                    Created = DateTime.Now
                };
                p.User    = firstUser;
                p.Dish    = randomDish;
                p.Picture = randomDish.Pictures.ElementAt(0);
                context.Posts.AddOrUpdate(p);
            }
            context.SaveChanges();
        }
Beispiel #2
0
        public static ApplicationUser addUserWithRole(UlloContext context, string userName, string userPassword, string roleName)
        {
            var hasher = new PasswordHasher();

            var role = context.Roles.SingleOrDefault(r => r.Name == roleName);

            if (role == null)
            {
                role = new IdentityRole {
                    Name = roleName, Id = Guid.NewGuid().ToString()
                };
                context.Roles.AddOrUpdate(role);
                context.SaveChanges();
            }

            var user = context.Users.SingleOrDefault(u => u.UserName == userName);

            if (user == null)
            {
                user = new ApplicationUser {
                    UserName       = userName,
                    PasswordHash   = hasher.HashPassword(userPassword),
                    Email          = userName,
                    EmailConfirmed = true,
                    SecurityStamp  = Guid.NewGuid().ToString()
                };
                user.Roles.Add(new IdentityUserRole {
                    RoleId = role.Id, UserId = user.Id
                });

                context.Users.AddOrUpdate(user);
                context.SaveChanges();
            }

            return(user);
        }
Beispiel #3
0
        public static void SeedDishes(UlloContext context, ApplicationUser firstUser)
        {
            context.Database.ExecuteSqlCommand("DELETE FROM Categories DBCC CHECKIDENT ('dbo.Categories', RESEED, 1)");
            context.Database.ExecuteSqlCommand("DELETE FROM Dishes DBCC CHECKIDENT ('dbo.Dishes', RESEED, 1)");
            context.Database.ExecuteSqlCommand("DELETE FROM Posts DBCC CHECKIDENT ('dbo.Posts', RESEED, 1)");
            context.Database.ExecuteSqlCommand("DELETE FROM Pictures DBCC CHECKIDENT ('dbo.Pictures', RESEED, 1)");
            context.Database.ExecuteSqlCommand("DELETE FROM Votes");

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data. E.g.

            /** CATEGORIES **/
            List <Category> categories = new List <Category>();

            /*
             * Category[] categories = {
             *  new Category { Name = "Piadine", Route = Identity.getNameAsRoute("Piadine") },
             *  new Category { Name = "Primi piatti in promo" },
             *  new Category { Name = "Primi piatti" },
             *  new Category { Name = "Secondi piatti caldi" },
             *  new Category { Name = "Secondi piatti di pesce caldi e freddi" },
             *  new Category { Name = "Secondi piatti freddi" },
             *  new Category { Name = "Insalatoni" },
             *  new Category { Name = "Insalatoni fino a 5 ingredienti" },
             *  new Category { Name = "Contorni freddi" },
             *  new Category { Name = "Contorni caldi" },
             *  new Category { Name = "Contorni caldi ordinabili entro le 11:00" },
             *  new Category { Name = "Desserts" }
             * };
             * context.Categories.AddOrUpdate(categories);
             */
            string[] categoryNames = { "Piadine",                                  "Primi piatti in promo", "Primi piatti",                    "Secondi piatti caldi", "Secondi piatti di pesce caldi e freddi",
                                       "Secondi piatti freddi",                    "Insalatoni",            "Insalatoni fino a 5 ingredienti", "Contorni freddi",
                                       "Contorni caldi ordinabili entro le 11:00", "Desserts" };
            foreach (string category in categoryNames)
            {
                categories.Add(new Category {
                    Name = category, Route = IdentityModels.getNameAsRoute(category)
                });
            }
            context.Categories.AddOrUpdate(categories.ToArray());
            context.SaveChanges();

            /** DISHES **/

            /*
             * Dish[] dishes = {
             *  new Dish { Name = "Penne con Sugo di Salsiccia e Panna", Price = 3.20M },
             *  new Dish { Name = "Spaghetti con Pesto alla Genovese", Price = 3.20M },
             *  new Dish { Name = "Spaghetti con Aglio, Olio e Peperoncino", Price = 3.20M }
             * };
             * dishes[0].Categories.Add(categories[1]);
             * dishes[1].Categories.Add(categories[1]);
             * dishes[2].Categories.Add(categories[1]);
             * context.Dishes.AddOrUpdate(dishes);
             */
            string[] dishNames = { "Penne con Sugo di Salsiccia e Panna", "Spaghetti con Pesto alla Genovese", "Spaghetti con Aglio, Olio e Peperoncino" };
            foreach (string dish in dishNames)
            {
                Dish d = new Dish {
                    Name = dish, Route = IdentityModels.getNameAsRoute(dish), Price = 3.20M, Created = DateTime.Now
                };
                d.User = firstUser;
                d.Categories.Add(categories[1]);
                d.Pictures.Add(new Picture {
                    Name      = dish,
                    Route     = String.Format("/Media/{0}.jpg", IdentityModels.getNameAsRoute(dish)),
                    Guid      = Guid.NewGuid(),
                    Created   = DateTime.Now,
                    AssetType = Picture.assetTypeEnum.Picture
                });
                context.Dishes.AddOrUpdate(d);
            }
            context.SaveChanges();
        }