<repo_name>FPizzetti/jqueue-rest-api<file_sep>/controllers/database.controller.js
'use strict';

var config = require('../config/env.config');

function list(req, res) {
    var databases = [];
    for (var i in config.databases) {
        if (config.databases.hasOwnProperty(i)) {
            var database = config.databases[i];
            var cloned = Object.assign({configName: i}, database);
            delete cloned.password;
            databases.push(cloned);
        }
    }
    res.send(200, databases);
}

function getByName(req, res) {
    var database = config.databases[req.params.db];
    if (!database) {
        res.send(404, {message: 'database not found'});
    } else {
        var cloned = Object.assign({configName: req.params.db}, database);
        delete cloned.password;
        res.send(200, cloned);
    }
}

module.exports = {
    list: list,
    getByName: getByName
};<file_sep>/middlewares/jqueue.middleware.js
'use strict';

var Jqueue = require('jqueue');
var mysql = require('mysql');
var databases = require('../config/env.config').databases;
var jqueueBuffer = {};
var jqueueBufferSize = 10;

module.exports = function (req, res, next) {

    if (req.params && req.params.db) {
        var dbName = req.params.db;
        var jqueue = jqueueBuffer[dbName];
        if (jqueue) {
            req.jqueue = jqueue.object;
            req.dataSource = jqueue.dataSource;
            next();
        } else {
            var database = databases[dbName];
            if (database) {
                var dataSource = mysql.createPool(database);
                jqueue = new Jqueue(dataSource);
                var bufferKeys = Object.keys(jqueueBuffer);
                if (bufferKeys.length >= jqueueBufferSize) {
                    delete jqueueBuffer[bufferKeys[0]];
                }
                jqueueBuffer[dbName] = {object: jqueue, dataSource: dataSource};
                req.jqueue = jqueue;
                req.dataSource = dataSource;
                next();
            } else {
                res.send(404, {message: 'database ' + dbName + ' not found'});
            }
        }
    } else {
        res.send(400, {message: 'missing database'});
    }

};<file_sep>/middlewares/queue.middleware.js
'use strict';

module.exports = function (req, res, next) {

    if (req.params && req.params.queue) {
        var queueName = req.params.queue;
        req.jqueue.use(queueName, true, function (error, queue) {
            if (!error) {
                req.queue = queue;
                next();
            } else {
                if (error.message.match(/ER_NO_SUCH_TABLE/)) {
                    res.send(404, {message: 'queue ' + queueName + ' not found'});
                } else {
                    res.send(500, error);
                }
            }
        });
    } else {
        res.send(400, {message: 'missing queue'});
    }

};<file_sep>/index.js
'use strict';
var restify = require('restify'),
    routes = require('./config/routes.config'),
    env = require('./config/env.config');

var server = restify.createServer();
server.use(restify.bodyParser());
server.use(restify.queryParser());

routes(server);

server.listen(env.port, function () {
    console.log('jqueue-rest-api listening at port %s', env.port);
});<file_sep>/config/env.config.js
'use strict';
module.exports = {
    port: 8000,
    databases: {
        devdb: {
            host: '192.168.100.121',
            user: 'pdg',
            password: '<PASSWORD>',
            database: 'jqueue'
        }
    }
};<file_sep>/daos/message.dao.js
'use strict';

function update(cb, connection, message) {
    connection.query('UPDATE ?? SET status = ? WHERE id = ?', [message.queue, message.status, message.id], function (error) {
        cb(error);
    });
}

function massiveUpdate(cb, connection, sql) {
    console.log('UPDATE sql:', sql);
    connection.query(sql.query, sql.whereParams, function (error) {
        console.log('err', error);
        cb(error);
    });
}

function massiveDelete(cb, connection, sql) {
    console.log('delete sql', sql);
    connection.query(sql.query, sql.whereParams, function (error) {
        console.log('err', error);
        cb(error);
    });
}

function listByQueue(cb, connection, sql) {
    var messageList = [];
    console.log('sql', sql);

    connection.query(sql.query, sql.whereParams, function (error, rows, fields) {
        if (!error) {

            if (rows.length) {
                for (var ix in rows) {
                    messageList.push(rows[ix]);
                }
            }
        }
        else {
            console.log('err', error);
        }
        cb(error, messageList);
    });
}

function deleteQueue(cb, connection, queue, id) {
    connection.query('DELETE FROM ?? WHERE id = ?', [queue, id], function (error) {
        cb(error);
    });
}

function getById(cb, connection, queue, id) {
    connection.query('SELECT * FROM ?? WHERE id = ?', [queue, id], function (error, rows) {
        if (error) {
            cb(error);
        } else {
            var result = null;
            if (rows.length) {
                result = rows[0];
            }
            cb(error, result);
        }
    });
}

module.exports = {
    update: update,
    getById: getById,
    deleteQueue: deleteQueue,
    listByQueue: listByQueue,
    massiveUpdate: massiveUpdate,
    massiveDelete: massiveDelete
};<file_sep>/controllers/queue.controller.js
'use strict';

var queueDao = require('../daos/queue.dao');
var log = require('../services/log.service');
var Jqueue = require('jqueue');

function list(req, res) {

    log.trace('listing queues');

    queueDao.list(function (err, list) {
        if (!err) {
            res.send(200, list);
        } else {
            log.error('error listing queues', err);
            res.send(500);
        }
    }, req.dataSource);


}

function getByName(req, res) {

    queueDao.queueTableInfo(function (err, result) {
        if (!err) {
            if (result) {
                res.send(200, result);
            } else {
                res.send(404, {message: 'queue ' + req.queue.getName() + ' not found'});
            }
        } else {
            log.error('error retrieving queue', err);
            res.send(500);
        }
    }, req.dataSource, req.queue);

}

function create(req, res) {
    if (req.params && req.params.queue) {
        var queueName = req.params.queue;
        var ephemeral = req.body && req.body.ephemeral ? req.body.ephemeral : false;
        var jqueue = new Jqueue(req.dataSource);
        jqueue.use(queueName, false, ephemeral, function (err, rows, fields) {
            if (err) {
                res.send(500, err);
            } else {
                res.send(201);
            }
        });
    }
}

function deleteByName(req, res, next) {

    if (req.params && req.params.queue) {
        queueDao.dropQueue(function (err) {
            if (!err) {
                if (req.method === 'PUT') {
                    next();
                } else {
                    res.send(200);
                }
            } else {
                if (err.message.match(/ER_BAD_TABLE_ERROR/)) {
                    if (req.method === 'PUT') {
                        next();
                    } else {
                        res.send(200);
                    }
                } else {
                    log.error('error dropping queue', err);
                    res.send(500);
                }
            }
        }, req.dataSource, req.params.queue);
    } else {
        res.send(400, {message: 'missing queue'});
    }

}

module.exports = {
    list: list,
    getByName: getByName,
    create: create,
    deleteByName: deleteByName
};