From 02d6e77bd180cbbf6f7f6e1a69c670e922d8204d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 31 Oct 2015 12:04:28 +0100 Subject: web: o Starting on edit button for device name. --- src/web/DillerWeb.js | 76 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 13 deletions(-) (limited to 'src/web/DillerWeb.js') diff --git a/src/web/DillerWeb.js b/src/web/DillerWeb.js index c261af7..95bcbb6 100644 --- a/src/web/DillerWeb.js +++ b/src/web/DillerWeb.js @@ -14,16 +14,26 @@ function DillerWeb(diller, db, config) { var calls = []; var app; + function genericErrorHandler(res) { + return function (data) { + log.warn('fail', data); + return res.status(500).json({message: 'fail', data: data}) + } + } + function getDevices(req, res) { db().tx(function (pg) { var dao = new DillerDao(pg); return dao.devices(); }).then(function (devices) { res.json({devices: devices}); - }, function (err) { - log.warn('fail', err); - res.status(500).json({message: 'fail'}); - }); + }, genericErrorHandler(res)); + } + + function deviceResponse(data) { + var device = data[0]; + device.properties = data[1]; + return {device: device}; } function getDevice(req, res) { @@ -35,14 +45,37 @@ function DillerWeb(diller, db, config) { dao.deviceById(deviceId), dao.devicePropertiesByDeviceId(deviceId)] ); + }).then(function (data) { + res.json(deviceResponse(data)); + }, genericErrorHandler(res)); + } + + function patchDevice(req, res) { + db().tx(function (tx) { + var deviceId = req.params.deviceId; + + var body = req.body; + + if (body.attribute == 'name') { + diller.updateDeviceName(deviceId, body.value) + .then(function () { + var dao = new DillerDao(tx); + return tx.batch([ + dao.deviceById(deviceId), + dao.devicePropertiesByDeviceId(deviceId)] + ); + }) + .then(function (data) { + res.json(deviceResponse(data)); + }, genericErrorHandler(res)); + } else { + res.status(400).json({message: 'Required keys: "attribute" and "value".'}); + } }).then(function (data) { var device = data[0]; device.properties = data[1]; res.json({device: device}); - }, function (err) { - log.warn('fail', err); - res.status(500).json({message: 'fail'}); - }); + }, genericErrorHandler(res)); } function getValues(req, res) { @@ -76,19 +109,34 @@ function DillerWeb(diller, db, config) { api[method](path, callback); var layer = _.last(api.stack); + var methodWithPayloads = { + put: true, + post: true, + patch: true + }; + + var keys = _.map(layer.keys, function (key) { + return key.name; + }); + + var hasPayload = methodWithPayloads[method]; + if (hasPayload) { + keys.push('payload'); + } + calls.push({ name: name, method: method, path: path, layer: layer, - keys: _.map(layer.keys, function (key) { - return key.name; - }) + hasPayload: hasPayload, + keys: keys }); } addApi('getDevices', 'get', '/device', getDevices); addApi('getDevice', 'get', '/device/:deviceId', getDevice); + addApi('patchDevice', 'patch', '/device/:deviceId', patchDevice); addApi('getValues', 'get', '/property/:propertyId/values', getValues); var templates = express.Router(); @@ -116,8 +164,6 @@ function DillerWeb(diller, db, config) { s += _.map(calls, function (call) { - //console.error(call); - //console.error('call.layer', call.layer); var s = ' function ' + call.name + '(' + call.keys.join(', ') + ') {\n' + ' var req = {};\n' + ' req.method = \'' + call.method + '\';\n' + @@ -127,6 +173,10 @@ function DillerWeb(diller, db, config) { return ' req.url = req.url.replace(/:' + key.name + '/, ' + key.name + ');\n' }).join('\n'); + if (call.hasPayload) { + s += ' req.data = payload;\n'; + } + s += ' return $http(req);\n' + ' }\n'; -- cgit v1.2.3