From 360c1bc37a1b8d0de259810b278054e5e5d32666 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 14 Nov 2014 07:13:08 +0100 Subject: [PATCH] Adding events/:name --- lib/eventApi.js | 12 ++++++++++++ lib/eventDb.js | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/eventApi.js b/lib/eventApi.js index 723a56e753..232f5adf3e 100644 --- a/lib/eventApi.js +++ b/lib/eventApi.js @@ -7,4 +7,16 @@ module.exports = function (app) { res.json({events: events}); }); }); + + app.get('/events/:name', function (req, res) { + eventDb.getEvents(req.params.name).then(function (events) { + if (events) { + res.json(events); + } else { + res.status(404).json({error: 'Could not find events'}); + } + }); + }); + + }; \ No newline at end of file diff --git a/lib/eventDb.js b/lib/eventDb.js index f7eff549e0..6fcb3700d0 100644 --- a/lib/eventDb.js +++ b/lib/eventDb.js @@ -23,6 +23,18 @@ function getEvents() { }); } +function getEvents(name) { + var sql = 'SELECT id, type, created_by as created, data ' + + 'FROM events WHERE data ->> \'name\' = $1 ' + + 'ORDER BY created_at DESC'; + return new Promise(function (resolve, reject) { + dbPool.query(sql, [name], function(err, res) { + if(err) {reject(err);} + resolve(res.rows.map(mapToEvent)); + }); + }); +} + function mapToEvent(row) { return { id: row.id,