From d1e94a6d0d6bb70446c7bbd5d69de2c325ccd6f2 Mon Sep 17 00:00:00 2001
From: jkaplon <jody@kaplon.us>
Date: Sun, 19 Jul 2015 09:50:18 -0400
Subject: [PATCH] Show current value (if any) from DB on core-edit.hbs; after
 editing device name or location, redirect back to index.

---
 server.js           | 23 +++++++++++++----------
 views/core-edit.hbs |  6 ++++++
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/server.js b/server.js
index 764c933..8032b5b 100644
--- a/server.js
+++ b/server.js
@@ -88,13 +88,18 @@ app.get('/core/edit/:id', function(req, res){
     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);
-        }
+    db.all("select coreName, locationDesc from Alerts where coreId = ? limit 1;", coreId, function(err, rows){
+    if(err !== null) {
+        console.log(err);
+    } else {
+        res.render('core-edit', {values: rows}, function(err, html) {
+            if(err !== null) {
+                console.log(err);
+            } else {
+                res.send(html);
+            }
+        });
+    }
     });
 });
 
@@ -102,11 +107,9 @@ 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(
@@ -125,7 +128,7 @@ app.post('/core/edit/:id', function(req, res){
             stmt.finalize();
         }
         
-        res.sendStatus(200);  // TODO, change this to return device index page maybe??? at least this is better than hanging on POST.
+        res.redirect('https://particle.kaplon.us/');
     }
 });
 
diff --git a/views/core-edit.hbs b/views/core-edit.hbs
index e2dfd30..f992991 100644
--- a/views/core-edit.hbs
+++ b/views/core-edit.hbs
@@ -6,11 +6,17 @@
         <div class="form-group">
             {{label "deviceName" "Edit Device Name:"}}
             {{input "deviceName" coreName class="form-control"}}
+            {{#if values.0.coreName}}
+                <p>* Current value is, {{values.0.coreName}}</p>
+            {{/if}}
         </div>
 
         <div class="form-group">
             {{label "locationDesc" "Edit Location:"}}
             {{input "locationDesc" locationDesc class="form-control"}}
+            {{#if values.0.locationDesc}}
+                <p>* Current value is, {{values.0.locationDesc}}</p>
+            {{/if}}
         </div>
 
         {{submit "save" "Save details" class="btn btn-primary"}}