summaryrefslogtreecommitdiffstats
path: root/server/api/clients.js
diff options
context:
space:
mode:
authorUdo Walter2019-03-31 19:26:26 +0200
committerUdo Walter2019-03-31 19:26:26 +0200
commit86383d4f5108eef9ef42730150c19311e1637950 (patch)
tree4ae3340c740d05832e0f428c2ea1bf875954212e /server/api/clients.js
parentMerge because Udo committed eslint errors :| (diff)
downloadbas-86383d4f5108eef9ef42730150c19311e1637950.tar.gz
bas-86383d4f5108eef9ef42730150c19311e1637950.tar.xz
bas-86383d4f5108eef9ef42730150c19311e1637950.zip
[server/httpresponse] small rework
Diffstat (limited to 'server/api/clients.js')
-rw-r--r--server/api/clients.js19
1 files changed, 10 insertions, 9 deletions
diff --git a/server/api/clients.js b/server/api/clients.js
index 6cd34a2..32b845d 100644
--- a/server/api/clients.js
+++ b/server/api/clients.js
@@ -6,7 +6,7 @@ const backendHelper = require(path.join(__appdir, 'lib', 'external-backends', 'b
var express = require('express')
const { decorateApp } = require('@awaitjs/express')
var router = decorateApp(express.Router())
-const httpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
+const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
// ############################################################################
// ########################### GET requests #################################
@@ -17,9 +17,10 @@ router.getAsync('', async (req, res) => {
})
router.getAsync('/:id', async (req, res) => {
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const client = await db.client.findOne({ where: { id: req.params.id }, include: ['groups'] })
if (client) res.status(200).send(client)
- else httpResponse.notFound(res, req.params.id)
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################
@@ -27,10 +28,10 @@ router.getAsync('/:id', async (req, res) => {
router.postAsync(['', '/:id'], async (req, res) => {
if (req.query.delete !== undefined && req.query.delete !== 'false') {
- if (!Array.isArray(req.body.ids)) return httpResponse.invalidBodyValue(res, 'ids', 'an array')
+ if (!Array.isArray(req.body.ids)) return HttpResponse.invalidBodyValue('ids', 'an array').send(res)
await backendHelper.deleteClients(req.body.ids)
const count = await db.client.destroy({ where: { id: req.body.ids } })
- httpResponse.successBatch(res, 'deleted', 'client', count)
+ HttpResponse.successBatch('deleted', 'client', count).send(res)
} else {
let client
let action = 'updated'
@@ -40,13 +41,13 @@ router.postAsync(['', '/:id'], async (req, res) => {
io.in('broadcast newClient').emit('notifications newAlert', { type: 'info', text: 'New client!' })
} else if (req.params.id > 0) {
client = await db.client.findOne({ where: { id: req.params.id } })
- if (!client) return httpResponse.notFound(res, req.params.id)
+ if (!client) return HttpResponse.notFound(req.params.id).send(res)
else await client.update(req.body.data)
} else {
- return httpResponse.invalidId(res)
+ return HttpResponse.invalidId().send(res)
}
await client.setGroups(req.body.groupIds)
- httpResponse.success(res, action, 'client', client.id)
+ HttpResponse.success(action, 'client', client.id).send(res)
}
})
@@ -55,8 +56,8 @@ router.postAsync(['', '/:id'], async (req, res) => {
router.delete('/:id', async (req, res) => {
const count = await db.client.destroy({ where: { id: req.params.id } })
- if (count) httpResponse.success(res, 'deleted', 'client', req.params.id)
- else httpResponse.notFound(res, req.params.id)
+ if (count) HttpResponse.success('deleted', 'client', req.params.id).send(res)
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################