Exemplo n.º 1
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 = _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"));
        }
Exemplo n.º 2
0
        internal static void Initialize(this ThisSiteDbContext db)
        {
            db.Database.Migrate();

            if (db.Products.Count() == 0)
            {
                db.Products.Add(new Product
                {
                    Description = "This hat sucks.",
                    Image       = "/images/hat1.jpg",
                    Name        = "This Site Sucks, the Hat",
                    Price       = 25m
                });
                db.Products.Add(new Product
                {
                    Description = "This shirt sucks.",
                    Image       = "/images/shirt1.jpg",
                    Name        = "This Site Sucks, the Shirt",
                    Price       = 20m
                });
                db.Products.Add(new Product
                {
                    Description = "<div style=\"align:center\">",
                    Image       = "/images/shirtcss1.jpg",
                    Name        = "CSS Sucks, The Shirt",
                    Price       = 20m
                });
                db.Products.Add(new Product
                {
                    Description = "Like your dreams, this shirt is broken.",
                    Image       = "/images/brokenshirt.jpg",
                    Name        = "Broken Shirt, The Shirt",
                    Price       = 20m
                });


                db.SaveChanges();
            }
        }
Exemplo n.º 3
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 = _context.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;

            _context.CartItems.Remove(item);

            _context.SaveChanges();
            return(RedirectToAction("Index"));
        }