aboutsummaryrefslogtreecommitdiff
path: root/src/web
diff options
context:
space:
mode:
Diffstat (limited to 'src/web')
-rw-r--r--src/web/DillerWeb.js56
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;