From f7ff8a837bed336a14aa0442ed8286f3ef78f577 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 1 Nov 2015 01:06:40 +0100 Subject: core: o Adding updates of device property's name and description. web: o Ading editing of property name and description. --- src/DillerDao.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src/DillerDao.js') diff --git a/src/DillerDao.js b/src/DillerDao.js index e7f6b54..63ad3c1 100644 --- a/src/DillerDao.js +++ b/src/DillerDao.js @@ -69,12 +69,27 @@ function DillerDao(tx) { return tx.one('INSERT INTO device_property(id, device, key, created_timestamp) VALUES(DEFAULT, $1, $2, CURRENT_TIMESTAMP) RETURNING ' + propertyColumns, [deviceId, key]); } - function updatePropertyName(id, name) { - return tx.none('UPDATE device_property SET name=$1 WHERE id=$2', [name, id]); - } + function updateProperty(id, attributes) { + var values = [id]; + var i = 2; + var fields = _(attributes).chain() + .map(function (value, attribute) { + if (attribute == 'name' || attribute == 'description') { + value = (value || '').trim(); + values.push(value.length > 0 ? value : null); + return attribute + ' = $' + i++; + } + }) + .collect() + .join(', ') + .value(); + + if (fields.length == 0) { + return Promise.resolve({}); + } - function updatePropertyDescription(id, description) { - return tx.none('UPDATE device_property SET description=$1 WHERE id=$2', description, id); + var sql = 'UPDATE device_property SET ' + fields + ' WHERE id = $1 RETURNING *'; + return tx.one(sql, values); } // ------------------------------------------------------------------------------------------------------------------- @@ -130,8 +145,7 @@ function DillerDao(tx) { devicePropertyByDeviceIdAndKey: devicePropertyByDeviceIdAndKey, devicePropertiesByDeviceId: devicePropertiesByDeviceId, insertDeviceProperty: insertDeviceProperty, - updatePropertyName: updatePropertyName, - updatePropertyDescription: updatePropertyDescription, + updateProperty: updateProperty, valuesByPropertyId: valuesByPropertyId, insertValue: insertValue, -- cgit v1.2.3