From a8e6ec0d6496686399653375c00bc01a1ae262f7 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Sun, 31 Mar 2019 13:50:08 +0000 Subject: [server/ipxeconfigs] add more responses --- server/api/ipxeentries.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'server/api/ipxeentries.js') 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) }) // ############################################################################ -- cgit v1.2.3-55-g7522