From 6bc320dddf45a88976ceb5fb17cf149d8b1e9e1b Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Tue, 4 Dec 2018 16:41:15 +0000 Subject: [groups,clients,configurator] api rework to the new format --- server/api/configurator.js | 125 ++++++++++++++++++++++++++------------------- 1 file changed, 72 insertions(+), 53 deletions(-) (limited to 'server/api/configurator.js') diff --git a/server/api/configurator.js b/server/api/configurator.js index 250471b..d0e4531 100644 --- a/server/api/configurator.js +++ b/server/api/configurator.js @@ -1,79 +1,98 @@ /* global __appdir */ var path = require('path') var db = require(path.join(__appdir, 'lib', 'sequelize')) +var Sequelize = require('sequelize') var express = require('express') -var router = express.Router() +const { decorateApp } = require('@awaitjs/express'); +var router = decorateApp(express.Router()) -router.get('/configs', (req, res) => { - db.config.findAll().then(configs => { - res.send(configs) - }) +// ############################################################################ +// ########################### GET requests ################################# + +router.getAsync('/configs', async (req, res) => { + const configs = await db.config.findAll({ order: [['name', 'ASC']] }) + res.status(200).send(configs) }) -router.get('/entries', (req, res) => { - db.entry.findAll().then(entries => { - res.send(entries) - }) +router.getAsync('/entries', async (req, res) => { + const entries = await db.entry.findAll({ order: [['name', 'ASC']] }) + res.status(200).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({ order: [[['entries'], 'sortValue', 'ASC']] }) - res.send(entries) +router.getAsync('/configs/:id/entries', async (req, res) => { + var 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.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 - } +// ############################################################################ +// ########################## POST requests ################################# - var config = null - if (req.params.id > 0) { - config = await db.config.findOne({ where: { id: req.params.id } }) - if (config) await config.update(item) +router.postAsync(['/configs', '/configs/: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 { - 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) + 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() } - 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) +router.postAsync(['/entries', '/entries/:id'], async (req, res) => { + if (req.query.delete !== undefined && req.query.delete !== 'false') { + const count = await db.entry.destroy({ where: { id: req.body.ids } }) + res.status(200).send({ count }) } else { - entry = await db.entry.create(item) - } - if (entry) { - res.send({ id: entry.id }) + let entry + if (req.params.id === undefined) entry = await db.entry.create(req.body.data) + else { + 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 }) + } else { + res.status(404).end() + } } - res.end() }) -router.post('/delete/configs', (req, res) => { - db.config.destroy({ where: { id: req.body.ids } }).then(count => { res.send({ count }) }) +// ############################################################################ +// ########################## DELETE requests ############################### + +router.deleteAsync('/configs/: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() }) -router.post('/delete/entries', (req, res) => { - db.entry.destroy({ where: { id: req.body.ids } }).then(count => { res.send({ count }) }) +router.deleteAsync('/entries/:id', async (req, res) => { + const count = await db.entry.destroy({ where: { id: req.params.id } }) + if (count) res.status(200).end() + else res.status(404).end() }) +// ############################################################################ +// ############################################################################ + module.exports.router = router -- cgit v1.2.3-55-g7522