From 86383d4f5108eef9ef42730150c19311e1637950 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Sun, 31 Mar 2019 17:26:26 +0000 Subject: [server/httpresponse] small rework --- server/api/clients.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'server/api/clients.js') 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) }) // ############################################################################ -- cgit v1.2.3-55-g7522