public async Task SeedAsync()
        {
            // run the migration if necessary
            _ctx.Database.EnsureCreated();

            StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new StoreUser()
                {
                    FirstName = "Andreea",
                    LastName  = "Stefan",
                    Email     = "*****@*****.**",
                    UserName  = "******",
                };

                var result = await _userManager.CreateAsync(user, "P@ssw0rd!");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create a new user!");
                }
            }


            // if count(products) > 0
            if (!_ctx.Products.Any())
            {
                // need to create sample data
                var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json");

                var json = File.ReadAllText(filepath);

                // deserialize the json and then serialize into a IEnumberable (list of products)
                var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json);

                _ctx.Products.AddRange(products);

                var order = _ctx.Orders.Where(o => o.Id == 1).FirstOrDefault();
                if (order != null)
                {
                    order.User  = user;
                    order.Items = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    };
                }

                _ctx.SaveChanges();
            }
        }
Beispiel #2
0
 public bool SaveAll()
 {
     try
     {
         return(_ctx.SaveChanges() > 0);
     }
     catch (Exception ex)
     {
         _logger.LogError($"Failed to get products by catagory: {ex}");
         return(false);
     }
 }
Beispiel #3
0
        public async Task Seed()
        {
            _ctx.Database.EnsureCreated();

            var user = await _userManager.FindByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new StoreUser()
                {
                    FirstName = "atlas",
                    LastName  = "bishop",
                    UserName  = "******",
                    Email     = "*****@*****.**"
                };

                var result = await _userManager.CreateAsync(user, "P@ssw0rd!");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Failed to create new user");
                }
            }

            if (!_ctx.Products.Any())
            {
                // need to create sample data
                var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json");
                var json     = File.ReadAllText(filepath);
                var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json);

                _ctx.Products.AddRange(products);

                var order = new Order()
                {
                    OrderDate   = DateTime.Now,
                    OrderNumber = "12345",
                    User        = user,
                    Items       = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    }
                };

                _ctx.Orders.Add(order);
                _ctx.SaveChanges();
            }
        }
Beispiel #4
0
 public bool SaveAll()
 {
     return(_ctx.SaveChanges() > 0);
 }