Skip to content

Commit

Permalink
Merge branch 'models-endpoint' of https://github.com/meshde/lookups-api
Browse files Browse the repository at this point in the history
… into meshde-models-endpoint
  • Loading branch information
callmekatootie committed Apr 21, 2020
2 parents 4cdf234 + b69e298 commit 99083bf
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
24 changes: 24 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,30 @@ paths:
description: Server error
schema:
$ref: '#/definitions/ErrorModel'
'/lookups/devices/models':
get:
tags:
- Device
description: |
List distinct device models.
parameters:
- name: type
in: query
description: Filter by device type, case insensitive, partial match is used
required: false
type: string

responses:
'200':
description: OK
schema:
type: array
items:
type: string
'500':
description: Server error
schema:
$ref: '#/definitions/ErrorModel'
'/lookups/countries':
get:
tags:
Expand Down
2 changes: 1 addition & 1 deletion docs/tc-lookup-api.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -3209,4 +3209,4 @@
}
],
"protocolProfileBehavior": {}
}
}
13 changes: 12 additions & 1 deletion src/controllers/DeviceController.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ async function getManufacturers (req, res) {
res.send(result)
}

/**
* Get distinct device models
* @param {Object} req the request
* @param {Object} res the response
*/
async function getDeviceModels (req, res) {
const result = await service.getDeviceModels(req.query)
res.send(result)
}

module.exports = {
list,
listHead,
Expand All @@ -117,5 +127,6 @@ module.exports = {
partiallyUpdate,
remove,
getTypes,
getManufacturers
getManufacturers,
getDeviceModels
}
7 changes: 7 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ module.exports = {
// any role / scope is allowed
}
},
'/lookups/devices/models': {
get: {
controller: 'DeviceController',
method: 'getDeviceModels'
// any role / scope is allowed
}
},
'/lookups/devices/:id': {
get: {
controller: 'DeviceController',
Expand Down
24 changes: 23 additions & 1 deletion src/services/DeviceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,27 @@ getManufacturers.schema = {
}).required()
}

/**
* Get distinct device models.
* @param {Object} criteria the search criteria
* @returns {Array} the distinct device models
*/
async function getDeviceModels (criteria) {
const result = []
await iterateDevices(criteria, (device) => {
if (!_.includes(result, device.model)) {
result.push(device.model)
}
})
return result
}

getDeviceModels.schema = {
criteria: Joi.object().keys({
type: Joi.string()
})
}

module.exports = {
list,
getEntity,
Expand All @@ -397,7 +418,8 @@ module.exports = {
update,
remove,
getTypes,
getManufacturers
getManufacturers,
getDeviceModels
}

logger.buildService(module.exports)

0 comments on commit 99083bf

Please sign in to comment.