Select version
v7.7 v7.6 v7.5 v7.3 v7.1 v7.0 v6.8 v6.7 v6.6 v6 v5 v4 v3 v2

Implement a Basket Web Service

Say you want a simple service that can add a product to basket, which you can call from your favorite JavaScript framework.

You'll need a class that contains x methods to call. This needs to implement Service. BasketService is nothing but a class with various methods to call.

Naming of the class has nothing to do with the actuall service point URL - we'll get to that in just a brief moment.

    
    
    namespace MyUcommerceApp
    {
    	public class BasketService : Service
    	{
    
    	}
    }
    
    

Now you'll need some kind of input / output. For a basket, you wouldn't need an input as the native APIs in uCommerce can figure that out by default as long as the basket exists and the cookie is supplied in the request - which it is by most defaults.

This means that we do not need any input. However we do need an indicator which tells what the request is. We'll need a class for this - and here we'll also supply the service url (or rather the sub-path of it.)

    
    
    namespace MyUcommerceApp
    {
    	[Route("/User/Basket")]
    	public class BasketRequest : IReturn<Basket> 
    	{
    		
    	}
    }
    
    

The route in the class points to mysite.com/ucommerceapi/user/basket - and will be the specific url that will return the basket for the user. BasketRequest is implemented with IReturn<Basket>, which states that the request will return a JSON object of type Basket.

PLEASE NOTE THAT: Basket should be a copy of the actual Basket, so below I'll create a class Basket, that contains the properties I want to return. So we need to Convert the PurchaseOrder to a basket.

But before I'll do that I'll extend my BasketService with a new method that can return a Basket.

    
    
    namespace MyUcommerceApp
    {
    	public class BasketService : Service
    	{
    		public Basket Get(BasketRequest request) 
    		{
    			PurchaseOrder purchaseOrder = TransactionLibrary.GetBasket(false).PurchaseOrder;
    
    			//You'll need to invent your very own converter to copy your favorite properties to the basket object.
    			Basket basket = ConvertFromPurchaseOrderToBasket(purchaseOrder);
    			
    			return basket;
    		}
    	}
    }
    
    

Now for the "basket web model". It's just another class with properties that ServiceStack will know how to serialize and return as a JavaScript object.

    
    
    namespace MyUcommerceApp
    {
    	public class Basket
        {
            public decimal? SubTotal { get; set; }
            
            public decimal? TaxTotal { get; set; }
    
            public decimal? DiscountTotal { get; set; }
    
            public decimal? OrderTotal { get; set; }
    
            public int? TotalItems { get; set; }
    
            public string FormattedSubTotal { get; set; }
            
            public string FormattedTaxTotal { get; set; }
            
            public string FormattedDiscountTotal { get; set; }
    
            public string FormattedOrderTotal { get; set; }
    
            public string FormattedTotalItems { get; set; }
    
            public List<LineItem> LineItems { get; set; }
        }
    }
    
    

You need to register the webservice assembly in the IOC container. Please see Add a new web service for more details.

Now your service is complete and ready for use. Hitting yoursite.com/ucommerceapi/metadata will show you all the available methods for use.

image
uCommerce Aps - Studsgade 29B - 8000 Aarhus C - Denmark
Phone +45 61 799 997 - Email [email protected] uCommerce logo