Beispiel #1
0
        //to check if the categories already have data then return that data , otherwise fill the category with new data
        private static async Task SeedCategoriesAsync(DotNetAppContext aspnetrunContext)
        {//CHECKING FOR THE DATA INSIDE THE CATEGORY , THEN IT WILL RETURN
            if (aspnetrunContext.Categories.Any())
            {
                return;
            }

            //OTHERWISE IT  WILL ADD THE DATA
            var categories = new List <Category>()
            {
                new Category()
                {
                    Name = "Laptop"
                },                                 // 1
                new Category()
                {
                    Name = "Drone"
                },                                // 2
                new Category()
                {
                    Name = "TV & Audio"
                },                                       // 3
                new Category()
                {
                    Name = "Phone & Tablet"
                },                                         // 4
                new Category()
                {
                    Name = "Camera & Printer"
                },                                           // 5

                new Category()
                {
                    Name = "Games"
                },                                // 6
                new Category()
                {
                    Name = "Accessories"
                },                                      // 7
                new Category()
                {
                    Name = "Watch"
                },                                // 8
                new Category()
                {
                    Name = "Home & Kitchen Appliances"
                }                                                      // 9
            };

            aspnetrunContext.Categories.AddRange(categories);
            await aspnetrunContext.SaveChangesAsync();
        }
        public static void Seed(
            DotNetAppContext dbContext,
            RoleManager <IdentityRole> roleManager,
            UserManager <IdentityUser> userManager
            )
        {
            // Create default Users (if there are none)
            if (!dbContext.Users.Any())
            {
                CreateUsers(dbContext, roleManager, userManager)
                .GetAwaiter()
                .GetResult();
            }

            // Create default Quizzes (if there are none) together with their set of Q&A
            // if (!dbContext.Quizzes.Any()) CreateQuizzes(dbContext);
        }
Beispiel #3
0
        private static async Task SeedReviewsAsync(DotNetAppContext aspnetrunContext)
        {
            if (aspnetrunContext.Reviews.Any())
            {
                return;
            }

            var reviews = new List <Review>()
            {
                new Review
                {
                    Name    = "Cristopher Lee",
                    EMail   = "*****@*****.**",
                    Star    = 4.3,
                    Comment = "enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia res eos qui ratione voluptatem sequi Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci veli"
                },

                new Review
                {
                    Name    = "Nirob Khan",
                    EMail   = "*****@*****.**",
                    Star    = 4.3,
                    Comment = "enim ipsam voluptatem  quia voluptas sit aspernatur aut odit aut fugit, sed quia res eos qui ratione voluptatem sequi Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci veli"
                },
                new Review
                {
                    Name    = "MD.ZENAUL ISLAM",
                    EMail   = "*****@*****.**",
                    Star    = 4.3,
                    Comment = "enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia res eos qui ratione voluptatem sequi Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci veli"
                }
            };


            aspnetrunContext.Reviews.AddRange(reviews);
            await aspnetrunContext.SaveChangesAsync();
        }
Beispiel #4
0
        //fill all the databse table data
        //seedAsync : name of teh method to fill data

        //WHAT IS ILOGGERFACTORY
        public static async Task SeedAsync(DotNetAppContext aspnetrunContext, ILoggerFactory loggerFactory, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

            //context class  is virtual database
            try
            {
                await SeedCategoriesAsync(aspnetrunContext);
                await SeedReviewsAsync(aspnetrunContext);
                await SeedProductsAsync(aspnetrunContext);
            }
            catch (Exception exception)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    var log = loggerFactory.CreateLogger <DotNetAppContext>();
                    log.LogError(exception.Message);
                    await SeedAsync
                        (aspnetrunContext, loggerFactory, retryForAvailability);
                }
                throw;
            }
        }
Beispiel #5
0
        private static async Task SeedProductsAsync(DotNetAppContext aspnetrunContext)
        {
            if (aspnetrunContext.Products.Any())
            {
                return;
            }
            var products = new List <Product>
            {
                // Phone
                new Product()
                {
                    Name         = "uPhone X",
                    Slug         = "uphone-x",
                    Summary      = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat.",
                    Description  = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat. Temporibus, voluptatibus. Lorem ipsum dolorsit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat. Temporibus, voluptatibus.",
                    ImageFile    = "product-1.png",
                    UnitPrice    = 295,
                    UnitsInStock = 10,
                    Star         = 4.3,
                    Category     = aspnetrunContext.Categories.FirstOrDefault(c => c.Name == "Phone & Tablet"),

                    //Specifications = aspnetrunContext.Specifications.ToList(),
                    Reviews = aspnetrunContext.Reviews.ToList(),
                    // Tags = aspnetrunContext.Tags.ToList()
                },
                new Product()
                {
                    Name         = "Ornet Note 9",
                    Slug         = "ornet-note",
                    Summary      = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo  dolorum ab tempora nihil dicta earum fugiat.",
                    Description  = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat. Temporibus, voluptatibus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat. Temporibus, voluptatibus.",
                    ImageFile    = "product-17.png",
                    UnitPrice    = 285,
                    UnitsInStock = 10,
                    Star         = 4.3,
                    CategoryId   = 4,
                    // Specifications = aspnetrunContext.Specifications.ToList(),
                    Reviews =
                        aspnetrunContext.Reviews.ToList(),
                    //Tags = aspnetrunContext.Tags.ToList()
                },
                new Product()
                {
                    Name         = "Sany Experia Z5",
                    Slug         = "sany-experia",
                    Summary      = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat.",
                    Description  = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo  dolorum ab tempora nihil dicta earum fugiat. Temporibus, voluptatibus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat.Temporibus, voluptatibus.",
                    ImageFile    = "product-24.png",
                    UnitPrice    = 360,
                    UnitsInStock = 10,
                    Star         = 4.3,
                    CategoryId   = 4,
                    //Specifications = aspnetrunContext.Specifications.ToList(),
                    Reviews = aspnetrunContext.Reviews.ToList(),
                    // Tags = aspnetrunContext.Tags.ToList()
                },
                new Product()
                {
                    Name         = "Flex P 3310",
                    Slug         = "flex-p",
                    Summary      = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat.",
                    Description  = "Lorem ipsum dolor  sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat. Temporibus, voluptatibus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat. Temporibus, voluptatibus.",
                    ImageFile    = "product-19.png",
                    UnitPrice    = 220,
                    UnitsInStock = 10,

                    Star       = 4.3,
                    CategoryId = 4,
                    //Specifications = aspnetrunContext.Specifications.ToList(),
                    Reviews = aspnetrunContext.Reviews.ToList(),
                    //Tags = aspnetrunContext.Tags.ToList()
                },
                // Camera
                new Product()
                {
                    Name         = "Mony Handycam Z 105",
                    Slug         = "mony-handycam-z",
                    Summary      = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat.,Temporibus, voluptatibus. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat. Temporibus, voluptatibus.",
                    ImageFile    = "product-5.png",
                    UnitPrice    = 145,
                    UnitsInStock = 10,
                    Star         = 4.3,
                    CategoryId   = 5,
                    //Specifications = aspnetrunContext.Specifications.ToList(),
                    Reviews = aspnetrunContext.Reviews.ToList(),
                    // Tags = aspnetrunContext.Tags.ToList()
                },
                new Product()
                {
                    Name = "Axor Digital Camera",

                    Slug    = "axor-digital",
                    Summary = "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat.sit amet, consectetur adipisicing elit. Ut, tenetur natus doloremque laborum quos iste ipsum rerum obcaecati impedit odit illo dolorum ab tempora nihil dicta earum fugiat. Temporibus, voluptatibus.",

                    ImageFile    = "product-6.png",
                    UnitPrice    = 199,
                    UnitsInStock = 10,
                    Star         = 4.3,
                    CategoryId   = 5,
                    // Specifications =aspnetrunContext.Specifications.ToList(),
                    Reviews = aspnetrunContext.Reviews.ToList(),
                    // Tags = aspnetrunContext.Tags.ToList()
                }
            };

            aspnetrunContext.Products.AddRange(products);
            await aspnetrunContext.SaveChangesAsync();
        }
        private static async Task CreateUsers(
            DotNetAppContext dbContext,
            RoleManager <IdentityRole> roleManager,
            UserManager <IdentityUser> userManager)
        {
            // local variables
            DateTime createdDate      = new DateTime(2016, 03, 01, 12, 30, 00);
            DateTime lastModifiedDate = DateTime.Now;

            //2 different roles(any number of roles)
            string role_Administrator  = "Administrator";
            string role_RegisteredUser = "******";

            //Create Roles (if they doesn't exist yet)
            if (!await roleManager.RoleExistsAsync(role_Administrator))
            {
                await roleManager.CreateAsync(new IdentityRole(role_Administrator));
            }
            if (!await roleManager.RoleExistsAsync(role_RegisteredUser))
            {
                await roleManager.CreateAsync(new IdentityRole(role_RegisteredUser));
            }

            // Create the "Admin" ApplicationUser account
            var user_Admin = new IdentityUser()
            {
                SecurityStamp = Guid.NewGuid().ToString(),
                UserName      = "******",
                Email         = "*****@*****.**",
                //they were custom data (applicationUsrt)
                //CreatedDate = createdDate,
                //LastModifiedDate = lastModifiedDate
            };

            // Insert "Admin" into the Database and assign the "Administrator" and "Registered" roles to him.
            if (await userManager.FindByIdAsync(user_Admin.Id) == null)
            {
                //actual password(Pass$Admin)
                await userManager.CreateAsync(user_Admin, "Pass4Admin");

                // await userManager.AddToRoleAsync(user_Admin, role_RegisteredUser);
                //create the role that user is now admin
                await userManager.AddToRoleAsync(user_Admin, role_Administrator);

                // Remove Lockout and E-Mail confirmation.
                //
                user_Admin.EmailConfirmed = true;
                user_Admin.LockoutEnabled = false;
            }

#if DEBUG
            // Create some sample registered user accounts
            var user_Ryan = new IdentityUser()
            {
                SecurityStamp = Guid.NewGuid().ToString(),
                UserName      = "******",
                Email         = "*****@*****.**",
                //CreatedDate = createdDate,
                //LastModifiedDate = lastModifiedDate
            };

            var user_Solice = new IdentityUser()
            {
                SecurityStamp = Guid.NewGuid().ToString(),
                UserName      = "******",
                Email         = "*****@*****.**",
                //CreatedDate = createdDate,
                //LastModifiedDate = lastModifiedDate
            };

            var user_Vodan = new IdentityUser()
            {
                SecurityStamp = Guid.NewGuid().ToString(),
                UserName      = "******",
                Email         = "*****@*****.**",
                //CreatedDate = createdDate,
                //LastModifiedDate = lastModifiedDate
            };

            // Insert sample registered users into the Database and also assign the "Registered" role to him.
            if (await userManager.FindByIdAsync(user_Ryan.Id) == null)
            {
                await userManager.CreateAsync(user_Ryan, "Pass4Ryan");

                await userManager.AddToRoleAsync(user_Ryan, role_RegisteredUser);

                // Remove Lockout and E-Mail confirmation.
                user_Ryan.EmailConfirmed = true;
                user_Ryan.LockoutEnabled = false;
            }
            if (await userManager.FindByIdAsync(user_Solice.Id) == null)
            {
                await userManager.CreateAsync(user_Solice, "Pass4Solice");

                await userManager.AddToRoleAsync(user_Solice, role_RegisteredUser);

                // Remove Lockout and E-Mail confirmation.
                user_Solice.EmailConfirmed = true;
                user_Solice.LockoutEnabled = false;
            }
            if (await userManager.FindByIdAsync(user_Vodan.Id) == null)
            {
                await userManager.CreateAsync(user_Vodan, "Pass4Vodan");

                await userManager.AddToRoleAsync(user_Vodan, role_RegisteredUser);

                // Remove Lockout and E-Mail confirmation.
                user_Vodan.EmailConfirmed = true;
                user_Vodan.LockoutEnabled = false;
            }
#endif
            await dbContext.SaveChangesAsync();
        }