示例#1
0
        public void TestInitialize()
        {
            //create in-memory context and inject to controller instance
            var options = new DbContextOptionsBuilder <LowballersContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            _context = new LowballersContext(options);

            var category = new Categories
            {
                CategoryId = 100,
                Name       = "Some Category"
            };

            //create mock data and add to in-memory database
            products.Add(new Products
            {
                ProductId = 200,
                Name      = "Prod 1",
                Price     = 12,
                Category  = category
            });

            //create mock data and add to in-memory database
            products.Add(new Products
            {
                ProductId = 30,
                Name      = "Prod 0",
                Price     = 15,
                Category  = category
            });

            //create mock data and add to in-memory database
            products.Add(new Products
            {
                ProductId = 29,
                Name      = "Prod 3",
                Price     = 55,
                Category  = category
            });

            foreach (var p in products)
            {
                _context.Add(p);
            }

            _context.SaveChanges();

            //instantiate controller with mock data in the dependency
            productsController = new ProductsController(_context);
        }
示例#2
0
        public IActionResult AddToCart(int Quantity, int ProductId)
        {
            //get the current product price from the db
            var product = _context.Products.SingleOrDefault(p => p.ProductId == ProductId);
            var price   = product.Price;

            //set (if not set) then get cart username
            var cartUsername = GetCartUsername();

            //create and save a new cart object
            var cart = new Carts
            {
                Quantity  = Quantity,
                ProductId = ProductId,
                Price     = price,
                Username  = cartUsername
            };

            _context.Carts.Add(cart);
            _context.SaveChanges();

            //redirect to Cart page
            return(RedirectToAction("Cart"));
        }