summaryrefslogtreecommitdiffstats
path: root/server/api/clients.js
diff options
context:
space:
mode:
authorUdo Walter2018-12-04 17:41:15 +0100
committerUdo Walter2018-12-04 17:41:15 +0100
commit6bc320dddf45a88976ceb5fb17cf149d8b1e9e1b (patch)
tree5edcf1f2608624dd61d677fef8a2017edca01241 /server/api/clients.js
parenteslint fix :) (diff)
downloadbas-6bc320dddf45a88976ceb5fb17cf149d8b1e9e1b.tar.gz
bas-6bc320dddf45a88976ceb5fb17cf149d8b1e9e1b.tar.xz
bas-6bc320dddf45a88976ceb5fb17cf149d8b1e9e1b.zip
[groups,clients,configurator] api rework to the new format
Diffstat (limited to 'server/api/clients.js')
-rw-r--r--server/api/clients.js99
1 files changed, 56 insertions, 43 deletions
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