Пример #1
0
        public Object Post([FromUri] string artid, [FromBody] CartAction cartAction)
        {
            try
            {
                int artidInt = Int32.Parse(artid);
                if (HttpContext.Current.Session["username"] != null)
                {
                    if (HttpContext.Current.Session["cart"] == null)
                    {
                        HttpContext.Current.Session["cart"] =
                            new Cart()
                        {
                            CartItems = new List <CartItem>()
                        };
                    }

                    Cart     cart            = (Cart)HttpContext.Current.Session["cart"];
                    CartItem currentCartItem =
                        cart.CartItems.Find(cartItem => cartItem.ArticleId == artidInt);
                    if (currentCartItem == null)
                    {
                        cart.CartItems.Add(new CartItem()
                        {
                            ArticleId = artidInt, Count = 0
                        });
                        currentCartItem =
                            cart.CartItems.Find(cartItem => cartItem.ArticleId == artidInt);
                    }
                    if (cartAction.actionTypeValue == CartAction.ActionType.neg)
                    {
                        currentCartItem.Count--;
                        if (currentCartItem.Count <= 0)
                        {
                            cart.CartItems.Remove(currentCartItem);
                        }
                    }
                    else if (cartAction.actionTypeValue == CartAction.ActionType.rem)
                    {
                        cart.CartItems.Remove(currentCartItem);
                    }
                    else if (cartAction.actionTypeValue == CartAction.ActionType.add)
                    {
                        currentCartItem.Count++;
                    }


                    HttpContext.Current.Session["cart"] = cart;

                    return(new ApiResponse <List <Cart> >()
                    {
                        data = new List <Cart>()
                        {
                            HttpContext.Current.Session["cart"] as Cart
                        }
                        ,
                        error = ""
                    });
                }
                else
                {
                    return(new ApiResponse <Object>()
                    {
                        error = "Sign in first"
                    });
                }
            }
            catch (Exception ex)
            {
                return(new ApiResponse <Object>()
                {
                    error = ex.Message + " : " + ex.StackTrace
                });
            }
        }
 //Actions on the Cart Table by queries
 public void Add_Update_Delete_ShoppingCart(CartAction action, Product item, int quantity)
 => DB.Add_Update_Delete_ShoppingCart(action, item, quantity);