Skip to content

Commit

Permalink
Add support for type filters
Browse files Browse the repository at this point in the history
  • Loading branch information
meshde committed Apr 5, 2020
1 parent 0656c42 commit b69e298
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,12 @@ paths:
- 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':
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/DeviceController.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async function getManufacturers (req, res) {
* @param {Object} res the response
*/
async function getDeviceModels (req, res) {
const result = await service.getDeviceModels()
const result = await service.getDeviceModels(req.query)
res.send(result)
}

Expand Down
11 changes: 9 additions & 2 deletions src/services/DeviceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,25 @@ getManufacturers.schema = {

/**
* Get distinct device models.
* @param {Object} criteria the search criteria
* @returns {Array} the distinct device models
*/
async function getDeviceModels () {
async function getDeviceModels (criteria) {
const result = []
await iterateDevices({}, (device) => {
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 Down

0 comments on commit b69e298

Please sign in to comment.