summaryrefslogtreecommitdiffstats
path: root/server/api/ipxeconfigs.js
diff options
context:
space:
mode:
authorUdo Walter2019-03-30 22:49:02 +0100
committerUdo Walter2019-03-30 22:49:02 +0100
commit8a4383885cb6bc19a2ce16bbad31ff0beada5ae2 (patch)
tree6d59120c2cea7c9d7e07d201af8f080d45a35945 /server/api/ipxeconfigs.js
parent[webapp] fix bug in groups; remember filters in systemlog (diff)
downloadbas-8a4383885cb6bc19a2ce16bbad31ff0beada5ae2.tar.gz
bas-8a4383885cb6bc19a2ce16bbad31ff0beada5ae2.tar.xz
bas-8a4383885cb6bc19a2ce16bbad31ff0beada5ae2.zip
[server] configurator api -> ipxeconfigs and ipxeentries api; [webapp] ui polishing
Diffstat (limited to 'server/api/ipxeconfigs.js')
-rw-r--r--server/api/ipxeconfigs.js115
1 files changed, 115 insertions, 0 deletions
diff --git a/server/api/ipxeconfigs.js b/server/api/ipxeconfigs.js
new file mode 100644
index 0000000..a53e7ae
--- /dev/null
+++ b/server/api/ipxeconfigs.js
@@ -0,0 +1,115 @@
+/* global __appdir */
+var path = require('path')
+var Sequelize = require('sequelize')
+var db = require(path.join(__appdir, 'lib', 'sequelize'))
+var express = require('express')
+const { decorateApp } = require('@awaitjs/express')
+var router = decorateApp(express.Router())
+
+// ############################################################################
+// ########################### GET requests #################################
+
+router.getAsync('', async (req, res) => {
+ const configs = await db.config.findAll({
+ include: [{ association: 'groups', attributes: [] }, { association: 'clients', attributes: [] }],
+ attributes: {
+ include: [
+ [Sequelize.fn('COUNT', Sequelize.col('groups.id')), 'groupCount'],
+ [Sequelize.fn('COUNT', Sequelize.col('clients.id')), 'clientCount']
+ ]
+ },
+ group: ['id']
+ })
+ res.status(200).send(configs)
+})
+
+router.getAsync('/:id', async (req, 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()
+})
+
+router.getAsync('/:id/entries', async (req, 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()
+})
+
+router.getAsync('/:id/groups', async (req, 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()
+})
+
+router.getAsync('/:id/clients', async (req, 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()
+})
+
+// ############################################################################
+// ########################## POST requests #################################
+
+router.postAsync(['', '/:id'], async (req, res) => {
+ if (req.query.delete !== undefined && req.query.delete !== 'false') {
+ const count = await db.config.destroy({ where: { id: req.body.ids } })
+ res.status(200).send({ count })
+ } else {
+ let config
+ if (req.params.id === undefined) config = await db.config.create(req.body.data)
+ else {
+ 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 })
+ } else {
+ res.status(404).end()
+ }
+ }
+})
+
+// ############################################################################
+// ########################### PUT requests #################################
+
+router.putAsync('/:id/groups', async (req, 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() }
+})
+
+router.putAsync('/:id/clients', async (req, 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() }
+})
+
+// ############################################################################
+// ########################## DELETE requests ###############################
+
+router.deleteAsync('/:id', async (req, res) => {
+ const count = await db.config.destroy({ where: { id: req.params.id } })
+ if (count) res.status(200).end()
+ else res.status(404).end()
+})
+
+// ############################################################################
+// ############################################################################
+
+module.exports.router = router