public static object ApiAdd(IRequest context)
        {
            var siteId      = context.GetPostInt("siteId");
            var productId   = context.GetPostString("productId");
            var productName = context.GetPostString("productName");
            var imageUrl    = context.GetPostString("imageUrl");
            var linkUrl     = context.GetPostString("linkUrl");
            var fee         = context.GetPostDecimal("fee");
            var isDelivery  = context.GetPostBool("isDelivery");
            var count       = context.GetPostInt("count", 1);
            var sessionId   = context.GetPostString("sessionId");

            var cartInfo = new CartInfo
            {
                SiteId      = siteId,
                UserName    = context.UserName,
                SessionId   = sessionId,
                ProductId   = productId,
                ProductName = productName,
                ImageUrl    = imageUrl,
                LinkUrl     = linkUrl,
                Fee         = fee,
                IsDelivery  = isDelivery,
                Count       = count,
                AddDate     = DateTime.Now
            };

            var cartId = CartDao.GetCartId(siteId, sessionId, productId);

            if (cartId == 0)
            {
                cartId = CartDao.Insert(cartInfo);
            }
            else
            {
                cartInfo             = CartDao.GetCartInfo(cartId);
                cartInfo.UserName    = context.UserName;
                cartInfo.ProductName = productName;
                cartInfo.ImageUrl    = imageUrl;
                cartInfo.LinkUrl     = linkUrl;
                cartInfo.Fee         = fee;
                cartInfo.IsDelivery  = isDelivery;
                cartInfo.Count      += count;
                cartInfo.AddDate     = DateTime.Now;
                CartDao.Update(cartInfo);
            }

            return(cartId);
        }
        public static object ApiCartSave(IRequest context)
        {
            var siteId       = context.GetPostInt("siteId");
            var sessionId    = context.GetPostString("sessionId");
            var cartInfoList = context.GetPostObject <List <CartInfo> >("cartInfoList");

            CartDao.Delete(siteId, context.UserName, sessionId);
            foreach (var cartInfo in cartInfoList)
            {
                cartInfo.UserName = context.UserName;
                cartInfo.Id       = CartDao.Insert(cartInfo);
            }

            return(cartInfoList);
        }
Example #3
0
 public ActionResult Payment(CartModel entity)
 {
     if (ModelState.IsValid)
     {
         var cart = SessionHelper.GetSessionCart();
         var list = new List <CartItemModel>();
         if (cart != null)
         {
             list = (List <CartItemModel>)cart;
             var dCart = new tb_Carts();
             dCart.Address     = entity.Address;
             dCart.Date        = DateTime.Now;
             dCart.Description = entity.Description;
             dCart.Email       = entity.Email;
             dCart.FullName    = entity.FullName;
             dCart.Id          = entity.Id;
             dCart.Modified    = DateTime.Now;
             dCart.PhoneNumber = entity.PhoneNumber;
             dCart.Status      = true;
             dCart.Total       = list.Sum(x => x.Product.Price - ((x.Product.Price * x.Product.Sale) / 100));
             dCart.UserId      = entity.UserId;
             dCart.Approval    = false;
             long Id = Dao.Insert(dCart);
             foreach (var item in list)
             {
                 var detail = new tb_DetailCart();
                 detail.CartId    = Id;
                 detail.ProductId = item.Product.Id;
                 detail.Quantity  = item.Quantity;
                 detail.Price     = item.Product.Price - ((item.Product.Price * item.Product.Sale) / 100);
                 Dao.InsertCDetail(detail);
             }
             return(Redirect("/hoan-thanh"));
         }
         else
         {
             ModelState.AddModelError(string.Empty, "Không có sản phẩm nào trong đơn hàng!");
         }
     }
     return(View(entity));
 }