コード例 #1
0
        public JsonResult AddToCart(int productId, int qty = 0)
        {
            if ((qty > c_maxProductQty) || (qty <= 0))
            {
                return(Json(new { success = false }, JsonRequestBehavior.AllowGet));
            }
            ;

            var userId    = Convert.ToInt32(Membership.GetUser().ProviderUserKey.ToString());
            var order     = m_facade.GetOrderByUserId(userId);
            var orderItem = m_facade.GetOrderItem(order.Id, productId);

            if (orderItem == null)
            {
                var product = m_facade.GetProductById(productId);
                m_facade.AddOrderItem(new OrderItem(order.Id, productId, qty, product.Name));
                return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
            }

            var newQty = orderItem.Qty + qty;

            if (newQty > c_maxProductQty)
            {
                newQty = c_maxProductQty;
            }

            orderItem.Qty = newQty;
            var orderItemUpdated = orderItem;

            m_facade.UpdateOrderItem(orderItemUpdated);
            return(Json(new { success = true }, JsonRequestBehavior.AllowGet));
        }