Exemplo n.º 1
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int bookId = Convert.ToInt32(context.Request["bookId"]);

            //判断一下商品是否存在!!
            BLL.BookManager bookManager = new BLL.BookManager();
            Model.Book      bookModel   = bookManager.GetModel(bookId);
            if (bookModel != null)
            {
                //判断一下数据库中是否有该商品,如果有,更新数量,没有添加.
                BLL.CartManager cartManager = new BLL.CartManager();
                int             userId      = ((Model.User)context.Session["userInfo"]).Id;
                Model.Cart      cartModel   = cartManager.GetCartModel(bookId, userId);
                if (cartModel != null)
                {
                    cartModel.Count = cartModel.Count + 1;
                    cartManager.Update(cartModel);
                }
                else
                {
                    Model.Cart modelCart = new Model.Cart();
                    modelCart.Count = 1;
                    modelCart.User  = (Model.User)context.Session["userInfo"];
                    modelCart.Book  = bookModel;
                    cartManager.Add(modelCart);
                }
                context.Response.Write("yes");
            }
            else
            {
                context.Response.Write("no");
            }
        }
Exemplo n.º 2
0
 public void ProcessRequest(HttpContext context)
 {
     context.Response.ContentType = "text/plain";
     BLL.UserManager userManager = new BLL.UserManager();
     if (userManager.ValidateUserLogin())//判断是否登录
     {
         int bookId = Convert.ToInt32(context.Request["bookId"]);
         //判断数据库中是否有该商品.
         BLL.BookManager bookManager = new BLL.BookManager();
         Model.Book      bookModel   = bookManager.GetModel(bookId);
         if (bookModel != null)
         {
             int             userId      = ((Model.User)context.Session["userInfo"]).Id;//获取登录用户登录的ID。
             BLL.CartManager cartManager = new BLL.CartManager();
             Model.Cart      cartModel   = cartManager.GetModel(userId, bookId);
             //如果购物车有该商品,更新数量加1,没有插入
             if (cartModel != null)
             {
                 cartModel.Count = cartModel.Count + 1;
                 cartManager.Update(cartModel);
             }
             else
             {
                 Model.Cart modelCart = new Model.Cart();
                 modelCart.Count = 1;
                 modelCart.Book  = bookModel;
                 modelCart.User  = ((Model.User)context.Session["userInfo"]);
                 cartManager.Add(modelCart);
             }
             context.Response.Write("ok:商品成功添加到购物车");
         }
         else
         {
             context.Response.Write("no:无此商品");
         }
     }
     else
     {
         context.Response.Write("login:没有登录");
     }
 }