On the post Exposing Data from DB2 Using StrongLoop Running on Linux on Z , the company Bread Box Grocery started working on a Coupon Distribution service. The service should provide value-added to Bread Box Grocery’s customers while at the same time increase revenue for Bread Box Grocery. The creation of the service had to be easy and fast to develop since the concept has to be tested. The service had to run on Linux on System Z to be close to the data since the data is stored in a Database running on Linux on System Z. Having the service run close to the data will lower latency and improve performance. The previous blog showed how easy was to get started with the creation and test of the application. This blog will show how to extend the APIs created in the previous blog entry and how to add a user interface to consume the created APIs.

Extending APIs

When the PARTNERS model was created (see Exposing Data from DB2 Using StrongLoop Running on Linux on Z ), the StrongLoop command $ slc loopback:model created two files: the partners.json and the partners.js. The file partners.json is used, among other things, to describe the mapping between the data source and the model. The file partners.js is use to extend APIs related to the PARTNERS model. The following section will show how to extend the POST operations for the PARTNERS model to returned only the last 4 entries in the database. The API extension to the PARTNERS model will be use in the user interface shown in the following picture.


User Interface to create a partner

User Interface to create a partner

To extend the model’s API add the following section shows in bold letters to the partners.js file:

# vi common/models/partners.js

module.exports = function(Partners) {
Partners.create_partners = function( json_arg, callback ) {
Partners.create({
"NAME" : json_arg.partner_name
});
Partners.find( { limit: 4, order: 'ID DESC' }, callback );
}
Partners.remoteMethod(
'create_partners',
{
http: { path: '/create_partners', verb: 'post'},
accepts: { arg: 'json_arg', type: 'object', required: true,
'http': {source: 'body'} },
returns: { description: "{ID: 123, NAME: 'Babies Emporium'}",
root: true, type: 'array' }
}
);
};

The Partners.remoteMethod function exposes the method as an external API. For this example the POST call accepts a JSON object as argument, and returns an array of JSON objects containing the ID and Name of each partner.
The Partners.create_partners function does the work, by taking for arguments the JSON Object and a callback object, creating the partner for the arguments in the database, and finding the last 4 entries in the database. To test the changes, start the application and open a browser to the explorer page.

# slc run
INFO strong-agent v2.0.2 profiling app 'breadbox_api' pid '26015'
INFO strong-agent[26015] started profiling agent
INFO supervisor reporting metrics to `internal:`
supervisor running without clustering (unsupervised)
INFO strong-agent not profiling, agent metrics requires a valid license.
Please contact sales@strongloop.com for assistance.
Web server listening at: http://0.0.0.0:3000
Browse your REST API at http://0.0.0.0:3000/explorer


Extended APIs for the PARTNERS model

Extended APIs for the PARTNERS model

Once the browser is open, click on the "POST /PARTNERs/create_partner" section, type the JSON argument in the json_arg text area and click on the Try it out! button:


create_partners POST operation request

create_partners POST operation request

After clicking the button the response should look like the following picture:


create_partners POST operation response

create_partners POST operation response

Enable AngularJS for your application

StrongLoop supports AngularJS as a User Interface framework, the following steps will show how to update a StrongLoop’s application to enable the use of AngularJS.

Update the server/middleware.json file to allow access to the client directory as if it was the root directory of the application. This will allow the application to serve html files from the client directory. To do so, add the lines listed in bold letters to the files section and rename the file server/boot/root.js file to server/boot/root.js_orig so that it won’t be executed at boot time.

# vi server/middleware.json
…
"files": {
"loopback#static": {
"params": "$!../client"
}
},
…

Create the directory js and subdirectory services:

# mkdir -p client/js/services
#

Enable AngularJS for your application:

# lb-ng server/server.js client/js/services/lb-services.js
Loading LoopBack app "/products/breadbox_api/server/server.js"
Generating "lbServices" for the API endpoint "/api"
Warning: scope User.accessTokens targets class "AccessToken", which is not exposed
via remoting. The Angular code for this scope won't be generated.
Saving the generated services source to "/products/breadbox_api/client/js/services/lb-services.js"
#

The creation of the User Interface is outside the scope of this blog, however the following lines show the directory structure of the application use for the Bread Box Grocery coupon distribution service.

client
client/index.html
client/css
...
client/images
...
client/js
client/js/BBApp_mop.js
...
client/js/angular
...
client/js/bootstrap/
...
client/js/services
client/js/services/lb-services.js
client/js/underscore
...
client/views
...

To access the APIs from the client side add the following lines to the AngularJS controller:

vi client/js/BBApp_mop.js
...
$http.post('/api/PARTNERs/create_partners', {partner_name:partnerName})
.success(function(data) {
console.log(“data: ”, data);
console.log("success");
}).error(function(data) {
console.log("failed");
});
...

To test the application run the following command and point the browser to the URL and port described in the output:
# slc run
INFO strong-agent v2.0.2 profiling app 'breadbox_api' pid '26015'
INFO strong-agent[26015] started profiling agent
INFO supervisor reporting metrics to `internal:`
supervisor running without clustering (unsupervised)
INFO strong-agent not profiling, agent metrics requires a valid license.
Please contact sales@strongloop.com for assistance.
Web server listening at: http://0.0.0.0:3000
Browse your REST API at http://0.0.0.0:3000/explorer


Bread Box Grocery distribution service user interface

Bread Box Grocery distribution service user interface

Now that the application is developed, there are some production consideration to make, namely deployment to a production service, monitoring, tracing, etc. The next blod post will show how Bread Box Grocery can use StrongLoop to deploy the application to a production server.

1 comment on"Exposing Data from DB2 Using StrongLoop Running on Linux on Z (part 2)"

  1. […] is part 3 of the Exposing Data from DB2 Using StrongLoop Running on Linux on Z blog. On part 1 and part 2, the company Bread Box Grocery started working on a Coupon Distribution service. The service […]

Join The Discussion

Your email address will not be published. Required fields are marked *