From cbf095ead1ca3abff56b7be205a1d920e14a9d5b Mon Sep 17 00:00:00 2001 From: jkaplon Date: Sat, 18 Jul 2015 15:13:10 -0400 Subject: [PATCH] Basic/working version of device name and location editing, relates to issue #16. --- package.json | 1 + server.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++- views/index.hbs | 24 +++++++++++++++-------- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 329b77b..098c9c3 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "body-parser": "^1.12.4", "express": "^4.12.4", "express-hbs": "^0.8.4", + "handlebars-form-helpers": "^0.1.3", "moment-timezone": "^0.4.0", "nodemailer": "^1.3.4", "sqlite3": "^3.0.8" diff --git a/server.js b/server.js index cae306f..764c933 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ var express = require("express"); var hbs = require('express-hbs'); +require('handlebars-form-helpers').register(hbs.handlebars); var nodemailer = require('nodemailer'); var moment = require('moment-timezone'); var fs = require("fs"); @@ -48,7 +49,9 @@ db.serialize(function() { } }); -app.use(bodyParser.json()); +app.use(bodyParser.json()); // Needed for JSON POST requests from Particle Cores. +app.use(bodyParser.urlencoded({ extended: false })); // Needed for web POST requests from edit form. + // Use `.hbs` for extensions and find partials in `views/partials`. app.engine('hbs', hbs.express4({ partialsDir: __dirname + '/views/partials' @@ -80,6 +83,52 @@ app.get('/', function(req, res){ }); }); +app.get('/core/edit/:id', function(req, res){ + var d = new Date(); + var coreId = req.params.id; + console.log("GET /core/edit/" + coreId + ", " + JSON.stringify(d, 4)); + + var rows = {}; + res.render('core-edit', {cores: rows}, function(err, html) { + if(err !== null) { + console.log(err); + } else { + res.send(html); + } + }); +}); + +app.post('/core/edit/:id', function(req, res){ + var d = new Date(); + var coreId = req.params.id; + console.log("POST /core/edit/" + coreId + "body: " + JSON.stringify(req.body) + ", " + JSON.stringify(d, 4)); + // Parse req.body. + if (!req.body) { + return res.sendStatus(400); + } else { + // Update DB + if (req.body.deviceName !== "") { + var stmt = db.prepare("UPDATE Alerts SET coreName = ? WHERE coreId = ?"); + stmt.run( + req.body.deviceName, + coreId + ); + stmt.finalize(); + } + + if (req.body.locationDesc !== "") { + var stmt = db.prepare("UPDATE Alerts SET locationDesc = ? WHERE coreId = ?"); + stmt.run( + req.body.locationDesc, + coreId + ); + stmt.finalize(); + } + + res.sendStatus(200); // TODO, change this to return device index page maybe??? at least this is better than hanging on POST. + } +}); + app.get('/core/:id', function(req, res){ //res.sendFile("/usr/src/app/index.html"); //fs.createReadStream('./log.log').pipe(res); diff --git a/views/index.hbs b/views/index.hbs index 1bcc3b2..9e76d1f 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -14,15 +14,23 @@ {{#each cores}} - - {{#if this.coreName}} - {{this.coreName}} - {{else}} - {{this.coreId}} - {{/if}} - + {{#if this.coreName}} + {{this.coreName}} + {{else}} + # No Name # +
+ Edit Details + {{/if}} + + + {{#if this.locationDesc}} + {{this.locationDesc}} + {{else}} + # No Location # +
+ Edit Details + {{/if}} - {{this.locationDesc}} {{this.MaxPub}} {{this.MaxStatus}}