示例#1
0
        public IActionResult Remove(int id)
        {
            Guid cartId;
            Cart cart = null;

            if (Request.Cookies.ContainsKey("cartId"))
            {
                if (Guid.TryParse(Request.Cookies["cartId"], out cartId))
                {
                    cart = _beerStoreDbContext.Carts
                           .Include(Carts => Carts.CartItems)
                           .ThenInclude(CartItems => CartItems.Product)
                           .FirstOrDefault(x => x.CookieIdentifier == cartId);
                }
            }

            CartItem item = cart.CartItems.FirstOrDefault(x => x.ID == id);

            cart.LastModified = DateTime.Now;

            _beerStoreDbContext.CartItems.Remove(item);

            _beerStoreDbContext.SaveChanges();
            return(RedirectToAction("Index"));
        }
示例#2
0
        //method
        internal static void Initialize(BeerStoreDbContext db)
        {
            db.Database.Migrate();

            if (db.Products.Count() == 0)
            {
                db.Products.Add(new Product
                {
                    Name        = "IPA",
                    Image       = "/Images/beer1.jpg",
                    Description = "California IPA",
                    Price       = 3.99m
                });

                db.Products.Add(new Product
                {
                    Name        = "IPA",
                    Image       = "/Images/beer2.jpg",
                    Description = "Low Alcohol Session IPA",
                    Price       = 4.99m
                });

                db.SaveChanges();
            }
        }
示例#3
0
        public IActionResult Details(int id, int quantity = 1)
        {
            Guid cartId;
            Cart cart = null;

            if (Request.Cookies.ContainsKey("cartId"))
            {
                if (Guid.TryParse(Request.Cookies["cartId"], out cartId))
                {
                    cart = _beerStoreDbContext.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;

                _beerStoreDbContext.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 = _beerStoreDbContext.Products.Find(id);
                cart.CartItems.Add(item);
            }
            item.Quantity    += quantity;
            cart.LastModified = DateTime.Now;

            _beerStoreDbContext.SaveChanges();
            return(RedirectToAction("Index", "Cart"));
        }