コード例 #1
0
        public async Task <ActionResult> PostCart([Bind("ID,ProductID,CartProductQuantity")]  Cart cart)
        {
            if (ModelState.IsValid)
            {
                await _cart.SetCartTotal(cart);

                //BAD LOGIC
                //if wishlist has the same item that was added to the cart, remove the item from the wishlist
                if (await _wishlist.HasSameItem(cart.ProductID))
                {
                    await _wishlist.DeleteSameItem(cart.ProductID);

                    await _wishlist.SaveWishlist();
                }

                //if cart has the same item that was created, increase the quantity of that item.
                if (await _cart.HasSameItem(cart.ProductID))
                {
                    await IncreaseProductQuantity(cart, cart.CartProductQuantity);

                    return(Ok(GetCart(cart.ProductID).Result));
                }

                _cart.AddCart(cart);
                await _cart.SaveCart();

                return(Ok(GetCart(cart.ProductID).Result));
            }
            return(BadRequest());
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("ID,ProductID,CartProductQuantity")] Cart cart)
        {
            if (ModelState.IsValid)
            {
                await _cart.SetCartTotal(cart);


                //BAD LOGIC <if you click on add wishlist then add cart for the same item
                //it will go to wishlist and remove the item>
                //if wishlist has the same item that was added to the cart, remove the item from the wishlist
                if (await _wishlist.HasSameItem(cart.ProductID))
                {
                    await _wishlist.DeleteSameItem(cart.ProductID);

                    await _wishlist.SaveWishlist();

                    //if cart has the same item that was created , increase the quantity of that item.
                    if (await _cart.HasSameItem(cart.ProductID))
                    {
                        await IncreaseProductQuantity(cart, cart.CartProductQuantity);

                        return(RedirectToRoute(new
                        {
                            controller = "Wishlist",
                            action = "index"
                        }));
                    }

                    _cart.AddCart(cart);
                    await _cart.SaveCart();

                    return(RedirectToRoute(new
                    {
                        controller = "Wishlist",
                        action = "index"
                    }));
                }

                //if cart has the same item that was created, increase the quantity of that item.
                if (await _cart.HasSameItem(cart.ProductID))
                {
                    await IncreaseProductQuantity(cart, cart.CartProductQuantity);

                    return(RedirectToAction(nameof(Index)));
                }


                _cart.AddCart(cart);
                await _cart.SaveCart();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(nameof(Index)));
        }