From 435f85ec3b8513238ec3f182c0f55c8f7cfac5a9 Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Sun, 28 Feb 2016 20:47:25 +0100 Subject: [PATCH] finish wiring up change, delete and add routes --- nodejs/models/rcdb.js | 8 ++++++- nodejs/public/javascripts/angularApp.js | 30 ++++++++++++++++++++----- nodejs/routes/index.js | 9 +++++++- nodejs/views/index.ejs | 3 ++- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/nodejs/models/rcdb.js b/nodejs/models/rcdb.js index 546f79e..17cf313 100644 --- a/nodejs/models/rcdb.js +++ b/nodejs/models/rcdb.js @@ -31,7 +31,13 @@ RC.prototype.delete = function(id, callback) { RC.prototype.add = function(rc, callback) { // var rc = {tristate:bool, on_value:"FF00...", off_value:"FF00...", name:"Switch name"}; - var query = connection.query('INSERT INTO rc_switches SET ?', rc, callback); + connection.query('INSERT INTO rc_switches SET ?', rc, callback); +} + +RC.prototype.change = function(rc, callback) { + // var rc = {tristate:bool, on_value:"FF00...", off_value:"FF00...", name:"Switch name"}; + console.log(rc); + connection.query('UPDATE rc_switches SET ? WHERE `switch_id` = ?', [rc, rc.switch_id], callback); } module.exports = RC; \ No newline at end of file diff --git a/nodejs/public/javascripts/angularApp.js b/nodejs/public/javascripts/angularApp.js index cdb6926..28d2c1e 100644 --- a/nodejs/public/javascripts/angularApp.js +++ b/nodejs/public/javascripts/angularApp.js @@ -130,17 +130,29 @@ app.factory('arduino', ['$http', 'auth', function($http, auth){ headers: {Authorization: 'Bearer '+auth.getToken()} }).success(function (data) { console.log(data); - RC.id = data.insertId; + RC.switch_id = data.insertId; o.RCs.push(RC); }); } o.deleteRC = function (RC) { - var id = RC.id; + var id = RC.switch_id; return $http.post('/deleteRC', {id}, { headers: {Authorization: 'Bearer '+auth.getToken()} }).success(function (data) { - o.RCs.splice(o.RCs.indexOf(RC), 1); + o.RCs.splice(o.RCs.indexOf(o.RCs.filter(function (obj) { + return obj.switch_id == id; + })), 1); + }); + } + + o.changeRC = function (RC) { + return $http.post('/changeRC', RC, { + headers: {Authorization: 'Bearer '+auth.getToken()} + }).success(function (data) { + o.RCs[o.RCs.indexOf(o.RCs.filter(function (obj) { + return obj.switch_id == RC.switch_id; + }))] = RC; }); } @@ -182,13 +194,21 @@ app.controller('MainCtrl', [ }; $scope.addRC = function(){ - console.log($scope.newrc); + arduino.addRC($scope.newrc); $scope.modal.shown = !$scope.modal.shown; $scope.newrc = angular.copy(arduino.emptyrc); } - $scope.deleteRC = function(RC) { + $scope.deleteRC = function() { + arduino.deleteRC($scope.newrc); + $scope.modal.shown = !$scope.modal.shown; + $scope.newrc = angular.copy(arduino.emptyrc); + } + $scope.changeRC = function() { + arduino.changeRC($scope.newrc); + $scope.modal.shown = !$scope.modal.shown; + $scope.newrc = angular.copy(arduino.emptyrc); } }]); diff --git a/nodejs/routes/index.js b/nodejs/routes/index.js index da01013..0945531 100644 --- a/nodejs/routes/index.js +++ b/nodejs/routes/index.js @@ -37,7 +37,7 @@ router.post('/sendRC', auth, function (req, res, next){ }); router.post('/addRC', auth, function (req, res, next){ - rcdb.add(req.body.rc, function (error, result) { + rcdb.add(req.body, function (error, result) { if(error) {console.log(error); return(next);} res.json(result); }); @@ -50,6 +50,13 @@ router.post('/deleteRC', auth, function (req, res, next){ }); }); +router.post('/changeRC', auth, function (req, res, next){ + rcdb.change(req.body, 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')); diff --git a/nodejs/views/index.ejs b/nodejs/views/index.ejs index 8163900..772ffea 100644 --- a/nodejs/views/index.ejs +++ b/nodejs/views/index.ejs @@ -47,7 +47,8 @@ - + +