. . .

Implement your logic

In the previous parts of the tutorial, you created your first service TutorialService, a class "Grocery" and some attributes. Then we showed you how to download, upload and start your service. We will now have a look at how you can add custom logic.

Quick overview

Let's start with a quick overview of the classes and get an understanding of how the logic is applied within your service. For a more in depth explanation, please refer to the logic flow documentation.
In this part of the tutorial, we will assume that you have basic knowledge about Spring and Spring Boot and understand the principles of beans and application context. Also, refer to the previously linked documentation to understand how we are using the Feign framework.

Your service is a Spring Boot service, with the main class being TutorialServiceApplication.java:

images/download/thumbnails/71320531/Screen-Shot-2020-02-27-at-10.19.30.png

Due to the @SpringBootApplication annotation on TutorialServiceApplication.java, all Spring components in com.apiomat.microservice.tutorialservice package and below are added to the Spring context of your service:

  • In the controller package, there are Controller components that have the API entry points and call service layer logic.

  • In the model package, there are POJO representations of the classes you modeled in the Dashboard (e.g. Grocery.java) and some specific enums for the static data and reference types (see Data oriented content for more details).

  • In the service package, there are @Service components with your service's logic. The package contains the following for each modeled class: A Feign web client interface to YAMBAS, an abstract service class with the default logic and a service implementation where you will override the default logic.

For an exhaustive overview of your service assets, please see Development Basics.

In a nutshell, the workflow goes like this:

  1. The controller classes handle API request mapping and call the internal service logic

  2. The service layer is built up like this:

    1. Global logic is applied in a service implementation provided by the brewer-base library.

    2. The default logic of your Grocery class is contained in AbstractGroceryApiServiceImpl.java. It will just proxy to YAMBAS via the generated Feign client.

    3. The specific logic of your Grocery class has to be implemented in GroceryApiServiceImpl.java, where you can override the abstract implementations.

Concrete implementation

Now that you have a better understanding of the logic flow within your Service, we will work on a concrete implementation case for the TutorialService. Before implementing any logic, you have to download the service from the dashboard and import it in your IDE. If you haven't done this yet, please follow the previous tutorial about Downloading and uploading your Service.

Save example

As stated before, the specific logic for your service has to be implemented in the GroceryApiServiceImpl class. In this case, we want to set a default price of 0.5 for any Grocery that gets created via an API call or the Dashboard:

  1. Open GroceryApiServiceImpl.java in your IDE. The abstract parent of this class provides multiple CRUD operation methods, including the save method.

  2. Override the save method in GroceryApiServiceImpl and set a default price on the saved object:

    GroceryApiServiceImpl.java
    ...
    @Override
    public String save( final String appName, final Grocery model )
    {
    /* set default price of Grocery to 0.5 */
    model.setPrice( 0.5 );
    ....
    }
    ...
  3. End the method by calling the parent's default logic, which will save the Grocery into the YAMBAS database:

    GroceryApiServiceImpl.java
    ...
    @Override
    public String save( final String appName, final Grocery model )
    {
    /* set default price of Grocery to 0.5 */
    model.setPrice( 0.5 );
     
    /* call the abstract parent implementation to save the Grocery */
    return super.save( appName, model );
    }
    ...
  4. Save your implementation change and deploy your service to Innkeeper using the Maven deploy goal (if you did not configure your Maven settings.xml for uploading, please refer to the upload part of the Downloading/Uploading section):

    mvn deploy -DcustomerName=<INSERT_YOUR_CUSTOMERNAME> -DcustomerPassword=<INSERT_YOUR_CUSTOMERPASSWORD> -Dhost=https://<INSERT_YOUR_YAMBAS_HOST>
  5. Go to the Dashboard and start/restart your service:

    images/download/attachments/71320531/Screen-Shot-2020-02-27-at-15.23.36.png

  6. Once your TutorialService is running and healthy, go to the data editor and create a Grocery. You will see that the price has been set to 0.5:

    images/download/attachments/71320531/Screen-Shot-2020-02-27-at-15.26.01.png

Update example

Let's go further and add logic for when a Grocery is updated. In this example we want to automatically apply a tax of 20% to any price that is updated:

  1. Open the GroceryApiServiceImpl.java class in your IDE. The abstract parent provides an update method.

  2. Override the update method in GroceryApiServiceImpl and apply the tax to the price before calling the parent's default logic:

    GroceryApiServiceImpl.java
    ...
    @Override
    public void update( final String appName, final String id, final Grocery model )
    {
    /* update the price of the grocery with the tax multiplier */
    final Double updatedPrice = model.getPrice( ) + ( model.getPrice( ) * 0.2 );
     
    /* set the updated price */
    model.setPrice( updatedPrice );
     
    /* update the Grocery */
    super.update( appName, id, model );
    }
    ...
  3. Again, deploy your service to Innkeeper:

    mvn deploy -DcustomerName=<INSERT_YOUR_CUSTOMERNAME> -DcustomerPassword=<INSERT_YOUR_CUSTOMERPASSWORD> -Dhost=https://<INSERT_YOUR_YAMBAS_HOST>
  4. Go to the Dashboard and restart your service.

  5. Once your TutorialService is running and healthy, go to the data editor and try updating the price of a Grocery. You will see that the tax is being applied when the value is updated.

Next steps