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/clients.js | 99 +++++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 43 deletions(-) (limited to 'server/api/clients.js') diff --git a/server/api/clients.js b/server/api/clients.js index 147314d..35c2648 100644 --- a/server/api/clients.js +++ b/server/api/clients.js @@ -3,50 +3,63 @@ var path = require('path') var db = require(path.join(__appdir, 'lib', 'sequelize')) var io = require(path.join(__appdir, 'lib', 'socketio')) const backendHelper = require(path.join(__appdir, 'lib', 'external-backends', 'backendhelper')) +var express = require('express') +const { decorateApp } = require('@awaitjs/express'); +var router = decorateApp(express.Router()) -// GET Requests -module.exports.get = { - getList: function (req, res) { - db.client.findAll({ attributes: ['id', 'name'], order: [['name', 'ASC']] }).then(list => { - res.send(list) - }) - }, - - // get name, description, ip, mac and uuid of a client (by id) - getClient: function (req, res) { - db.client.findOne({ where: { id: req.query.id }, include: ['groups'] }).then(client => { - if (client) res.send(client) - else res.status(404).end() - }) - } -} - -// POST Requests -module.exports.post = { - // create client or update information of a client (returns id) - save: function (req, res) { - if (req.body.id > 0) { - db.client.findOne({ where: { id: req.body.id } }).then(client => { - if (client) { - var promises = [] - if (req.body.info) promises.push(client.update(req.body.info)) - if (req.body.groupIds) promises.push(client.setGroups(req.body.groupIds)) - Promise.all(promises).then(() => { res.send({ id: req.body.id }) }) - } else { res.status(404).end() } - }) - } else { - db.client.create(req.body.info).then(client => { - io.in('broadcast newClient').emit('notifications newAlert', { type: 'info', text: 'New client!' }) - if (req.body.groupIds) client.setGroups(req.body.groupIds).then(() => { res.send({ id: client.id }) }) - }) - } - }, +// ############################################################################ +// ########################### GET requests ################################# + +router.getAsync('', async (req, res) => { + const clients = await db.client.findAll({ order: [['name', 'ASC']] }) + res.send(clients) +}) - // delete clients - delete: async function (req, res) { +router.getAsync('/:id', async (req, res) => { + const client = await db.client.findOne({ where: { id: req.params.id }, include: ['groups'] }) + console.log(req.params.id) + if (client) res.status(200).send(client) + else res.status(404).end() +}) + +// ############################################################################ +// ########################## POST requests ################################# + +router.postAsync(['', '/:id'], async (req, res) => { + if (req.query.delete !== undefined && req.query.delete !== 'false') { await backendHelper.deleteClients(req.body.ids) - db.client.destroy({ where: { id: req.body.ids } }).then(count => { - res.send({ count }) - }) + const count = await db.client.destroy({ where: { id: req.body.ids } }) + res.status(200).send({ count }) + } else { + let client + if (req.params.id === undefined) { + client = await db.client.create(req.body.data) + io.in('broadcast newClient').emit('notifications newAlert', { type: 'info', text: 'New client!' }) + } + else { + client = await db.client.findOne({ where: { id: req.params.id } }) + console.log('asd') + if (client) await client.update(req.body.data) + } + if (client) { + await client.setGroups(req.body.groupIds) + res.status(200).send({ id: client.id }) + } else { + res.status(404).end() + } } -} +}) + +// ############################################################################ +// ########################## DELETE requests ############################### + +router.delete('/:id', async (req, res) => { + const count = await db.client.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