From f6cadb25e42e6568098f2962fa9b302cfbf074c5 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Sun, 29 Jul 2018 23:31:08 +0000 Subject: [server/router] changed api routing from query keyword to url parameter for group and client api (todo: the rest) --- server/api/clients.js | 18 +++++++------- server/api/groups.js | 62 ++++++++++++++++++++++++------------------------- server/models/client.js | 2 +- server/models/group.js | 2 +- server/router.js | 18 ++++++++++---- 5 files changed, 55 insertions(+), 47 deletions(-) (limited to 'server') diff --git a/server/api/clients.js b/server/api/clients.js index aeb4673..37e207b 100644 --- a/server/api/clients.js +++ b/server/api/clients.js @@ -3,18 +3,16 @@ 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: { + + getInfo: function (req, res) { + db.client.findOne({ where: { id: req.query.id } }).then(client => { + res.send(client) + }) } + }, - post: function (req, res) { + post: { } } diff --git a/server/api/groups.js b/server/api/groups.js index 8158d1c..ff29799 100644 --- a/server/api/groups.js +++ b/server/api/groups.js @@ -3,40 +3,40 @@ var path = require('path') var db = require(path.join(__appdir, 'lib', 'sequelize')) module.exports = { - get: function (req, res) { - var id = req.query.id > 0 ? req.query.id : null - switch (req.query.action) { - case 'getParents': - db.group.findOne({ where: { id: req.query.id }, include: ['parents']}).then(group => { - group.getParents().then(parents => { - res.send(parents.map(x => ({ id: x.id, name: x.name }))) - }) - }) - break - case 'getSubGroups': - db.group.findAll({ where: { '$parents.id$': id }, include: ['parents'] }).then(result => { - res.send(result) - }) - break - case 'getClients': - db.client.findAll({ where: { '$groups.id$': id }, include: ['groups'] }).then(result => { - res.send(result) + get: { + + getParents: function (req, res) { + const id = req.query.id > 0 ? req.query.id : null + db.group.findOne({ where: { id: id }, include: ['parents'] }).then(group => { + group.getParents().then(parents => { + res.send(parents.map(x => ({ id: x.id, name: x.name }))) }) - break - default: - res.end() + }) + }, + + getSubGroups: function (req, res) { + const id = req.query.id > 0 ? req.query.id : null + db.group.findAll({ where: { '$parents.id$': id }, include: ['parents'] }).then(result => { + res.send(result) + }) + }, + + getClients: function (req, res) { + const id = req.query.id > 0 ? req.query.id : null + db.client.findAll({ where: { '$groups.id$': id }, include: ['groups'] }).then(result => { + res.send(result) + }) } + }, - post: function (req, res) { - var id = req.body.id > 0 ? req.body.id : null - switch (req.body.action) { - case 'update': - if (!id) res.end() - db.group.update({ name: req.body.name }, { where: { id: id } }) - res.end() - break - default: - res.end() + post: { + + update: function (req, res) { + const id = req.body.id > 0 ? req.body.id : null + if (!id) res.end() + db.group.update({ name: req.body.name }, { where: { id: id } }) + res.end() } + } } diff --git a/server/models/client.js b/server/models/client.js index ad3bc28..1086023 100644 --- a/server/models/client.js +++ b/server/models/client.js @@ -14,8 +14,8 @@ module.exports = (sequelize, DataTypes) => { }, { timestamps: false }) - var GroupXClient = sequelize.define('group_x_client', {}, { timestamps: false, freezeTableName: true }) client.associate = function (models) { + var GroupXClient = sequelize.define('group_x_client', {}, { timestamps: false, freezeTableName: true }) client.belongsToMany(models.group, { as: 'groups', through: GroupXClient, foreignKey: 'clientId', otherKey: 'groupId'}) } return client diff --git a/server/models/group.js b/server/models/group.js index 9151db5..223df07 100644 --- a/server/models/group.js +++ b/server/models/group.js @@ -11,8 +11,8 @@ module.exports = (sequelize, DataTypes) => { }, { timestamps: false }) - var GroupXGroup = sequelize.define('group_x_group', {}, { timestamps: false, freezeTableName: true }) group.associate = function (models) { + var GroupXGroup = sequelize.define('group_x_group', {}, { timestamps: false, freezeTableName: true }) group.belongsToMany(group, { as: 'parents', through: GroupXGroup, foreignKey: 'childId', otherKey: 'parentId'}) } return group diff --git a/server/router.js b/server/router.js index bde35a8..6c0e4cd 100644 --- a/server/router.js +++ b/server/router.js @@ -2,6 +2,16 @@ var express = require('express') var router = express.Router() var path = require('path') +function mapActions (api, method) { + return function (req, res) { + if (method in api && req.params.action in api[method]) { + api[method][req.params.action](req, res) + } else { + res.status(501).end() + } + } +} + // Authentication routes var auth = require(path.join(__dirname, 'lib', 'authentication')) router.get('/auth', auth.auth) @@ -16,13 +26,13 @@ router.get('/user/info', auth.verifyToken, user.info) // Groups API var groups = require(path.join(__dirname, 'api', 'groups')) -router.get('/groups', groups.get) -router.post('/groups', groups.post) +router.get('/groups/:action', mapActions(groups, 'get')) +router.post('/groups/:action', mapActions(groups, 'post')) // Clients API var clients = require(path.join(__dirname, 'api', 'clients')) -router.get('/clients', clients.get) -router.post('/clients', clients.post) +router.get('/clients/:action', mapActions(clients, 'get')) +router.post('/clients/:action', mapActions(clients, 'post')) // Permissions API var permissions = require(path.join(__dirname, 'api', 'permissions')) -- cgit v1.2.3-55-g7522