In the middle of changs to split out separate Cores table, upates on edit screen are broken; interim checkin to view code on bitbucket.

This commit is contained in:
jkaplon 2015-07-30 08:28:16 -04:00
parent a47e0c2fbd
commit 788c446916

View File

@ -98,7 +98,7 @@ app.get('/core/edit/:id', function(req, res){
var coreId = req.params.id; var coreId = req.params.id;
console.log("GET /core/edit/" + coreId + ", " + JSON.stringify(d, 4)); 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) { if(err !== null) {
console.log(err); console.log(err);
} else { } else {
@ -120,23 +120,39 @@ app.post('/core/edit/:id', function(req, res){
if (!req.body) { if (!req.body) {
return res.sendStatus(400); return res.sendStatus(400);
} else { } else {
if (req.body.deviceName !== "") { // check existence of row in Cores table, if there update, otherwise insert.
var stmt = db.prepare("UPDATE Alerts SET coreName = ? WHERE coreId = ?"); var existQry = "SELECT coreId FROM Cores WHERE coreId = ?;"
stmt.run( db.get(existQry, function(err, row){
req.body.deviceName, if(err) throw err;
coreId if(typeof row == "undefined") {
); var insStmt = db.prepare("INSERT INTO Cores (coreId, coreName, locationDesc) VALUES (?, ?, ?);");
stmt.finalize(); 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 !== "") { if (req.body.locationDesc !== "") {
var stmt = db.prepare("UPDATE Alerts SET locationDesc = ? WHERE coreId = ?"); var stmt = db.prepare("UPDATE Cores SET locationDesc = ? WHERE coreId = ?");
stmt.run( stmt.run(
req.body.locationDesc, req.body.locationDesc,
coreId coreId
); );
stmt.finalize(); stmt.finalize();
} }
}
});
res.redirect('https://particle.kaplon.us/'); res.redirect('https://particle.kaplon.us/');
} }