From 1d07a975a2562b800fe183c6560f4a3b84ee307a Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Tue, 18 Sep 2018 08:33:04 +0000 Subject: [configurator] add ipxe configurator --- server/api/configurator.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 server/api/configurator.js (limited to 'server/api/configurator.js') 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 -- cgit v1.2.3-55-g7522