Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
titleService Index without the Composer
linenumberstrue
'use strict';
//require core framework
var soajs = require('soajs');


//require service configuration			
var config = require('./config.js');


//require service business logic (the model is required inside the business logic
var BLModule = require("./lib/index");


//create a new service instance	
var service = new soajs.server.service(config);		

//initialize the service
service.init(function () {		

	//implement first API using method GET
    service.get("/cart/getCart", function (req, res) {		 
		
		//call the initBLModel function ( read more below )
        initBLModel(req, res, function (BL) {
			
			//pass the service configuration and soajs to the business logic			
            BL.getCart(config, req.soajs, function (error, response) {		


				//formulate a standardized response and return it
                return res.json(req.soajs.buildResponse(error, response));	
            });
		});
    });

	//same approach as first API
    service.post("/cart/setCart", function (req, res) {
        initBLModel(req, res, function (BL) {
            BL.setCart(config, req.soajs, function (error, response) {
                return res.json(req.soajs.buildResponse(error, response));
            });
        });
    });

	//same approach as first API
    service.delete("/cart/emptyCart", function (req, res) {
        initBLModel(req, res, function (BL) {
            BL.emptyCart(config, req.soajs, function (error, response) {
                return res.json(req.soajs.buildResponse(error, response));
            });
        });
    });

	//start the service
    service.start();		
});

//function used to read the model name from the inputs provided
//load it in memory and return an instance to it
function initBLModel(req, res, cb) {


	//get the model name from the inputs arriving via the request
    var modelName = req.soajs.inputmaskData.model;		
    
	//call BLModule.init and send the model name to load the later in memory 
	//and return an instance for it via its callback
	BLModule.init(modelName, function (error, BL) {


		//throw an error if model injection failed for some reason
        if (error) {
            req.soajs.log.error(error);		
            return res.json(req.soajs.buildResponse({"code": 407, "msg": config.errors[407]}));  
        }
        else {


			//otherwise return the business logic instance via the callback to use it in the APIs
            return cb(BL);  
        }
    });
}
Code Block
languagejs
titleService Indes Index with the Composer
linenumberstrue
"use strict";
//require the composer
var composer = require("soajs.composer");		


//call composer.deploy to deploy the service from its config file
composer.deploy(__dirname + "/config.js", function(error){


	//optional log message after service is started
    console.log( (error)? error : "Cart Service started ...");	
}); 

...