From 803148d5a23afe207fb5de9ac73c986a324feb9c Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 31 Oct 2015 14:09:39 +0100 Subject: core: o Improved transaction handling. web: o Supporting changing a device's name and description. --- src/DillerDao.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'src/DillerDao.js') diff --git a/src/DillerDao.js b/src/DillerDao.js index 2133dfc..a47f181 100644 --- a/src/DillerDao.js +++ b/src/DillerDao.js @@ -2,7 +2,7 @@ var _ = require('lodash'); function DillerDao(tx) { - var deviceColumns = 'id, key, created_timestamp'; + var deviceColumns = 'id, created_timestamp, key, name, description'; var propertyColumns = 'id, device, key, created_timestamp'; var valueColumns = 'property, timestamp, value_text, value_numeric'; @@ -27,27 +27,25 @@ function DillerDao(tx) { } function updateDevice(id, attributes) { - var values = [id]; var i = 2; - var fields = _.map(attributes, function (value, name) { - console.log('name', name, 'value', value, 'i', i); - if (name == 'name') { - values.push(value); - return 'name = $' + i++; - } - }); + var fields = _(attributes).chain() + .map(function (value, attribute) { + if (attribute == 'name' || attribute == 'description') { + values.push(value); + return attribute + ' = $' + i++; + } + }) + .collect() + .join(', ') + .value(); if (fields.length == 0) { - return; // TODO: return an empty promise; + return Promise.resolve({}); } - fields = _.collect(fields); - - var x = 'UPDATE device SET ' + fields.join(', ') + ' WHERE id = $1'; - console.log('x', x); - console.log('values', values); - return tx.none(x, values); + var sql = 'UPDATE device SET ' + fields + ' WHERE id = $1'; + return tx.none(sql, values); } // ------------------------------------------------------------------------------------------------------------------- -- cgit v1.2.3