Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rest API for update Device note #222

Merged
merged 3 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/units/api/controllers/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,32 @@ function removeOriginGroupDevice(req, res) {
apiutil.redirectApiWrapper('serial', removeOriginGroupDevices, req, res)
}

function putDeviceInfoBySerial(req, res) {
const serial = req.swagger.params.serial.value
const body = req.swagger.params.device.value.device
dbapi.loadDeviceBySerial(serial)
.then((data) => {
if (!data) {
return apiutil.respond(res, 404, `Not Found (${serial})`)
}
var updates = []
// Update fields based on given body
if (_.has(body, 'note')) {
updates.push(dbapi.setDeviceNote(serial, body.note))
}
if (updates.length === 0) {
return apiutil.respond(res, 400, 'No content to update')
}
return Promise.all(updates)
.then(function() {
apiutil.respond(res, 200)
})
})
.catch(function(err) {
apiutil.internalError(res, 'Failed to update device: ', err.stack)
})
}

function deleteDevices(req, res) {
const serials = apiutil.getBodyParameter(req.body, 'serials')
const target = apiutil.getQueryParameter(req.swagger.params.redirected) ? 'device' : 'devices'
Expand Down Expand Up @@ -509,12 +535,17 @@ function deleteDevices(req, res) {
})
}

function putDeviceBySerial(req, res) {
apiutil.redirectApiWrapper('serial', putDeviceInfoBySerial, req, res)
}

function deleteDevice(req, res) {
apiutil.redirectApiWrapper('serial', deleteDevices, req, res)
}

module.exports = {
getDevices: getDevices
, putDeviceBySerial: putDeviceBySerial
, getDeviceBySerial: getDeviceBySerial
, getDeviceGroups: getDeviceGroups
, getDeviceBookings: getDeviceBookings
Expand Down
45 changes: 45 additions & 0 deletions lib/units/api/swagger/api_v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,45 @@ paths:
$ref: "#/definitions/UnexpectedErrorResponse"
security:
- accessTokenAuth: []
put:
summary: Adds device informatin
description: Adds device information
operationId: putDeviceBySerial
consumes:
- application/json
tags:
- admin
parameters:
- name: serial
in: path
description: Device identifier (serial)
required: true
type: string
- name: device
in: body
description: >
Information to add for device. Supports only notes -field.
required: true
schema:
$ref: "#/definitions/DevicePayload"
responses:
"200":
description: Storing success
schema:
$ref: "#/definitions/Response"
default:
description: >
Unexpected Error:
* 400: Bad Request => invalid request
* 401: Unauthorized => bad credentials
* 404: Not Found => unknown device
* 500: Internal Server Error
* 503: Service Unavailable => server too busy or a lock on a resource is pending
* 504: Gateway Time-out => server is not responding
schema:
$ref: "#/definitions/UnexpectedErrorResponse"
security:
- accessTokenAuth: [ ]
/devices/groups/{id}:
x-swagger-router-controller: devices
put:
Expand Down Expand Up @@ -2380,6 +2419,12 @@ definitions:
emails:
description: Comma-separated list of emails
type: string
DevicePayload:
description: payload object for adding device information
properties:
note:
description: Device Note
type: string
DevicesPayload:
description: Payload object for adding/removing devices
properties:
Expand Down