Ejemplo n.º 1
0
        /// <summary>
        /// Request shopping cart.
        /// </summary>
        /// <param name="request">Shopping cart request message.</param>
        /// <returns>Shopping cart response message.</returns>
        public CartResponse GetCart(CartRequest request)
        {
            CartResponse response = new CartResponse();
            response.CorrelationId = request.RequestId;

            // Validate client tag and access token
            if (!ValidRequest(request, response, Validate.ClientTag | Validate.AccessToken))
                return response;

            // Always return recomputed shopping cart
            response.Cart = Mapper.ToDataTransferObject(_shoppingCart);

            return response;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sets (add, edit, delete) shopping cart data.
        /// </summary>
        /// <param name="request">Shopping cart request message.</param>
        /// <returns>Shopping cart response message.</returns>
        public CartResponse SetCart(CartRequest request)
        {
            CartResponse response = new CartResponse();
            response.CorrelationId = request.RequestId;

            // Validate client tag and access token
            if (!ValidRequest(request, response, Validate.ClientTag | Validate.AccessToken))
                return response;

            if (request.Action == "Read")
            {
                // Do nothing, just return cart
            }
            else if (request.Action == "Create")
            {
                _shoppingCart.AddItem(request.CartItem.Id, request.CartItem.Name,
                    request.CartItem.Quantity, request.CartItem.UnitPrice);
            }
            else if (request.Action == "Update")
            {
                // Either shipping method or quantity requires update
                if (!string.IsNullOrEmpty(request.ShippingMethod))
                    _shoppingCart.ShippingMethod = (ShippingMethod)Enum.Parse(typeof(ShippingMethod), request.ShippingMethod);
                else
                    _shoppingCart.UpdateQuantity(request.CartItem.Id, request.CartItem.Quantity);
            }
            else if (request.Action == "Delete")
            {
                _shoppingCart.RemoveItem(request.CartItem.Id);
            }

            _shoppingCart.ReCalculate();
            response.Cart = Mapper.ToDataTransferObject(_shoppingCart);

            return response;
        }