Example #1
0
        internal static void Initialize(this OrganicStoreDbContext db)
        {
            db.Database.Migrate();

            if (db.Products.Count() == 0)
            {
                db.Database.Migrate();

                db.Products.Add(new Product {
                    Description = "Lorem ipsum ipsum lorem",
                    Name        = "Ginger",
                    Image       = "/images/ginger-root.png",
                    Price       = 7.99m
                });

                db.Products.Add(new Product
                {
                    Description = "Lorem ipsum ipsum lorem",
                    Name        = "Hass Avocado",
                    Image       = "/images/HassAvocado.jpg",
                    Price       = 5.49m
                });

                db.Products.Add(new Product
                {
                    Description = "Lorem ipsum ipsum lorem",
                    Name        = "Raspberries",
                    Image       = "/images/raspberries.jpg",
                    Price       = 2.99m
                });
                db.SaveChanges();
            }
        }
        public IActionResult AddToCart(int id, int quantity = 1)
        {
            Guid cartId;
            Cart cart = null;

            if (Request.Cookies.ContainsKey("cartId"))
            {
                if (Guid.TryParse(Request.Cookies["cartId"], out cartId))
                {
                    //https://docs.microsoft.com/en-us/ef/core/querying/related-data
                    cart = _context.Carts
                           .Include(carts => carts.CartItems)
                           .ThenInclude(cartitems => cartitems.Product)
                           .FirstOrDefault(x => x.CookieIdentifier == cartId);
                }
            }

            if (cart == null)
            {
                cart   = new Cart();
                cartId = Guid.NewGuid();
                cart.CookieIdentifier = cartId;

                _context.Carts.Add(cart);
                Response.Cookies.Append("cartId", cartId.ToString(), new Microsoft.AspNetCore.Http.CookieOptions {
                    Expires = DateTime.UtcNow.AddYears(100)
                });
            }
            CartItem item = cart.CartItems.FirstOrDefault(x => x.Product.ID == id);

            if (item == null)
            {
                item         = new CartItem();
                item.Product = _context.Products.Find(id);
                cart.CartItems.Add(item);
            }

            item.Quantity    += quantity;
            cart.LastModified = DateTime.Now;

            _context.SaveChanges();
            return(RedirectToAction("Index", "Cart"));
        }
Example #3
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new OrganicStoreDbContext(
                       serviceProvider.GetRequiredService <
                           DbContextOptions <OrganicStoreDbContext> >()))
            {
                // Look for any movies.
                if (context.User.Any())
                {
                    return;   // DB has been seeded
                }

                context.User.AddRange(
                    new User
                {
                    Id       = 1,
                    Name     = "Bryant Omoregie",
                    Email    = "j",
                    Password = "******"
                }
                    );

                if (context.Product.Any())
                {
                    return;
                }

                context.Product.AddRange(
                    new Product
                {
                    Id           = 1,
                    Name         = "Organic Honeycrisp Apple",
                    Type         = "Produce",
                    Description  = "Organic Honeycrisp Apple, a sodium free food",
                    ProductImage = "https://m.media-amazon.com/images/S/assets.wholefoodsmarket.com/PIE/product/5789086c808ed8290ce339ab_produce_honeycrispapple.1._TTD_._SR300,300_.jpg"
                },

                    new Product
                {
                    Id           = 2,
                    Name         = "Large Hass Avocados",
                    Type         = "Produce",
                    Description  = "Avocado, a sodium free food",
                    ProductImage = "https://m.media-amazon.com/images/S/assets.wholefoodsmarket.com/PIE/product/56e4a5edf7c6bd1100c9b400_365_-hass-avocado.1._TTD_._SR300,300_.jpg"
                },

                    new Product
                {
                    Id           = 3,
                    Name         = "Organic Large Hass Avocados",
                    Type         = "Produce",
                    Description  = "Avocado, a sodium free food",
                    ProductImage = "https://m.media-amazon.com/images/S/assets.wholefoodsmarket.com/PIE/product/575f2594d59d0103e5ac59c2_produce_ogavocados.1._TTD_._SR300,300_.jpg"
                }

                    );

                if (context.Cart.Any())
                {
                    return;
                }

                context.Cart.AddRange
                (
                    new Cart
                {
                    Id       = 1,
                    ClientID = 1,
                }
                );
                context.SaveChanges();
            }
        }