Skip to content

Service Development

  1. Create a Java class called ShoppingCartService in the com.mbaas.service package. The name of the package is not important, as long as there is one. Make sure the class implements the com.backendless.servercode.IBackendlessService interface. This is a marker interface - it does not declare any methods. When a class implements the interface, Backendless will handle the class as an API Service:

    package com.mbaas.service;
    
    import com.backendless.servercode.IBackendlessService;
    
    public class ShoppingCartService implements IBackendlessService
    {
    }
    

  2. The service will use three additional classes (see the diagram in the Service Design section) - Create the class files with the code shown below in your project:

    ShoppingItem class:

    package com.mbaas.shopping;
    
    public class ShoppingItem
    {
        public String objectId;
        public String product;
        public float price;
        public int quantity;
    }
    

    ShoppingCart class:
    package com.mbaas.shopping;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class ShoppingCart
    {
      private List<ShoppingItem> items = new ArrayList<>();
    
      public void addItem( ShoppingItem item )
      {
        items.add( item );
      }
    
      public List<ShoppingItem> getItems()
      {
        return items;
      }
    
      public boolean deleteItem( String productName )
      {
        Iterator<ShoppingItem> iterator = items.iterator();
    
        while( iterator.hasNext() )
        {
          ShoppingItem item = iterator.next();
    
          if( item.product.equals( productName ) )
          {
            iterator.remove();
            return true;
          }
        }
    
        return false;
      }
    }
    

    Order class:
    package com.mbaas.shopping;
    
    import java.util.Iterator;
    import java.util.List;
    
    public class Order
    {
        private String objectId;
        private List<ShoppingItem> items;
        private float orderPrice;
    
        public float getOrderPrice()
        {
            return orderPrice;
        }
    
        public void setOrderPrice( float orderPrice )
        {
            this.orderPrice = orderPrice;
        }
    
        public String getObjectId()
        {
            return objectId;
        }
    
        public void setObjectId( String objectId )
        {
            this.objectId = objectId;
        }
    
        public List<ShoppingItem> getItems()
        {
            return items;
        }
    
        public void setItems( List<ShoppingItem> items )
        {
            this.items = items;
    
            Iterator<ShoppingItem> iterator = items.iterator();
    
            while( iterator.hasNext() )
            {
                ShoppingItem item = iterator.next();
                orderPrice += item.price * item.quantity;
            }
        }
    }
    

  3. Return to the ShoppingCartService class and add the method shown below. The method is private and will be used by other methods added in the subsequent steps. The method is responsible for retrieving a ShoppingCart object from Backendless cache.

    package com.mbaas.service;
    
    import com.backendless.servercode.IBackendlessService;
    import com.backendless.Backendless;
    
    public class ShoppingCartService implements IBackendlessService
    {
      private ShoppingCart getCart( String cartName )
      {
        if( cartName == null || cartName.trim().length() == 0 )
          throw new IllegalArgumentException( "Missing cart name" );
    
        return Backendless.Cache.get( cartName, ShoppingCart.class );
      }
    }
    

  4. Add the addItem, deleteItem, getItems and purchase methods which will become API operations once the code is deployed to Backendless. Every method expects the name of the cart. The service implementation maps the ShoppingCart object to the shopping cart name and stores/retrieves it from the server-side cache. If the object is not available in cache (which means it would be the first request for the cart), an object is created and stored in cache. Once a cart is updated in the addItem, deleteItem methods, it is placed back into cache. The purchase method retrieves the shopping cart object from cache and stores its contents (the ShoppingItem objects) in the Order table of the Backendless database

    package com.mbaas.service;
    
    import com.backendless.Backendless;
    import com.backendless.servercode.IBackendlessService;
    import java.util.List;
    
    public class ShoppingCartService implements IBackendlessService
    {
      public void addItem( String cartName, ShoppingItem item )
      {
        ShoppingCart shoppingCart = getCart( cartName );
    
        if( shoppingCart == null )
          shoppingCart = new ShoppingCart();
    
        shoppingCart.addItem( item );
        item.objectId = null;
        Backendless.Cache.put( cartName, shoppingCart );
      }
    
      public boolean deleteItem( String cartName, String productName )
      {
        ShoppingCart shoppingCart = getCart( cartName );
    
        if( shoppingCart == null )
          return false;
    
        boolean result = shoppingCart.deleteItem( productName );
        Backendless.Cache.put( cartName, shoppingCart );
        return result;
      }
    
      public List<ShoppingItem> getItems( String cartName )
      {
        ShoppingCart shoppingCart = getCart( cartName );
    
        if( shoppingCart == null )
          shoppingCart = new ShoppingCart();
    
        return shoppingCart.getItems();
      }
    
      private ShoppingCart getCart( String cartName )
      {
        if( cartName == null || cartName.trim().length() == 0 )
          throw new IllegalArgumentException( "Missing cart name" );
    
        return Backendless.Cache.get( cartName, ShoppingCart.class );
      }
    }
    

  5. Build the project. Make sure all the compiled classes are in the classes directory as shown in the image below. When using the API Service template, they will be copied into the classes directory automatically.
    service-compiled-code