forked from Ivonhoe/stf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move devices endpoint from app unit to api unit
- Loading branch information
Showing
5 changed files
with
115 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ info: | |
contact: | ||
url: http://openstf.io/ | ||
email: [email protected] | ||
basePath: /api/v1/ | ||
basePath: /api/v1 | ||
schemes: | ||
- http | ||
- https | ||
|
@@ -33,6 +33,42 @@ paths: | |
description: Unexpected Error | ||
schema: | ||
$ref: "#/definitions/ErrorResponse" | ||
/devices: | ||
x-swagger-router-controller: device | ||
get: | ||
summary: Device List | ||
description: List of all the STF devices including Disconnected and Offline | ||
operationId: getDevices | ||
responses: | ||
"200": | ||
description: List of Devices | ||
schema: | ||
$ref: "#/definitions/DeviceListResponse" | ||
default: | ||
description: Unexpected Error | ||
schema: | ||
$ref: "#/definitions/ErrorResponse" | ||
/devices/{serial}: | ||
x-swagger-router-controller: device | ||
get: | ||
summary: Device Information | ||
description: Device Information | ||
operationId: getDeviceBySerial | ||
parameters: | ||
- name: serial | ||
in: path | ||
description: Device Serial | ||
required: true | ||
type: string | ||
responses: | ||
"200": | ||
description: Device Information | ||
schema: | ||
$ref: "#/definitions/DeviceResponse" | ||
default: | ||
description: Unexpected Error | ||
schema: | ||
$ref: "#/definitions/ErrorResponse" | ||
|
||
definitions: | ||
UserResponse: | ||
|
@@ -41,6 +77,20 @@ definitions: | |
properties: | ||
user: | ||
type: object | ||
DeviceListResponse: | ||
required: | ||
- devices | ||
properties: | ||
devices: | ||
type: array | ||
items: | ||
type: object | ||
DeviceResponse: | ||
required: | ||
- device | ||
properties: | ||
device: | ||
type: object | ||
ErrorResponse: | ||
required: | ||
- message | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters