Source: controllers/Atomic.js

'use strict';

/**
 * @module controller
 * API controller for methods mapping.
 * @license MIT
 * @author Kai KRETSCHMANN <kai@kretschmann.consulting>
 */

const log4js = require("log4js");
const logger = log4js.getLogger();
logger.level = process.env.LOGLEVEL || "warn";

const utils = require('../utils/writer.js');
const feature = require('../utils/feature.js');
const eventEmitter = require('../utils/eventer').em;
const Atomic = require('../service/AtomicService');


/**
 * @method
 * Expose API lorem ipsum.
 * @public
 */
module.exports.getLoremIpsum = function getLoremIpsum(req, res, next) {
    const amount = req.swagger.params['amount'].value;

    if(feature.isEnabled(req, 'apihit')) {
        eventEmitter.emit('apihit', req);
    }

    Atomic.getLoremIpsum(amount)
        .then(function(payload) {
            utils.writeText(res, payload, 200);
        })
        .catch(function(payload) {
            utils.writeText(res, payload, 400);
        });
};

/**
 * @method
 * Expose API uuid.
 * @public
 */
module.exports.getUuid = function getUuid(req, res, next) {
    const amount = req.swagger.params['amount'].value;
    const rtype = req.swagger.params['result'].value;

    if(feature.isEnabled(req, 'apihit')) {
        eventEmitter.emit('apihit', req);
    }

    Atomic.getUuid(amount)
        .then(function(response) {
            utils.writeResult(rtype, res, response);
        })
        .catch(function(response) {
            utils.writeJson(res, response, 400);
        });
};

/**
 * @method
 * Expose API phone.
 * @public
 */
module.exports.getPhone = function getPhone(req, res, next) {
    const amount = req.swagger.params['amount'].value;

    if(feature.isEnabled(req, 'apihit')) {
        eventEmitter.emit('apihit', req);
    }

    Atomic.getPhone(amount)
        .then(function(response) {
            utils.writeJson(res, response);
        })
        .catch(function(response) {
            utils.writeJson(res, response, 400);
        });
};

/**
 * @method
 * Expose API isbn.
 * @public
 */
module.exports.getIsbn = function getIsbn(req, res, next) {
    const amount = req.swagger.params['amount'].value;

    if(feature.isEnabled(req, 'apihit')) {
        eventEmitter.emit('apihit', req);
    }

    Atomic.getIsbn(amount)
        .then(function(response) {
            utils.writeJson(res, response);
        })
        .catch(function(response) {
            utils.writeJson(res, response, 400);
        });
};

/**
 * @method
 * Expose API process names.
 * @public
 */
module.exports.getProcessNames = function getProcessNames(req, res, next) {
    const amount = req.swagger.params['amount'].value;

    if(feature.isEnabled(req, 'apihit')) {
        eventEmitter.emit('apihit', req);
    }

    Atomic.getProcessNames(amount)
        .then(function(response) {
            utils.writeJson(res, response);
        })
        .catch(function(response) {
            utils.writeJson(res, response, 400);
        });
};

/**
 * @method
 * Expose API random numbers.
 * @public
 */
module.exports.getRandomNumbers = function getRandomNumbers(req, res, next) {
    const amount = req.swagger.params['amount'].value;
    const from = req.swagger.params['from'].value;
    const to = req.swagger.params['to'].value;

    if(feature.isEnabled(req, 'apihit')) {
        eventEmitter.emit('apihit', req);
    }

    Atomic.getRandomNumbers(amount, from, to)
        .then(function(response) {
            utils.writeJson(res, response);
        })
        .catch(function(response) {
            utils.writeJson(res, response, 400);
        });
};

/**
 * @method
 * Expose API timestamp.
 * @public
 */
module.exports.getTimestamp = function getTimestamp(req, res, next) {
    const amount = req.swagger.params['amount'].value;
    const from = req.swagger.params['from'].value;
    const to = req.swagger.params['to'].value;

    if(feature.isEnabled(req, 'apihit')) {
        eventEmitter.emit('apihit', req);
    }

    Atomic.getTimestamp(amount, from, to)
        .then(function(response) {
            utils.writeJson(res, response);
        })
        .catch(function(response) {
            utils.writeJson(res, response, 400);
        });
};