summaryrefslogtreecommitdiffstats
path: root/server/api
diff options
context:
space:
mode:
authorUdo Walter2019-04-01 01:54:28 +0200
committerUdo Walter2019-04-01 01:54:28 +0200
commitc708dd1ca22edef8bad1c42650fe90aaf95c77be (patch)
tree2b0d18dfafd53aca56f849d74011fbfadd9c7b8e /server/api
parent[webapp/systemlog] add colors to category (diff)
downloadbas-c708dd1ca22edef8bad1c42650fe90aaf95c77be.tar.gz
bas-c708dd1ca22edef8bad1c42650fe90aaf95c77be.tar.xz
bas-c708dd1ca22edef8bad1c42650fe90aaf95c77be.zip
[server/ipxeconfigs] add more responses
Diffstat (limited to 'server/api')
-rw-r--r--server/api/ipxeconfigs.js67
-rw-r--r--server/api/ipxeentries.js2
2 files changed, 43 insertions, 26 deletions
diff --git a/server/api/ipxeconfigs.js b/server/api/ipxeconfigs.js
index a53e7ae..f7020b6 100644
--- a/server/api/ipxeconfigs.js
+++ b/server/api/ipxeconfigs.js
@@ -5,6 +5,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 #################################
@@ -24,31 +25,35 @@ router.getAsync('', async (req, res) => {
})
router.getAsync('/:id', async (req, res) => {
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const config = await db.config.findOne({ where: { id: req.params.id }, include: ['groups', 'clients'] })
if (config) res.status(200).send(config)
- else res.status(404).end()
+ else HttpResponse.notFound(req.params.id).send(res)
})
router.getAsync('/:id/entries', async (req, res) => {
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const config = await db.config.findOne({
where: { id: req.params.id },
include: ['entries'],
order: [[db.config.associations.entries, db.config.associations.entries.through, 'sortValue', 'ASC']]
})
if (config) res.status(200).send(config.entries)
- else res.status(404).end()
+ else HttpResponse.notFound(req.params.id).send(res)
})
router.getAsync('/:id/groups', async (req, res) => {
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const config = await db.config.findOne({ where: { id: req.params.id }, include: ['groups'] })
if (config) res.status(200).send(config.groups)
- else res.status(404).end()
+ else HttpResponse.notFound(req.params.id).send(res)
})
router.getAsync('/:id/clients', async (req, res) => {
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const config = await db.config.findOne({ where: { id: req.params.id }, include: ['clients'] })
if (config) res.status(200).send(config.clients)
- else res.status(404).end()
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################
@@ -56,28 +61,31 @@ router.getAsync('/:id/clients', 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('ids', 'an array').send(res)
const count = await db.config.destroy({ where: { id: req.body.ids } })
- res.status(200).send({ count })
+ HttpResponse.successBatch('deleted', 'ipxe config', count).send(res)
} else {
let config
- if (req.params.id === undefined) config = await db.config.create(req.body.data)
- else {
+ let action = 'updated'
+ if (req.params.id === undefined) {
+ config = await db.config.create(req.body.data)
+ action = 'created'
+ } else if (req.params.id > 0) {
config = await db.config.findOne({ where: { id: req.params.id } })
- if (config) await config.update(req.body.data)
- }
- if (config) {
- await config.setEntries([])
- if (req.body.entries.length > 0) {
- const promises = []
- req.body.entries.forEach((entry, index) => {
- promises.push(config.addEntry(entry.id, { through: { sortValue: index, customName: entry.customName, keyBind: entry.keyBind } }))
- })
- await Promise.all(promises)
- }
- res.status(200).send({ id: config.id })
+ if (!config) return HttpResponse.notFound(req.params.id).send(res)
+ else await config.update(req.body.data)
} else {
- res.status(404).end()
+ return HttpResponse.invalidId().send(res)
+ }
+ await config.setEntries([])
+ if (req.body.entries.length > 0) {
+ const promises = []
+ req.body.entries.forEach((entry, index) => {
+ promises.push(config.addEntry(entry.id, { through: { sortValue: index, customName: entry.customName, keyBind: entry.keyBind } }))
+ })
+ await Promise.all(promises)
}
+ HttpResponse.success(action, 'ipxe entry', config.id).send(res)
}
})
@@ -85,28 +93,37 @@ router.postAsync(['', '/:id'], async (req, res) => {
// ########################### PUT requests #################################
router.putAsync('/:id/groups', async (req, res) => {
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
+ if (!Array.isArray(req.body.ids)) return HttpResponse.invalidBodyValue('ids', 'an array').send(res)
const config = await db.config.findOne({ where: { id: req.params.id } })
if (config) {
await config.setGroups(req.body.ids)
- res.status(200).end()
- } else { res.status(404).end() }
+ HttpResponse.success('set', 'clients of ipxe config', config.id).send(res)
+ } else {
+ HttpResponse.notFound(req.params.id).send(res)
+ }
})
router.putAsync('/:id/clients', async (req, res) => {
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
+ if (!Array.isArray(req.body.ids)) return HttpResponse.invalidBodyValue('ids', 'an array').send(res)
const config = await db.config.findOne({ where: { id: req.params.id } })
if (config) {
await config.setClients(req.body.ids)
- res.status(200).end()
- } else { res.status(404).end() }
+ HttpResponse.success('set', 'clients of ipxe config', config.id).send(res)
+ } else {
+ HttpResponse.notFound(req.params.id).send(res)
+ }
})
// ############################################################################
// ########################## DELETE requests ###############################
router.deleteAsync('/:id', async (req, res) => {
+ if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const count = await db.config.destroy({ where: { id: req.params.id } })
if (count) res.status(200).end()
- else res.status(404).end()
+ else HttpResponse.notFound(req.params.id).send(res)
})
// ############################################################################
diff --git a/server/api/ipxeentries.js b/server/api/ipxeentries.js
index bfdb29d..1003754 100644
--- a/server/api/ipxeentries.js
+++ b/server/api/ipxeentries.js
@@ -15,7 +15,7 @@ router.getAsync('', async (req, res) => {
})
router.getAsync('/:id', async (req, res) => {
- if (!(req.params.id > 0)) HttpResponse.invalidId().send(res)
+ if (!(req.params.id > 0)) return 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(req.params.id).send(res)