Example #1
0
        public async Task <IActionResult> Edit(int id, [Bind("CategoryID,CategoryName,Description")] Category category)
        {
            if (id != category.CategoryID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(category);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoryExists(category.CategoryID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
        public async Task <IActionResult> Edit(int id, [Bind("ProductID,ProductName,Description,Image,Price,CategoryID")] Product product)
        {
            if (id != product.ProductID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(product);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.ProductID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(List)));
            }
            ViewData["CategoryID"] = new SelectList(_context.Set <Category>(), "CategoryID", "CategoryName", product.CategoryID);
            return(View(product));
        }
        public CartItem AddToCart(int productId)
        {
            var userId = _userManager.GetUserId(_httpContext.User);

            Console.WriteLine($"IN ADDTOCART METHOD: {userId}");
            var cart = GetCartByUserId(userId);

            if (cart == null)
            {
                //create new cart for current user.
                cart = CreateCart();
            }

            //after adding the qty field check if the item already exists in the cart. If so increase the qty;
            //Create shopping cart item
            CartItem cartItem = null;

            if (cart.ItemsList == null)
            {
                cart.ItemsList = new List <CartItem>();
            }

            if (cart.ItemsList.Count() > 0)
            {
                cartItem = cart.ItemsList.FirstOrDefault(p => p.ProductId == productId);
            }

            if (cartItem != null)
            {
                cartItem.Quantity++;
                _dbContext.Update(cartItem);
                _dbContext.SaveChanges();
            }
            else
            {
                cartItem           = new CartItem();
                cartItem.ProductId = productId;
                cartItem.CartID    = cart.Id;
                cartItem.Quantity  = 1;
                _dbContext.Add(cartItem);
                _dbContext.SaveChanges();
            }

            return(cartItem);
        }