Example #1
0
        public async Task <int> AddToCart(Items item)
        {
            // Get the matching cart and item instances
            var cartItem = await storeDB.Carts.SingleOrDefaultAsync(
                c => c.CartId == ShoppingCartId &&
                c.ItemId == item.Id);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new Carts
                {
                    ItemId      = item.Id,
                    CartId      = ShoppingCartId,
                    Count       = 1,
                    DateCreated = DateTime.Now
                };
                storeDB.Carts.Add(cartItem);
            }
            else
            {
                // If the item does exist in the cart,
                // then add one to the quantity
                cartItem.Count++;
            }
            // Save changes
            await storeDB.SaveChangesAsync();

            return(cartItem.Count);
        }
        public async Task <IActionResult> Create([Bind("Id,ProductName,Price,Photo")] ProductTbl productTbl)
        {
            if (ModelState.IsValid)
            {
                _context.Add(productTbl);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(productTbl));
        }
        public async Task <int> InsertCustomerRecord(Customer customer)
        {
            using (var context = new ShoppingCartDBContext(ShoppingCartDBContext.optionBuild.dbOption))
            {
                await context.Customers.AddAsync(customer);

                await context.SaveChangesAsync();
            }

            return(customer.CustomerId);
        }