コード例 #1
0
        public static bool add(int productid, int qty)
        {
            int         userid = Convert.ToInt32(HttpContext.Current.Session["auth_user"]);
            List <Cart> carts = CartHandler.getCartsById(userid);
            bool        exist = false, exceed = false;

            if (carts.Count != 0)
            {
                foreach (Cart product in carts)
                {
                    if (product.ProductID == productid)
                    {
                        int     newqty  = qty + product.Quantity;
                        Product Product = ProductHandler.getProductByID(productid);
                        if (newqty > Product.Stock)
                        {
                            exceed = true;
                        }
                        else
                        {
                            Cart cart = getCart(productid, userid);
                            CartHandler.update(cart, newqty);
                        }
                        exist = true;
                        break;
                    }
                }
            }
            if (!exist || carts.Count == 0)
            {
                CartHandler.Add(userid, productid, qty);
            }

            if (exceed)
            {
                return(false);
            }

            else
            {
                return(true);
            }
        }