From a85ae2fe316497e51b639925f1e9e6f54a1d690b Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Thu, 25 Feb 2016 19:40:44 +0100 Subject: [PATCH] clean up routes --- nodejs/routes/index.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/nodejs/routes/index.js b/nodejs/routes/index.js index 7b9e8a6..da01013 100644 --- a/nodejs/routes/index.js +++ b/nodejs/routes/index.js @@ -8,7 +8,7 @@ var auth = jwt({secret: config.crypto_secret, userProperty: 'payload'}); var router = express.Router(); // Database models -var rc_db = new models.RCDB(); +var rcdb = new models.RCDB(); var users = new models.Users(); // Arduino communications @@ -23,19 +23,34 @@ router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); }); -router.post('/sendRC', auth, function (req, res, next){ - rc.send(req.body.code); - res.send(req.body); -}); - +// RC Routes router.get('/getRCs', auth, function (req, res, next) { - rc_db.findAll(function (error, results, fields) { + rcdb.findAll(function (error, results, fields) { if(error){ console.log(error); return(next); } res.json(results); }); - }); +router.post('/sendRC', auth, function (req, res, next){ + rc.send(req.body.code); + res.send('Success!'); +}); + +router.post('/addRC', auth, function (req, res, next){ + rcdb.add(req.body.rc, function (error, result) { + if(error) {console.log(error); return(next);} + res.json(result); + }); +}); + +router.post('/deleteRC', auth, function (req, res, next){ + rcdb.delete(req.body.id, function (error, result) { + if(error) {console.log(error); return(next);} + res.json(result); + }); +}); + +// IR Routes router.get('/irTest', auth, function (req, res) { arduino.write(new Buffer('X0' + String.fromCharCode(4) + String.fromCharCode(12) + String.fromCharCode(0)+String.fromCharCode(0)+String.fromCharCode(2689 / 256) + String.fromCharCode(2689 % 256),'ascii')); res.send('switch light');