summaryrefslogtreecommitdiffstats
path: root/server/api/clients.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/api/clients.js')
-rw-r--r--server/api/clients.js63
1 files changed, 52 insertions, 11 deletions
diff --git a/server/api/clients.js b/server/api/clients.js
index aeb4673..046bced 100644
--- a/server/api/clients.js
+++ b/server/api/clients.js
@@ -2,19 +2,60 @@
var path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
-module.exports = {
- get: function (req, res) {
- switch (req.query.action) {
- case 'getInfo':
- db.client.findOne({ where: { id: req.query.id }}).then(client => {
- res.send(client)
- })
- break
- default:
- res.end()
+// GET Requests
+module.exports.get = {
+ getList: function (req, res) {
+ db.client.findAll({ attributes: ['id', 'name'], order: [['name', 'ASC']] }).then(list => {
+ res.send(list)
+ })
+ },
+
+ // get all groups
+ getAll: function (req, res) {
+ db.client.findAll().then(list => {
+ res.send(list)
+ })
+ },
+
+ // get all clients that have no groups
+ getTopLevel: function (req, res) {
+ db.client.findAll({ where: { '$groups.id$': null }, include: ['groups'] }).then(clients => {
+ res.send(clients)
+ })
+ },
+
+ // 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) {
+ const id = req.body.id > 0 ? req.body.id : null
+ if (id) {
+ db.client.findOne({ where: { 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 }) })
+ } else { res.status(404).end() }
+ })
+ } else {
+ db.client.create(req.body.info).then(client => {
+ if (req.body.groupIds) client.setGroups(req.body.groupIds).then(() => { res.send({ id: client.id }) })
+ })
}
},
- post: function (req, res) {
+ // delete clients
+ delete: function (req, res) {
+ db.client.destroy({ where: { id: req.body.ids } }).then(count => { res.send({ count }) })
}
}