Message:

Subscribe rss
Blog categories
All | Backendless features
How to Perform Complete Business Logic in One API Call
May 30, 2019 by Oleg Vyalyh

Perform Business Logic in One API Call

In a previous article (How to Save an Object with All the Children in a Single Call to Server), we examined how to simply save an object model. However, Backendless custom services give us much more flexibility when it comes to saving objects. In this article, we are going to cover how to perform complex business logic actions such as saving an object with calculated information in one API call using custom services. As we’ll demonstrate in this example, you can actually encapsulate entire portions of your business logic on the server side.

For this example, we will build a custom service that will emulate the order process for an automotive technician service station.

We are going to extend the model from the previous article and use it in the API service that will create an order (as a new record in the database) and attach to it already existing data (dynamic and constant). The same service also will calculate some values on the fly.

Unlike the example where we simply save objects and relations, this one needs some work on the client side as well. We will cover this in more detail later in the article.

Table configuration

We are going to take the model from the previous article and extend it with the following changes (as usual, if you are anxious to test, use the db export). The tables we will be using are described below:

Add a Data Table in Backendless

person

… other columns …
discount DOUBLE

Person Table

auto

body_type STRING
brand STRING
color STRING
engine_capacity INT
model STRING
year INT

Auto Table

work_type

name STRING
price DOUBLE

Fill the table with the values:

  • replacement of brake shoes – 53
  • oil change – 24
  • light calibration – 81

Work Type Table

material_type

name STRING
price DOUBLE

Fill the table with the values:

  • brake shoes – 153.48
  • oil – 8.33

Material Type Table

service_order

customer Data Relation 1:1 person
vehicle Data Relation 1:1 auto
materials Data Relation 1:N material_type
service Data Relation 1:N work_type
discount DOUBLE
total_cost DOUBLE

Service Order Table Schema

Custom service

Now we will create a service that will perform all of our business logic work. (To save you time, we have prepared source files for you to use in CodeRunner or your editor of choice: src_SaveObjectModel_car_service.)

The steps are similar to the previous example, but the result differs.

Step 1. Download CodeRunner and model classes.

First, we need to download CodeRunner. If you aren’t familiar with it and server business logic, please visit our documentation here.

Download CodeRunner for Java

Second, we need model Java classes for our table model. Use the Code Generation section to get them.

Generate Java Classes Code

Step 2. Project creation.

After downloading, you need unpack the zip-archive that contains CodeRunner and open it as a project in IntelliJ IDEA.

From the second archive, you’ll need the data  package with the model classes for your tables. Just put the package data  in the com.<app_name>  package in the opened project.

Then create class SaveObjectModel  in the package services .

The structure should be like this:

Java Object Structure

Step 3. Bootstrap class.

Please note, we renamed classes for convenience and created special data mapping in the Bootstrap  class.

Step 4. The service.

Please read through the comments in the following code.

SaveObjectModel

Step 5. Deploy service

– Build your project

– Use run file <your_project>/bin/Deploy.sh  to deploy the service to Backendless server.

Step 6. Client side

As you might have noticed from database scheme, we have two tables with constant data. It is material_type  and work_type . This is a way to reduce the number of requests to the database both on client and server side.

We can get all necessary constant data during loading of the application and then use them further in the code.

Service test

Test code in a plain Java app. You should copy the onStart()  method with table mappings from the custom service to your test class.

That’s all for today. You can find our archive with test classes here. Thanks for reading, and happy coding!

Share this post
Tweet about this on TwitterShare on FacebookGoogle+