summaryrefslogtreecommitdiffstats
path: root/server/api/configurator.js
diff options
context:
space:
mode:
authorUdo Walter2018-09-18 10:33:04 +0200
committerUdo Walter2018-09-18 10:33:04 +0200
commit1d07a975a2562b800fe183c6560f4a3b84ee307a (patch)
tree07f886f07047016af3d9a7565e4abedf497c4c2f /server/api/configurator.js
parent[groups] small bugfixes (diff)
downloadbas-1d07a975a2562b800fe183c6560f4a3b84ee307a.tar.gz
bas-1d07a975a2562b800fe183c6560f4a3b84ee307a.tar.xz
bas-1d07a975a2562b800fe183c6560f4a3b84ee307a.zip
[configurator] add ipxe configurator
Diffstat (limited to 'server/api/configurator.js')
-rw-r--r--server/api/configurator.js80
1 files changed, 80 insertions, 0 deletions
diff --git a/server/api/configurator.js b/server/api/configurator.js
new file mode 100644
index 0000000..74f49bd
--- /dev/null
+++ b/server/api/configurator.js
@@ -0,0 +1,80 @@
+/* global __appdir */
+var path = require('path')
+var db = require(path.join(__appdir, 'lib', 'sequelize'))
+var express = require('express')
+var router = express.Router()
+
+router.get('/configs', (req, res) => {
+ db.config.findAll().then(configs => {
+ res.send(configs)
+ })
+})
+
+router.get('/entries', (req, res) => {
+ db.entry.findAll().then(entries => {
+ res.send(entries)
+ })
+})
+
+router.get('/configs/:id/entries', async (req, res) => {
+ var config = await db.config.findOne({ where: { id: req.params.id } })
+ var entries = await config.getEntries()
+ entries.sort((a, b) => a.config_x_entry.sortValue - b.config_x_entry.sortValue)
+ res.send(entries)
+})
+
+router.post(['/configs', '/configs/:id'], async (req, res) => {
+ var item = {
+ name: req.body.name,
+ description: req.body.description,
+ defaultEntry: req.body.defaultEntry,
+ timeout: req.body.timeout > 0 ? req.body.timeout : null,
+ script: req.body.script
+ }
+
+ var config = null
+ if (req.params.id > 0) {
+ config = await db.config.findOne({ where: { id: req.params.id } })
+ if (config) await config.update(item)
+ } else {
+ config = await db.config.create(item)
+ }
+
+ if (config) {
+ await config.setEntries([])
+ if (req.body.entries.length > 0) {
+ var 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.send({ id: config.id })
+ }
+ res.end()
+})
+
+router.post(['/entries', '/entries/:id'], async (req, res) => {
+ var item = { name: req.body.name, script: req.body.script }
+ var entry = null
+ if (req.params.id > 0) {
+ entry = await db.entry.findOne({ where: { id: req.params.id } })
+ if (entry) await entry.update(item)
+ } else {
+ await db.entry.create(item)
+ }
+ if (entry) {
+ res.send({ id: entry.id })
+ }
+ res.end()
+})
+
+router.post('/delete/configs', (req, res) => {
+ db.config.destroy({ where: { id: req.body.ids } }).then(count => { res.send({ count }) })
+})
+
+router.post('/delete/entries', (req, res) => {
+ db.entry.destroy({ where: { id: req.body.ids } }).then(count => { res.send({ count }) })
+})
+
+module.exports.router = router