public IActionResult Index(CartItem item)
        {
            var cartIdCookie = GetEncryptedShopCookie("CART_ID");

            if (cartIdCookie == null)
            {
                var cart = new Cart();
                cart.CartItems = new List <CartItem>();
                cart.CartItems.Add(item);



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

                SetEncryptedShopCookie("CART_ID", cart.Id.ToString());

                PopulateShopData(cart.Id);
            }
            else
            {
                var cartId = Convert.ToInt32(cartIdCookie);

                var cart = _context.Carts
                           .Include(c => c.CartItems)
                           .SingleOrDefault(c => c.Id == cartId);

                if (cart == null)
                {
                    return(NotFound());
                }

                var cartItem = cart.CartItems
                               .SingleOrDefault(ci => ci.ProductId == item.ProductId);

                if (cartItem == null)
                {
                    cart.CartItems.Add(item);
                }
                else
                {
                    if (item.Quantity == 0)
                    {
                        _context.Remove(cartItem);
                    }
                    else
                    {
                        cartItem.Quantity = item.Quantity;
                        _context.Update(cartItem);
                    }
                }

                _context.SaveChanges();

                PopulateShopData(cartId);
            }

            return(PartialView("_CartItemPartial"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("ID,Name,GPU,RAM,Processor,ScreenSizeInch,RouteToImage,ImageBytes")] Notebook notebook)
        {
            if (ModelState.IsValid)
            {
                _context.Add(notebook);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(notebook));
        }
Ejemplo n.º 3
0
        public IActionResult Index(CartItem item)
        {
            var cartIdCookie = GetEncryptedShopCookie("CART_ID");

            if (!ModelState.IsValid)
            {
                // there is a model error
                if (cartIdCookie == null)
                {
                    PopulateViewData(null);
                }
                else
                {
                    PopulateViewData(Convert.ToInt32(cartIdCookie));
                }
                return(View());
            }

            if (cartIdCookie == null)
            {
                // need to create a new cart
                var cart = new Cart();

                cart.CartItems = new List <CartItem>();
                cart.CartItems.Add(item);

                var userId = GetEncryptedShopCookie("USER_ID");
                if (userId != null)
                {
                    cart.UserId = Convert.ToInt32(userId);
                }

                // add the cart to the context, then apply the changes to the database via the SaveChanges method
                _context.Add(cart);
                _context.SaveChanges();

                // set the cartId in a cookie
                SetEncryptedShopCookie("CART_ID", cart.Id.ToString());

                // we are going to show the user the Index.cshtml view, so populate the ViewData values
                PopulateViewData(cart.Id);
            }
            else
            {
                // already has a cart so fetch from database
                var cartId = Convert.ToInt32(cartIdCookie);

                // fetch the cart from the database
                var cart = _context.Carts
                           .Include(c => c.CartItems)
                           .SingleOrDefault(c => c.Id == cartId);

                if (cart == null)
                {
                    return(NotFound());
                }

                // get the existing cart item, if any
                var cartItem = cart.CartItems
                               .SingleOrDefault(ci => ci.ProductId == item.ProductId);

                // add new item to cart or replace quantity on an existing item
                if (cartItem == null)
                {
                    // new item
                    cart.CartItems.Add(item);
                }
                else
                {
                    // update existing item
                    if (item.Quantity == 0)
                    {
                        // delete
                        _context.Remove(cartItem);
                    }
                    else
                    {
                        // update
                        cartItem.Quantity = item.Quantity;
                        _context.Update(cartItem);
                    }
                }

                _context.SaveChanges();

                PopulateViewData(cartId);
            }

            return(View());
        }