summaryrefslogtreecommitdiffstats
path: root/server/api
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
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')
-rw-r--r--server/api/clients.js19
-rw-r--r--server/api/groups.js38
-rw-r--r--server/api/ipxeentries.js22
3 files changed, 40 insertions, 39 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)
})
// ############################################################################
diff --git a/server/api/groups.js b/server/api/groups.js
index 99260ea..e3683bf 100644
--- a/server/api/groups.js
+++ b/server/api/groups.js
@@ -6,7 +6,7 @@ const ipHelper = require(path.join(__appdir, 'lib', 'iphelper'))
const express = require('express')
const { decorateApp } = require('@awaitjs/express')
const router = decorateApp(express.Router())
-const httpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
+const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
// ############################################################################
// ########################### GET requests #################################
@@ -29,7 +29,7 @@ router.getAsync('/:id', async (req, res) => {
if (all) res.status(200).send({ ...group.get({ plain: true }), ...await groupUtil.getAllChildren([group]) })
else res.status(200).send(group)
} else {
- return httpResponse.notFound(res, req.params.id)
+ return HttpResponse.notFound(req.params.id).send(res)
}
} else if (req.params.id === '0') {
const [subgroups, clients] = await Promise.all([
@@ -38,7 +38,7 @@ router.getAsync('/:id', async (req, res) => {
])
res.send({ id: 0, subgroups, clients })
} else {
- httpResponse.invalidId(res, true)
+ HttpResponse.invalidId(true).send(res)
}
})
@@ -47,9 +47,9 @@ 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)
const count = await db.group.destroy({ where: { id: req.body.ids } })
- httpResponse.successBatch(res, 'deleted', 'group', count)
+ HttpResponse.successBatch('deleted', 'group', count).send(res)
} else {
let group
let action = 'updated'
@@ -58,10 +58,10 @@ router.postAsync(['', '/:id'], async (req, res) => {
action = 'created'
} else if (req.params.id > 0) {
group = await db.group.findOne({ where: { id: req.params.id }, include: ['ipranges'] })
- if (!group) return httpResponse.notFound(res, req.params.id)
+ if (!group) return HttpResponse.notFound(req.params.id).send(res)
else await group.update(req.body.data)
} else {
- return httpResponse.invalidId(res)
+ return HttpResponse.invalidId().send(res)
}
const promises = []
// Set group parents
@@ -96,39 +96,39 @@ router.postAsync(['', '/:id'], async (req, res) => {
}
}
await Promise.all(promises)
- httpResponse.success(res, action, 'group', group.id)
+ HttpResponse.success(action, 'group', group.id).send(res)
}
})
router.postAsync('/:id/subgroups', async (req, res) => {
- if (!(req.params.id > 0)) return httpResponse.invalidId(res)
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const group = await db.group.findOne({ where: { id: req.params.id } })
if (group) {
if (req.query.delete !== undefined && req.query.delete !== 'false') {
const count = await group.removeSubgroups(req.body.ids)
- httpResponse.successBatch(res, 'removed', 'subgroup', count)
+ HttpResponse.successBatch('removed', 'subgroup', count).send(res)
} else {
const count = await group.addSubgroups(req.body.ids)
- httpResponse.successBatch(res, 'added', 'subgroup', count)
+ HttpResponse.successBatch('added', 'subgroup', count).send(res)
}
} else {
- httpResponse.notFound(res, req.params.id)
+ HttpResponse.notFound(req.params.id).send(res)
}
})
router.postAsync('/:id/clients', async (req, res) => {
- if (!(req.params.id > 0)) return httpResponse.invalidId(res)
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const group = await db.group.findOne({ where: { id: req.params.id } })
if (group) {
if (req.query.delete !== undefined && req.query.delete !== 'false') {
const count = await group.removeClients(req.body.ids)
- httpResponse.successBatch(res, 'removed', 'client', count)
+ HttpResponse.successBatch('removed', 'client', count).send(res)
} else {
const count = await group.addClients(req.body.ids)
- httpResponse.successBatch(res, 'added', 'client', count)
+ HttpResponse.successBatch('added', 'client', count).send(res)
}
} else {
- httpResponse.notFound(res, req.params.id)
+ HttpResponse.notFound(req.params.id).send(res)
}
})
@@ -136,10 +136,10 @@ router.postAsync('/:id/clients', async (req, res) => {
// ########################## DELETE requests ###############################
router.delete('/:id', async (req, res) => {
- if (!(req.params.id > 0)) return httpResponse.invalidId(res)
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const count = await db.group.destroy({ where: { id: req.params.id } })
- if (count) httpResponse.success(res, 'deleted', 'group', req.params.id)
- else httpResponse.notFound(res, req.params.id)
+ if (count) HttpResponse.success('deleted', 'group', req.params.id).send(res)
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################
diff --git a/server/api/ipxeentries.js b/server/api/ipxeentries.js
index 45cdc9d..bfdb29d 100644
--- a/server/api/ipxeentries.js
+++ b/server/api/ipxeentries.js
@@ -4,7 +4,7 @@ var db = require(path.join(__appdir, 'lib', 'sequelize'))
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 #################################
@@ -15,10 +15,10 @@ router.getAsync('', async (req, res) => {
})
router.getAsync('/:id', async (req, res) => {
- if (!(req.params.id > 0)) httpResponse.invalidId(res)
+ if (!(req.params.id > 0)) HttpResponse.invalidId().send(res)
const entry = await db.entry.findOne({ where: { id: req.params.id } })
if (entry) res.status(200).send(entry)
- else httpResponse.notFound(res, req.params.id)
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################
@@ -26,9 +26,9 @@ 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)
const count = await db.entry.destroy({ where: { id: req.body.ids } })
- httpResponse.successBatch(res, 'deleted', ['ipxe entry', 'ipxe entries'], count)
+ HttpResponse.successBatch('deleted', ['ipxe entry', 'ipxe entries'], count).send(res)
} else {
let entry
let action = 'updated'
@@ -37,12 +37,12 @@ router.postAsync(['', '/:id'], async (req, res) => {
action = 'created'
} else if (req.params.id > 0) {
entry = await db.entry.findOne({ where: { id: req.params.id } })
- if (!entry) return httpResponse.notFound(res, req.params.id)
+ if (!entry) return HttpResponse.notFound(req.params.id).send(res)
else await entry.update(req.body.data)
} else {
- return httpResponse.invalidId(res)
+ return HttpResponse.invalidId().send(res)
}
- httpResponse.success(res, action, 'ipxe entry', entry.id)
+ HttpResponse.success(action, 'ipxe entry', entry.id).send(res)
}
})
@@ -50,10 +50,10 @@ router.postAsync(['', '/:id'], async (req, res) => {
// ########################## DELETE requests ###############################
router.deleteAsync('/:id', async (req, res) => {
- if (!(req.params.id > 0)) return httpResponse.invalidId(res)
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const count = await db.entry.destroy({ where: { id: req.params.id } })
- if (count) httpResponse.success(res, 'deleted', ['ipxe entry', 'ipxe entries'], req.params.id)
- else httpResponse.notFound(res, req.params.id)
+ if (count) HttpResponse.success('deleted', ['ipxe entry', 'ipxe entries'], req.params.id).send(res)
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################