From 0277ae81ee083dc2e317bd45f58de5878f0761e3 Mon Sep 17 00:00:00 2001 From: jkaplon Date: Tue, 23 Aug 2016 17:08:23 -0400 Subject: [PATCH] admin template working; error on handling POST though. --- server.js | 50 ++++++++++++++++++++++++++++++++++++++++++++----- views/admin.hbs | 24 +++++++++++++++--------- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/server.js b/server.js index b769278..55801cd 100644 --- a/server.js +++ b/server.js @@ -118,6 +118,12 @@ app.get('/', function(req, res){ }); }); +app.post(':loc/admin', isAuthenticated, function(req, res){ + winston.info('POST by admin at ' + loc); + // possible to forward req to POST to '/'? + var loc = req.params.loc; +}); + app.post('/', function(req, res){ var postEvent = req.body.postEvent; var source = req.body.source; @@ -171,14 +177,48 @@ app.get('/:loc/admin', isAuthenticated, function(req, res) { res.status(404).send('Not found'); } else { // TODO: load admin template - res.status(200).send('admin template here...'); + //res.status(200).send('admin template here...'); + pg.connect(conString, function(err, client, done) { + if(err) { + return winston.error('error fetching client from pool', err); + } + var devIndexQry = + "select status, published_at " + + "from alerts " + + "where status in ('Open', 'Closed') " + + "and coreid = '2a002b000947343432313031' " + + "order by published_at desc " + + "limit 2" + client.query(devIndexQry, function(err, result) { + //call `done()` to release the client back to the pool + done(); + if(err) { + return winston.error('error running query', err); + } + // Loop over elements in rows array, convert ugly UTC times to pretty local times. + result.rows.forEach(function(row){ + row.pubdate = courtsopenUtils.getLocDateFromUTC(row.published_at); + row.pubtime = courtsopenUtils.getLocTimeFromUTC(row.published_at); + if(row.status.toLowerCase().indexOf('closed') > -1){ + row.statusclass = 'closed'; + row.oppstatus = 'open'; + } else { + row.statusclass = 'open'; + row.oppstatus = 'closed'; + } + }); + res.render('admin', {values: result.rows}, function(err, html) { + if(err !== null) { + winston.error(err); + } else { + res.send(html); + } + }); + }); + }); } }); -app.post(':loc/admin', isAuthenticated, function(req, res){ - winston.info('POST by admin at ' + loc); - // possible to forward req to POST to '/'? -}); app.get('/:loc/login', function(req, res){ var loc = req.params.loc; diff --git a/views/admin.hbs b/views/admin.hbs index f13f26f..ef0d2d5 100644 --- a/views/admin.hbs +++ b/views/admin.hbs @@ -4,17 +4,23 @@

TT Tennis Courts Are...

- - - - - - -
Location: Current Status:
Temple Terrace Recreation Center, clay courts{{values.status}}
+{{! Index-0 is the most recent message. }} +
+

{{values.0.status}}

+
+
+

Update received: {{values.0.pubdate}}, {{values.0.pubtime}}

+
+
+
+

Prior update was: {{values.1.status}} at {{values.1.pubdate}}, {{values.1.pubtime}}

+
+ +
- - + +