diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-10-31 14:09:39 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-10-31 14:09:39 +0100 |
commit | 803148d5a23afe207fb5de9ac73c986a324feb9c (patch) | |
tree | b887cede3f56e4e24c05dd9640dbd01afc2b9367 /src/web | |
parent | 02d6e77bd180cbbf6f7f6e1a69c670e922d8204d (diff) | |
download | diller-server-803148d5a23afe207fb5de9ac73c986a324feb9c.tar.gz diller-server-803148d5a23afe207fb5de9ac73c986a324feb9c.tar.bz2 diller-server-803148d5a23afe207fb5de9ac73c986a324feb9c.tar.xz diller-server-803148d5a23afe207fb5de9ac73c986a324feb9c.zip |
core:
o Improved transaction handling.
web:
o Supporting changing a device's name and description.
Diffstat (limited to 'src/web')
-rw-r--r-- | src/web/DillerWeb.js | 56 |
1 files changed, 30 insertions, 26 deletions
diff --git a/src/web/DillerWeb.js b/src/web/DillerWeb.js index 95bcbb6..bcb15d4 100644 --- a/src/web/DillerWeb.js +++ b/src/web/DillerWeb.js @@ -4,11 +4,15 @@ var _ = require('lodash'); var di = require('di'); var DillerConfig = require('../DillerConfig'); -var Diller = require('../Diller'); -var DillerDb = require('../DillerDb'); -var DillerDao = require('../DillerDao'); - -function DillerWeb(diller, db, config) { +var DillerTx = require('../DillerTx'); + +/** + * @param tx DillerTx + * @param config DillerConfig + * @returns {{init: init, listen: listen, generateRpc: generateRpc}} + * @constructor + */ +function DillerWeb(tx, config) { var log = config.log(); var calls = []; @@ -21,27 +25,25 @@ function DillerWeb(diller, db, config) { } } + function deviceResponse(data) { + var device = data[0]; + device.properties = data[1]; + return {device: device}; + } + function getDevices(req, res) { - db().tx(function (pg) { - var dao = new DillerDao(pg); + tx(function (pg, dao) { return dao.devices(); }).then(function (devices) { res.json({devices: devices}); }, genericErrorHandler(res)); } - function deviceResponse(data) { - var device = data[0]; - device.properties = data[1]; - return {device: device}; - } - function getDevice(req, res) { - db().tx(function (tx) { + tx(function (pg, dao) { var deviceId = req.params.deviceId; - var dao = new DillerDao(tx); - return tx.batch([ + return pg.batch([ dao.deviceById(deviceId), dao.devicePropertiesByDeviceId(deviceId)] ); @@ -51,16 +53,20 @@ function DillerWeb(diller, db, config) { } function patchDevice(req, res) { - db().tx(function (tx) { + tx(function (pg, dao, diller) { var deviceId = req.params.deviceId; var body = req.body; - if (body.attribute == 'name') { - diller.updateDeviceName(deviceId, body.value) + if (!body.attribute) { + res.status(400).json({message: 'Required keys: "attribute" and "value".'}); + } else if (body.attribute == 'name' || body.attribute == 'description') { + var attributes = {}; + attributes[body.attribute] = body.value; + + return diller.updateDeviceAttributes(deviceId, attributes) .then(function () { - var dao = new DillerDao(tx); - return tx.batch([ + return pg.batch([ dao.deviceById(deviceId), dao.devicePropertiesByDeviceId(deviceId)] ); @@ -69,7 +75,7 @@ function DillerWeb(diller, db, config) { res.json(deviceResponse(data)); }, genericErrorHandler(res)); } else { - res.status(400).json({message: 'Required keys: "attribute" and "value".'}); + res.status(400).json({message: 'Unsupported attribute: ' + body.attribute}); } }).then(function (data) { var device = data[0]; @@ -79,10 +85,8 @@ function DillerWeb(diller, db, config) { } function getValues(req, res) { - db().tx(function (tx) { + tx(function (tx, dao) { var propertyId = req.params.propertyId; - - var dao = new DillerDao(tx); return dao.valuesByPropertyId(propertyId, 10); }).then(function (values) { res.json({values: values}); @@ -221,6 +225,6 @@ function DillerWeb(diller, db, config) { } } -di.annotate(DillerWeb, new di.Inject(Diller, DillerDb, DillerConfig)); +di.annotate(DillerWeb, new di.Inject(DillerTx, DillerConfig)); module.exports = DillerWeb; |