Exemplo n.º 1
0
        public void AddToCart(Product product)
        {
            // Get the matching cart and product instances
            var cartItem = _dbContext.CartItems.SingleOrDefault(
                c => c.CartId == Id
                && c.ProductId == product.Id);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new CartItem
                {
                    Product = product,
                    CartId = Id,
                    Count = 1,
                    DateCreated = DateTime.Now
                };

                _dbContext.CartItems.Add(cartItem);
            }
            else
            {
                // If the item does exist in the cart, then add one to the quantity
                cartItem.Count++;
            }
        }
Exemplo n.º 2
0
        public async Task<IActionResult> Edit(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).SetState(EntityState.Modified);
                await db.SaveChangesAsync(Context.RequestAborted);
                return RedirectToAction("Index");
            }

            return View(product);
        }
Exemplo n.º 3
0
        public async Task<IActionResult> Create(Product product)
        {
            if (ModelState.IsValid)
            {
                await db.Products.AddAsync(product, Context.RequestAborted);
                await db.SaveChangesAsync(Context.RequestAborted);
                return RedirectToAction("Index");
            }

            return View(product);
        }