From 788c4469164e5bbd1a24dc2df41b8e923f46ae6c Mon Sep 17 00:00:00 2001 From: jkaplon Date: Thu, 30 Jul 2015 08:28:16 -0400 Subject: [PATCH] In the middle of changs to split out separate Cores table, upates on edit screen are broken; interim checkin to view code on bitbucket. --- server.js | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/server.js b/server.js index 48f23c3..15f4982 100644 --- a/server.js +++ b/server.js @@ -98,7 +98,7 @@ app.get('/core/edit/:id', function(req, res){ var coreId = req.params.id; console.log("GET /core/edit/" + coreId + ", " + JSON.stringify(d, 4)); - db.all("select max(coreName), max(locationDesc) from Alerts where coreId = ? group by coreId;", coreId, function(err, rows){ + db.all("select coreName, locationDesc from Cores where coreId = ?;", coreId, function(err, rows){ if(err !== null) { console.log(err); } else { @@ -120,23 +120,39 @@ app.post('/core/edit/:id', function(req, res){ if (!req.body) { return res.sendStatus(400); } else { - if (req.body.deviceName !== "") { - var stmt = db.prepare("UPDATE Alerts SET coreName = ? WHERE coreId = ?"); - stmt.run( - req.body.deviceName, - coreId - ); - stmt.finalize(); - } + // check existence of row in Cores table, if there update, otherwise insert. + var existQry = "SELECT coreId FROM Cores WHERE coreId = ?;" + db.get(existQry, function(err, row){ + if(err) throw err; + if(typeof row == "undefined") { + var insStmt = db.prepare("INSERT INTO Cores (coreId, coreName, locationDesc) VALUES (?, ?, ?);"); + insStmt.run( + coreId, + req.body.deviceName, + req.body.locationDesc + ); + insStmt.finalize(); + } else { + if (req.body.deviceName !== "") { + var stmt = db.prepare("UPDATE Cores 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(); - } + if (req.body.locationDesc !== "") { + var stmt = db.prepare("UPDATE Cores SET locationDesc = ? WHERE coreId = ?"); + stmt.run( + req.body.locationDesc, + coreId + ); + stmt.finalize(); + } + } + }); res.redirect('https://particle.kaplon.us/'); }