Exemple #1
0
 private static void SeedRoles(YouTubePlaylistsDbContext context)
 {
     if (!context.AppRoles.Any())
     {
         context.AppRoles.Add(new AppRole("Administrator"));
         context.AppRoles.Add(new AppRole("User"));
         context.SaveChanges();
     }
 }
Exemple #2
0
 public static void Seed(YouTubePlaylistsDbContext context)
 {
     SeedRoles(context);
     context = new YouTubePlaylistsDbContext();
     SeedUsers(context);
     context = new YouTubePlaylistsDbContext();
     SeedCategories(context);
     context = new YouTubePlaylistsDbContext();
     SeedPlaylists(context);
     context = new YouTubePlaylistsDbContext();
 }
Exemple #3
0
        private static void SeedPlaylists(YouTubePlaylistsDbContext context)
        {
            if (context.Playlists.Any())
            {
                return;
            }

            var authors         = context.Users.ToList();
            var categoriesCount = context.Categories.Count();
            var random          = new Random();

            for (int i = 1; i <= 10; i++)
            {
                var pl = new Playlist
                {
                    Creator     = authors[random.Next(0, authors.Count)],
                    CategoryId  = random.Next(1, categoriesCount + 1),
                    Title       = $"Playlist_{i}",
                    Description = $"Playlist_{i} description...",
                };

                var videosCount = random.Next(3, 6);
                for (int j = 0; j < videosCount; j++)
                {
                    var vid = new Video
                    {
                        Url = videoUrls[random.Next(0, videoUrls.Length)]
                    };

                    pl.Videos.Add(vid);
                }

                for (int j = 1; j <= 5; j++)
                {
                    var rating = new Rating
                    {
                        User  = authors[j],
                        Value = random.Next(1, 6)
                    };

                    pl.Ratings.Add(rating);
                }

                context.Playlists.Add(pl);
            }

            context.SaveChanges();
        }
Exemple #4
0
        private static void SeedUsers(YouTubePlaylistsDbContext context)
        {
            if (context.Users.Any())
            {
                return;
            }

            var admin = new User()
            {
                UserName      = "******",
                Email         = "*****@*****.**",
                FirstName     = $"adminFN",
                LastName      = $"adminLN",
                PasswordHash  = new PasswordHasher().HashPassword("admin"),
                SecurityStamp = Guid.NewGuid().ToString()
            };

            var roles = context.AppRoles.ToList();

            admin.AppRoles.Add(roles[0]);

            context.Users.AddOrUpdate(admin);

            var random = new Random();

            for (int i = 1; i <= 5; i++)
            {
                var user = new User()
                {
                    Email         = $"user{i}@site.com",
                    UserName      = $"user{i}@site.com",
                    FirstName     = $"user{i}FN",
                    LastName      = $"user{i}LN",
                    PasswordHash  = new PasswordHasher().HashPassword($"user{i}"),
                    SecurityStamp = Guid.NewGuid().ToString()
                };

                user.AppRoles.Add(roles[1]);

                context.Users.Add(user);
            }

            context.SaveChanges();
        }
Exemple #5
0
        private static void SeedCategories(YouTubePlaylistsDbContext context)
        {
            if (context.Categories.Any())
            {
                return;
            }

            for (int i = 1; i <= 30; i++)
            {
                var cat = new Category
                {
                    Name = $"Cat_{i}",
                };

                context.Categories.Add(cat);
            }

            context.SaveChanges();
        }
Exemple #6
0
 public static void Initialize()
 {
     //Database.Delete("YouTubePlaylists");
     Database.SetInitializer(new MigrateDatabaseToLatestVersion <YouTubePlaylistsDbContext, Configuration>());
     YouTubePlaylistsDbContext.Create().Database.Initialize(true);
 }