summaryrefslogtreecommitdiffstats
path: root/server/api/ipxeentries.js
diff options
context:
space:
mode:
authorUdo Walter2019-03-31 15:50:08 +0200
committerUdo Walter2019-03-31 15:50:08 +0200
commita8e6ec0d6496686399653375c00bc01a1ae262f7 (patch)
tree6a7ed1a87988c27e71927a907036460a6ec96098 /server/api/ipxeentries.js
parent[server/clients] add more responses (diff)
downloadbas-a8e6ec0d6496686399653375c00bc01a1ae262f7.tar.gz
bas-a8e6ec0d6496686399653375c00bc01a1ae262f7.tar.xz
bas-a8e6ec0d6496686399653375c00bc01a1ae262f7.zip
[server/ipxeconfigs] add more responses
Diffstat (limited to 'server/api/ipxeentries.js')
-rw-r--r--server/api/ipxeentries.js28
1 files changed, 17 insertions, 11 deletions
diff --git a/server/api/ipxeentries.js b/server/api/ipxeentries.js
index 25cfd21..53f65b5 100644
--- a/server/api/ipxeentries.js
+++ b/server/api/ipxeentries.js
@@ -4,6 +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'))
// ############################################################################
// ########################### GET requests #################################
@@ -14,9 +15,10 @@ router.getAsync('', async (req, res) => {
})
router.getAsync('/:id', async (req, res) => {
+ if (!(req.params.id > 0)) httpResponse.invalidId(res)
const entry = await db.entry.findOne({ where: { id: req.params.id }})
if (entry) res.status(200).send(entry)
- else res.status(404).end()
+ else httpResponse.notFound(res, req.params.id)
})
// ############################################################################
@@ -24,20 +26,23 @@ 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')
const count = await db.entry.destroy({ where: { id: req.body.ids } })
- res.status(200).send({ count })
+ httpResponse.successBatch(res, 'deleted', ['ipxe entry', 'ipxe entries'], count)
} else {
let entry
- if (req.params.id === undefined) entry = await db.entry.create(req.body.data)
- else {
+ let action = 'updated'
+ if (req.params.id === undefined) {
+ entry = await db.entry.create(req.body.data)
+ action = 'created'
+ } else if (req.params.id > 0) {
entry = await db.entry.findOne({ where: { id: req.params.id } })
- if (entry) await entry.update(req.body.data)
- }
- if (entry) {
- res.status(200).send({ id: entry.id })
+ if (!entry) return httpResponse.notFound(res, req.params.id)
+ else await entry.update(req.body.data)
} else {
- res.status(404).end()
+ return httpResponse.invalidId(res)
}
+ httpResponse.success(res, action, 'ipxe entry', entry.id)
}
})
@@ -45,9 +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)
const count = await db.entry.destroy({ where: { id: req.params.id } })
- if (count) res.status(200).end()
- else res.status(404).end()
+ if (count) httpResponse.success(res, 'deleted', ['ipxe entry', 'ipxe entries'], req.params.id)
+ else httpResponse.notFound(res, req.params.id)
})
// ############################################################################