mirror of
https://github.com/openstf/stf
synced 2025-10-05 02:29:26 +02:00
move devices endpoint from app unit to api unit
This commit is contained in:
parent
0e8b308b6c
commit
d6f37681ce
5 changed files with 115 additions and 50 deletions
61
lib/units/api/controllers/device.js
Normal file
61
lib/units/api/controllers/device.js
Normal file
|
@ -0,0 +1,61 @@
|
|||
var Promise = require('bluebird')
|
||||
|
||||
var dbapi = require('../../../db/api')
|
||||
var logger = require('../../../util/logger')
|
||||
var datautil = require('../../../util/datautil')
|
||||
|
||||
var log = logger.createLogger('api:contoller:device')
|
||||
|
||||
module.exports = {
|
||||
getDevices: getDevices
|
||||
, getDeviceBySerial: getDeviceBySerial
|
||||
};
|
||||
|
||||
function getDevices(req, res) {
|
||||
dbapi.loadDevices()
|
||||
.then(function(cursor) {
|
||||
return Promise.promisify(cursor.toArray, cursor)()
|
||||
.then(function(list) {
|
||||
list.forEach(function(device) {
|
||||
datautil.normalize(device, req.user)
|
||||
})
|
||||
|
||||
res.json({
|
||||
success: true
|
||||
, devices: list
|
||||
})
|
||||
})
|
||||
})
|
||||
.catch(function(err) {
|
||||
log.error('Failed to load device list: ', err.stack)
|
||||
res.json(500, {
|
||||
success: false
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function getDeviceBySerial(req, res) {
|
||||
var serial = req.swagger.params.serial.value
|
||||
|
||||
dbapi.loadDevice(serial)
|
||||
.then(function(device) {
|
||||
if (device) {
|
||||
datautil.normalize(device, req.user)
|
||||
res.json({
|
||||
success: true
|
||||
, device: device
|
||||
})
|
||||
}
|
||||
else {
|
||||
res.json(404, {
|
||||
success: false
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch(function(err) {
|
||||
log.error('Failed to load device "%s": ', req.params.serial, err.stack)
|
||||
res.json(500, {
|
||||
success: false
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue