Skip to content

Commit

Permalink
deviceType.getById & deviceType.getByType
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Oct 3, 2016
2 parents 38fc737 + 08a48ec commit 32c7866
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 3 deletions.
5 changes: 5 additions & 0 deletions api/core/devicetype/deviceType.getById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var queries = require('./deviceType.queries.js');

module.exports = function(device){
return gladys.utils.sqlUnique(queries.getById, [device.id]);
}
5 changes: 5 additions & 0 deletions api/core/devicetype/deviceType.getByType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var queries = require('./deviceType.queries.js');

module.exports = function(device){
return gladys.utils.sql(queries.getByType, [device.type]);
}
35 changes: 34 additions & 1 deletion api/core/devicetype/deviceType.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,38 @@ module.exports = {
`,
getByDeviceAndIdentifier: 'SELECT id FROM devicetype WHERE device = ? AND identifier = ?;',
delete : 'DELETE FROM devicetype WHERE id = ?;',
deleteDeviceStates: 'DELETE FROM devicestate WHERE devicetype = ?;'
deleteDeviceStates: 'DELETE FROM devicestate WHERE devicetype = ?;',
getById: `
SELECT dt.*, ds3.datetime as lastChanged, ds3.value AS lastValue, ds3.id AS lastValueId
FROM device d
JOIN devicetype dt ON (d.id = dt.device)
LEFT JOIN (
SELECT ds.devicetype, MAX(id) as id
FROM devicestate ds
INNER JOIN (
SELECT devicetype, MAX(datetime) as datetime FROM devicestate GROUP BY devicetype
) as dsJoin
WHERE dsJoin.devicetype = ds.devicetype AND dsJoin.datetime = ds.datetime
GROUP by ds.devicetype
) as deviceStateJoin ON (deviceStateJoin.devicetype = dt.id)
LEFT JOIN devicestate ds3 ON deviceStateJoin.id = ds3.id
WHERE dt.id = ?;
`,
getByType: `
SELECT dt.*, ds3.datetime as lastChanged, ds3.value AS lastValue, ds3.id AS lastValueId
FROM device d
JOIN devicetype dt ON (d.id = dt.device)
LEFT JOIN (
SELECT ds.devicetype, MAX(id) as id
FROM devicestate ds
INNER JOIN (
SELECT devicetype, MAX(datetime) as datetime FROM devicestate GROUP BY devicetype
) as dsJoin
WHERE dsJoin.devicetype = ds.devicetype AND dsJoin.datetime = ds.datetime
GROUP by ds.devicetype
) as deviceStateJoin ON (deviceStateJoin.devicetype = dt.id)
LEFT JOIN devicestate ds3 ON deviceStateJoin.id = ds3.id
WHERE dt.type = ?;
`,

};
4 changes: 3 additions & 1 deletion api/core/devicetype/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ module.exports.save = require('./deviceType.save.js');
module.exports.getAll = require('./deviceType.getAll.js');
module.exports.getByRoom = require('./deviceType.getByRoom.js');
module.exports.getByDevice = require('./deviceType.getByDevice.js');
module.exports.getByIdentifier = require('./deviceType.getByIdentifier.js');
module.exports.getByIdentifier = require('./deviceType.getByIdentifier.js');
module.exports.getByType = require('./deviceType.getByType.js');
module.exports.getById = require('./deviceType.getById.js');
26 changes: 26 additions & 0 deletions test/unit/api/core/devicetype/deviceType.getById.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var should = require('should');
var validateDeviceType = require('../../validator/deviceTypeValidator.js');

describe('DeviceType', function() {

describe('getById', function() {

it('should return a deviceType by his id', function (done) {

var device = {
id : 1
};

gladys.deviceType.getById(device)
.then(function(type){
type.should.be.instanceOf(Object);
validateDeviceType(type);
done();
})
.catch(done);

});

});

});
27 changes: 27 additions & 0 deletions test/unit/api/core/devicetype/deviceType.getByType.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var should = require('should');
var validateDeviceType = require('../../validator/deviceTypeValidator.js');

describe('DeviceType', function() {

describe('getByType', function() {

it('should return a list of deviceType by type', function (done) {

var device = {
type: 'binary'
};

gladys.deviceType.getByType(device)
.then(function(types){
validateDeviceType(types);
types.forEach(function(type){
type.type.should.equal(device.type);
});
done();
}).catch(done);

});

});

});
2 changes: 1 addition & 1 deletion test/unit/api/validator/deviceTypeValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ function validateDeviceType(deviceType) {
deviceType.should.have.property('min');
deviceType.should.have.property('max');
deviceType.should.have.property('device');
}
}

0 comments on commit 32c7866

Please sign in to comment.