Basic/working version of device name and location editing, relates to issue #16.
This commit is contained in:
		
							parent
							
								
									9915256f77
								
							
						
					
					
						commit
						cbf095ead1
					
				@ -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"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										51
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								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);
 | 
			
		||||
 | 
			
		||||
@ -14,15 +14,23 @@
 | 
			
		||||
        {{#each cores}}
 | 
			
		||||
            <tr>
 | 
			
		||||
                <td>
 | 
			
		||||
                    <a href="https://particle.kaplon.us/core/{{this.coreId}}">
 | 
			
		||||
                        {{#if this.coreName}}
 | 
			
		||||
                            {{this.coreName}}                        
 | 
			
		||||
                        {{else}}
 | 
			
		||||
                            {{this.coreId}}
 | 
			
		||||
                        {{/if}}
 | 
			
		||||
                    </a>
 | 
			
		||||
                    {{#if this.coreName}}
 | 
			
		||||
                        <a href="https://particle.kaplon.us/core/{{this.coreId}}">{{this.coreName}}</a>
 | 
			
		||||
                    {{else}}
 | 
			
		||||
                        <a href="https://particle.kaplon.us/core/{{this.coreId}}"># No Name #</a>
 | 
			
		||||
                        </br>
 | 
			
		||||
                        <a href="https://particle.kaplon.us/core/edit/{{this.coreId}}" class="btn btn-primary">Edit Details</a>
 | 
			
		||||
                    {{/if}}
 | 
			
		||||
                </td>
 | 
			
		||||
                <td>
 | 
			
		||||
                    {{#if this.locationDesc}}
 | 
			
		||||
                        {{this.locationDesc}}
 | 
			
		||||
                    {{else}}
 | 
			
		||||
                        # No Location #
 | 
			
		||||
                        </br>
 | 
			
		||||
                        <a href="https://particle.kaplon.us/core/edit/{{this.coreId}}" class="btn btn-primary">Edit Details</a>
 | 
			
		||||
                    {{/if}}
 | 
			
		||||
                </td>
 | 
			
		||||
                <td>{{this.locationDesc}}</td>
 | 
			
		||||
                <td>{{this.MaxPub}}</td>
 | 
			
		||||
                <td>{{this.MaxStatus}}</td>
 | 
			
		||||
            </tr>
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user