From d2a09d89b9acf3225aa16c807004fe38a7339ca3 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 24 Oct 2014 15:32:33 +0200 Subject: [PATCH] Adding events api --- unleash-server/lib/eventApi.js | 10 ++++++++++ unleash-server/lib/eventDb.js | 22 +++++++++++++++++++++- unleash-server/server.js | 2 ++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 unleash-server/lib/eventApi.js diff --git a/unleash-server/lib/eventApi.js b/unleash-server/lib/eventApi.js new file mode 100644 index 0000000000..723a56e753 --- /dev/null +++ b/unleash-server/lib/eventApi.js @@ -0,0 +1,10 @@ +var eventDb = require('./eventDb'); + +module.exports = function (app) { + + app.get('/events', function (req, res) { + eventDb.getEvents().then(function (events) { + res.json({events: events}); + }); + }); +}; \ No newline at end of file diff --git a/unleash-server/lib/eventDb.js b/unleash-server/lib/eventDb.js index 077d647995..f7eff549e0 100644 --- a/unleash-server/lib/eventDb.js +++ b/unleash-server/lib/eventDb.js @@ -13,6 +13,26 @@ function storeEvent(event) { }); } +function getEvents() { + var sql = 'SELECT id, type, created_by as created, data FROM events ORDER BY created_at DESC'; + return new Promise(function (resolve, reject) { + dbPool.query(sql, function(err, res) { + if(err) {reject(err);} + resolve(res.rows.map(mapToEvent)); + }); + }); +} + +function mapToEvent(row) { + return { + id: row.id, + type: row.type, + createdBy: row.created, + data: row.data + }; +} + module.exports = { - store: storeEvent + store: storeEvent, + getEvents: getEvents }; \ No newline at end of file diff --git a/unleash-server/server.js b/unleash-server/server.js index 1b77063120..d195afd923 100644 --- a/unleash-server/server.js +++ b/unleash-server/server.js @@ -3,6 +3,7 @@ var express = require('express'), log4js = require('log4js'), logger = require('./lib/logger'), routes = require('./lib/routes'), + eventApi = require('./lib/eventApi'), featureApi = require('./lib/featureApi'), validator = require('express-validator'), app = express(), @@ -24,6 +25,7 @@ app.set('port', process.env.HTTP_PORT || 4242); app.use(baseUriPath, express.static(__dirname + '/public')); app.use(bodyParser.json({strict: false})); +eventApi(router); featureApi(router); routes(router); app.use(baseUriPath, router);