public void TestInitialize()
        {
            var options = new DbContextOptionsBuilder <toprockContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            _context = new toprockContext(options);
            products = new List <Product>();

            // 2. create mock data and add to in-memory database
            Category mockCategory = new Category
            {
                CategoryId = 100,
                Name       = "A Fake Category"
            };

            products.Add(new Product
            {
                ProductId = 1,
                Name      = "Product A",
                Price     = 10,
                Category  = mockCategory
            });

            products.Add(new Product
            {
                ProductId = 2,
                Name      = "Product B",
                Price     = 5,
                Category  = mockCategory
            });

            products.Add(new Product
            {
                ProductId = 3,
                Name      = "Product C",
                Price     = 15,
                Category  = mockCategory
            });

            foreach (var p in products)
            {
                // add each product to in-memory db
                _context.Product.Add(p);
            }
            _context.SaveChanges();


            productsController = new ProductsController(_context);
        }
        public IActionResult AddToCart(int Quantity, int ProductId)
        {
            // identity product price
            var product = _context.Product.SingleOrDefault(p => p.ProductId == ProductId);
            var price   = product.Price;

            // determine the username;
            var cartUsername = GetCartUsername();

            // check if this user has this product is already in cart. If so, update quantity
            var cartItem = _context.Cart.SingleOrDefault(c => c.ProductId == ProductId && c.Username == cartUsername);

            if (cartItem == null)
            {
                // if product not already in cart, create and save a new Cart object
                var cart = new Cart
                {
                    ProductId = ProductId,
                    Quantity  = Quantity,
                    Price     = price,
                    Username  = cartUsername
                };

                _context.Cart.Add(cart);
            }
            else
            {
                cartItem.Quantity += Quantity; // add the new quantity to the existing quantity
                _context.Update(cartItem);
            }

            _context.SaveChanges();

            //show the cart page
            return(RedirectToAction("Cart"));
        }