Esempio n. 1
0
        public static async void SeedRelationships(ApplicationDbContext context, IServiceProvider serviceProvider)
        {
            context.Database.EnsureCreated();
            var             userManager = serviceProvider.GetRequiredService <UserManager <ApplicationUser> >();
            ApplicationUser user        = await userManager.FindByEmailAsync("*****@*****.**");

            ApplicationUser user2 = await userManager.FindByEmailAsync("*****@*****.**");

            ApplicationUser admin = await userManager.FindByEmailAsync("*****@*****.**");

            // seed some book listings
            if (!context.BookListings.Any())
            {
                var bookListings = new BookListing[]
                {
                    new BookListing(1, user.Id, 1, "Good", 45.99, 1),
                    new BookListing(3, user.Id, 2, "Fair", 10.00, 0),
                    new BookListing(2, user2.Id, 3, "Fair", 15.49, 0),
                    new BookListing(4, user.Id, 4, "Excellent", 14.00, 1),
                    new BookListing(4, user2.Id, 4, "Fair", 22.95, 0)
                };
                foreach (BookListing listing in bookListings)
                {
                    context.Add(listing);
                }
                await context.SaveChangesAsync();
            }

            // seed some transaction history
            if (!context.TransactionLogs.Any())
            {
                var transactions = new TransactionLog[]
                {
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 102.55, "New", new DateTime(2016, 5, 29)),
                    new TransactionLog(user.Id, user2.Id, 6, 2, 0, 43.22, "Good", new DateTime(2016, 8, 13)),
                    new TransactionLog(user2.Id, user.Id, 7, 2, 0, 67.12, "Fair", new DateTime(2017, 1, 13)),
                    new TransactionLog(user2.Id, admin.Id, 8, 3, 0, 12.99, "Bad", new DateTime(2012, 12, 21)),
                    new TransactionLog(user2.Id, user.Id, 9, 4, 0, 150.00, "Excellent", DateTime.Now)
                };
                foreach (TransactionLog tran in transactions)
                {
                    context.Add(tran);
                }
                await context.SaveChangesAsync();
            }
        }
Esempio n. 2
0
        public static void SeedRelationships(ApplicationDbContext context, IServiceProvider serviceProvider)
        {
            context.Database.EnsureCreated();
            var userManager = serviceProvider.GetRequiredService <UserManager <ApplicationUser> >();

            var users = context.Users.ToList();
            // 21 Books seeded.
            var bookCount = 21;
            var condition = new string[] { "New", "Excellent", "Good", "Fair", "Bad" };

            // Seed some book listings.
            if (!context.BookListings.Any())
            {
                Random   r      = new Random();
                DateTime date   = new DateTime(2017, 5, 1);
                var      course = context.Courses.ToList();
                for (int i = 12; i < bookCount + 1; i++)
                {
                    int    basePrice = r.Next(10, 301);
                    double maxPrice  = basePrice + (r.Next(0, (basePrice / 2))) + r.NextDouble();
                    int    range     = (basePrice / 5);
                    double minPrice  = (maxPrice - range) + r.NextDouble();
                    for (int c = 0; c < condition.Length; c++)
                    {
                        int    seller   = r.Next(0, users.Count);
                        double price    = r.NextDouble() * (maxPrice - (minPrice)) + minPrice;
                        var    courseId = r.Next(1, course.Count + 1);
                        var    listing  = new BookListing(i, users[seller].Id, courseId, condition[c], Math.Round(price, 2), r.Next(2));
                        context.BookListings.Add(listing);
                        if (!context.BookToCourses.Where(a => a.BookID == i && a.CourseID == courseId).Any())
                        {
                            var bookToCourse = new BookToCourse(i, courseId);
                            context.BookToCourses.Add(bookToCourse);
                            context.SaveChanges();
                        }
                    }
                }
            }
            context.SaveChanges();

            // Seed some transaction history.
            if (!context.TransactionLogs.Any())
            {
                Random r = new Random();
                for (int i = 12; i < bookCount + 1; i++)
                {
                    int      basePrice = r.Next(10, 301);
                    DateTime startDate = new DateTime(2017, 4, 1);
                    DateTime endDate   = startDate.AddDays(29);
                    while (DateTime.Compare(startDate, endDate) <= 0)
                    {
                        double maxPrice = basePrice + (r.Next(0, (basePrice / 2))) + r.NextDouble();
                        int    range    = (basePrice / 5);
                        double minPrice = (maxPrice - range) + r.NextDouble();
                        for (int c = 0; c < condition.Length; c++)
                        {
                            int transCount = r.Next(3, 10);
                            for (int t = 0; t < transCount; t++)
                            {
                                int seller = r.Next(0, users.Count);
                                int buyer  = r.Next(0, users.Count);
                                do
                                {
                                    buyer = r.Next(0, users.Count);
                                } while (buyer == seller);
                                double rating = 0 + (5 - 0) * r.NextDouble();
                                double price  = r.NextDouble() * (maxPrice - (minPrice)) + minPrice;
                                users[seller].Rating += rating;
                                var tran = new TransactionLog(users[seller].Id, users[buyer].Id, i, Math.Round(rating, 1), 0, Math.Round(price, 2), condition[c], startDate);
                                context.TransactionLogs.Add(tran);
                            }
                            //Change price for next condition.
                            maxPrice = minPrice + r.Next(0, (range / 2)) + r.NextDouble();
                            minPrice = (maxPrice - range) + r.NextDouble();
                        }
                        startDate = startDate.AddDays(1);
                    }
                }
                context.SaveChanges();

                // Update the users' rating.
                foreach (ApplicationUser usr in users)
                {
                    var transCount = context.TransactionLogs.Where(a => a.SellerID == usr.Id).Count();
                    var rating     = Math.Round(usr.Rating / transCount, 1);
                    usr.Rating = rating;
                    context.Update(usr);
                    //await userManager.UpdateAsync(usr);
                }
                context.SaveChanges();
            }
        }
Esempio n. 3
0
        public static async void SeedRelationships(ApplicationDbContext context, IServiceProvider serviceProvider)
        {
            context.Database.EnsureCreated();
            var             userManager = serviceProvider.GetRequiredService <UserManager <ApplicationUser> >();
            ApplicationUser user        = await userManager.FindByEmailAsync("*****@*****.**");

            ApplicationUser user2 = await userManager.FindByEmailAsync("*****@*****.**");

            ApplicationUser admin = await userManager.FindByEmailAsync("*****@*****.**");

            // seed some book listings
            if (!context.BookListings.Any())
            {
                var bookListings = new BookListing[]
                {
                    new BookListing(1, user.Id, 1, "Good", 45.99, 1),
                    new BookListing(3, user.Id, 2, "Fair", 10.00, 0),
                    new BookListing(2, user2.Id, 3, "Fair", 15.49, 0),
                    new BookListing(4, user.Id, 4, "Excellent", 14.00, 1),
                    new BookListing(4, user2.Id, 4, "Fair", 22.95, 0)
                };
                foreach (BookListing listing in bookListings)
                {
                    context.Add(listing);
                }
                await context.SaveChangesAsync();
            }

            // seed some transaction history
            if (!context.TransactionLogs.Any())
            {
                var transactions = new TransactionLog[]
                {
                    // ---- Same Condition ---- \\
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 102.55, "New", new DateTime(2016, 5, 10)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 96.45, "New", new DateTime(2016, 5, 10)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 98.35, "New", new DateTime(2016, 5, 10)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 105.03, "New", new DateTime(2016, 5, 10)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 101.67, "New", new DateTime(2016, 5, 10)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 100.34, "New", new DateTime(2016, 5, 10)),

                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 100.00, "New", new DateTime(2016, 5, 11)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 101.34, "New", new DateTime(2016, 5, 11)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 99.34, "New", new DateTime(2016, 5, 11)),

                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 98.47, "New", new DateTime(2016, 5, 12)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 100.67, "New", new DateTime(2016, 5, 12)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 103.45, "New", new DateTime(2016, 5, 12)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 93.34, "New", new DateTime(2016, 5, 12)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 95.86, "New", new DateTime(2016, 5, 12)),

                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 104.21, "New", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 102.34, "New", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 100.61, "New", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 103.23, "New", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 99.94, "New", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 96.01, "New", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 89.34, "New", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 97.75, "New", new DateTime(2016, 5, 13)),

                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 108.50, "New", new DateTime(2016, 5, 14)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 106.60, "New", new DateTime(2016, 5, 14)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 102.93, "New", new DateTime(2016, 5, 14)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 101.63, "New", new DateTime(2016, 5, 14)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 100.43, "New", new DateTime(2016, 5, 14)),

                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 103.94, "New", new DateTime(2016, 5, 15)),

                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 101.34, "New", new DateTime(2016, 5, 16)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 100.56, "New", new DateTime(2016, 5, 16)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 96.63, "New", new DateTime(2016, 5, 16)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 99.02, "New", new DateTime(2016, 5, 16)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 103.43, "New", new DateTime(2016, 5, 16)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 109.73, "New", new DateTime(2016, 5, 16)),
                    // -------- \\

                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 88.15, "Excellent", new DateTime(2016, 5, 10)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 89.35, "Excellent", new DateTime(2016, 5, 11)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 82.15, "Excellent", new DateTime(2016, 5, 12)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 84.05, "Excellent", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 90.12, "Excellent", new DateTime(2016, 5, 14)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 89.34, "Excellent", new DateTime(2016, 5, 15)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 87.84, "Excellent", new DateTime(2016, 5, 16)),

                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 79.15, "Good", new DateTime(2016, 5, 10)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 74.63, "Good", new DateTime(2016, 5, 11)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 70.00, "Good", new DateTime(2016, 5, 12)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 72.75, "Good", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 70.05, "Good", new DateTime(2016, 5, 14)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 77.45, "Good", new DateTime(2016, 5, 15)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 75.50, "Good", new DateTime(2016, 5, 16)),

                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 53.12, "Fair", new DateTime(2016, 5, 10)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 50.03, "Fair", new DateTime(2016, 5, 11)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 42.75, "Fair", new DateTime(2016, 5, 12)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 45.16, "Fair", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 46.99, "Fair", new DateTime(2016, 5, 14)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 49.73, "Fair", new DateTime(2016, 5, 15)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 48.00, "Fair", new DateTime(2016, 5, 16)),

                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 33.12, "Bad", new DateTime(2016, 5, 10)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 28.34, "Bad", new DateTime(2016, 5, 11)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 26.75, "Bad", new DateTime(2016, 5, 12)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 35.95, "Bad", new DateTime(2016, 5, 13)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 10.65, "Bad", new DateTime(2016, 5, 14)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 27.94, "Bad", new DateTime(2016, 5, 15)),
                    new TransactionLog(user.Id, user2.Id, 5, 5, 0, 30.11, "Bad", new DateTime(2016, 5, 16)),


                    new TransactionLog(user.Id, user2.Id, 6, 2, 0, 43.22, "Good", new DateTime(2016, 8, 13)),
                    new TransactionLog(user2.Id, user.Id, 7, 2, 0, 67.12, "Fair", new DateTime(2017, 1, 13)),
                    new TransactionLog(user2.Id, admin.Id, 8, 3, 0, 12.99, "Bad", new DateTime(2012, 12, 21)),
                    new TransactionLog(user2.Id, user.Id, 9, 4, 0, 150.00, "Excellent", DateTime.Now),
                };
                foreach (TransactionLog tran in transactions)
                {
                    context.Add(tran);
                }
                await context.SaveChangesAsync();
            }
        }