Ejemplo n.º 1
0
 public bool DbIsHealthy(WebshopContext context)
 {
     return(context.ProductCategories.Count() == ProductCategorySeeder.Count() &&
            context.Addresses.Count() == AddressSeeder.Count() &&
            context.Customers.Count() == CustomerSeeder.Count() &&
            context.Images.Count() == ImageSeeder.Count() &&
            context.Warehouses.Count() == WarehouseSeeder.Count() &&
            context.Products.Count() == ProductSeeder.Count() &&
            context.Orders.Count() == OrderSeeder.Count() &&
            context.OrderProducts.Count() == OrderProductSeeder.Count());
 }
Ejemplo n.º 2
0
        public override void Initialize(IServiceProvider serviceProvider, WebshopContext context)
        {
            base.Initialize(serviceProvider, context);
            // check if database is healthy
            if (!DbIsHealthy(context))
            {
                base.RecreateDatabase(context);
            }
            else
            {
                return;
            }

            // seed data
            var productCategories = ProductCategorySeeder.GetSeedList();

            context.ProductCategories.AddRange(productCategories);
            context.SaveChanges();

            var addresses = AddressSeeder.GetSeedList();

            context.Addresses.AddRange(addresses);
            context.SaveChanges();

            var customers = CustomerSeeder.GetSeedList();

            context.Customers.AddRange(customers);
            context.SaveChanges();

            var images = ImageSeeder.GetSeedList();

            context.Images.AddRange(images);
            context.SaveChanges();

            var warehouses = WarehouseSeeder.GetSeedList();

            context.Warehouses.AddRange(warehouses);
            context.SaveChanges();

            var products = ProductSeeder.GetSeedList();

            context.Products.AddRange(products);
            context.SaveChanges();

            var orders = OrderSeeder.GetSeedList();

            context.Orders.AddRange(orders);
            context.SaveChanges();

            var orderProducts = OrderProductSeeder.GetSeedList();

            context.OrderProducts.AddRange(orderProducts);
            context.SaveChanges();
        }
        private static void Build()
        {
            if (_products.Any())
            {
                return;
            }
            var categories = ProductCategorySeeder.GetSeedList();
            var colors     = new List <string> {
                "Red", "Green", "Blue", "Orange", "Yellow", "Purple", "White", "Black", "Pink", "Fuchsia", "Silver", "Gray", "Olive", "Maroon", "Aqua", "Lime", "Teal", "Navy", "Brown", "Peach", "Gold"
            };
            var sizes = new List <string> {
                "XXS", "XS", "SM", "M", "L", "XL", "XXL"
            };
            var colorsAndSizes = new List <string>();

            colors.ForEach(color => sizes.ForEach(size => colorsAndSizes.Add($"{color} - {size}")));
            var sb       = new StringBuilder();
            var products = new List <Product>();

            var id = 1;

            for (var i = 1; i <= categories.Count; i++)
            {
                var category      = categories[i - 1];
                var categoryPrice = i * 10;
                var price         = categoryPrice > 50 ? 20 : categoryPrice;

                foreach (var colorsAndSize in colorsAndSizes)
                {
                    sb.Clear();
                    sb.Append(category.Name.EndsWith("s")
                        ? category.Name.Substring(0, category.Name.Length - 1)
                        : category.Name);
                    sb.Append($" {colorsAndSize}");
                    var amountInStock = id % 2 == 0 ? (50) + (id * 77) % 100 : 100 - (id * 77) % 33;

                    products.Add(BuildItem(id, sb.ToString(), id, amountInStock, sb.ToString(), price, i,
                                           id % 2 == 0 ? 1 : 2));

                    id++;
                }
            }
            _products = products;
        }