summaryrefslogtreecommitdiffstats
path: root/server/api/clients.js
diff options
context:
space:
mode:
authorUdo Walter2019-03-31 05:07:33 +0200
committerUdo Walter2019-03-31 05:07:33 +0200
commit530bc42e4769f40d057394d30344aa6f05267cc8 (patch)
treed5c4977a54c911984c877d0cf2529b6cf457c1fc /server/api/clients.js
parent[server/groups] add more responses (diff)
downloadbas-530bc42e4769f40d057394d30344aa6f05267cc8.tar.gz
bas-530bc42e4769f40d057394d30344aa6f05267cc8.tar.xz
bas-530bc42e4769f40d057394d30344aa6f05267cc8.zip
[server/clients] add more responses
Diffstat (limited to 'server/api/clients.js')
-rw-r--r--server/api/clients.js27
1 files changed, 15 insertions, 12 deletions
diff --git a/server/api/clients.js b/server/api/clients.js
index 4dba883..6cd34a2 100644
--- a/server/api/clients.js
+++ b/server/api/clients.js
@@ -6,19 +6,20 @@ 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'))
// ############################################################################
// ########################### GET requests #################################
router.getAsync('', async (req, res) => {
const clients = await db.client.findAll({ order: [['name', 'ASC']] })
- res.send(clients)
+ res.status(200).send(clients)
})
router.getAsync('/:id', async (req, res) => {
const client = await db.client.findOne({ where: { id: req.params.id }, include: ['groups'] })
if (client) res.status(200).send(client)
- else res.status(404).end()
+ else httpResponse.notFound(res, req.params.id)
})
// ############################################################################
@@ -26,24 +27,26 @@ 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')
await backendHelper.deleteClients(req.body.ids)
const count = await db.client.destroy({ where: { id: req.body.ids } })
- res.status(200).send({ count })
+ httpResponse.successBatch(res, 'deleted', 'client', count)
} else {
let client
+ let action = 'updated'
if (req.params.id === undefined) {
client = await db.client.create(req.body.data)
+ action = 'created'
io.in('broadcast newClient').emit('notifications newAlert', { type: 'info', text: 'New client!' })
- } else {
+ } else if (req.params.id > 0) {
client = await db.client.findOne({ where: { id: req.params.id } })
- if (client) await client.update(req.body.data)
- }
- if (client) {
- await client.setGroups(req.body.groupIds)
- res.status(200).send({ id: client.id })
+ if (!client) return httpResponse.notFound(res, req.params.id)
+ else await client.update(req.body.data)
} else {
- res.status(404).end()
+ return httpResponse.invalidId(res)
}
+ await client.setGroups(req.body.groupIds)
+ httpResponse.success(res, action, 'client', client.id)
}
})
@@ -52,8 +55,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) res.status(200).end()
- else res.status(404).end()
+ if (count) httpResponse.success(res, 'deleted', 'client', req.params.id)
+ else httpResponse.notFound(res, req.params.id)
})
// ############################################################################