From 039be437233b3443f8ab0b93d6f34929534ba670 Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Thu, 19 Jul 2018 03:58:05 +0000 Subject: [server/groups][webapp/groups] added first version of the groups module --- server/api/clients.js | 18 +++++----- server/api/groups.js | 40 ++++++++++++++++++++++ server/api/locations.js | 13 ------- server/migrations/20180717132233-create-group.js | 19 ++++++++++ server/migrations/20180717132333-create-client.js | 28 +++++++++++++++ .../20180717202333-create-group_x_group.js | 28 +++++++++++++++ .../20180717202533-create-group_x_client.js | 28 +++++++++++++++ server/models/client.js | 22 ++++++++++++ server/models/group.js | 19 ++++++++++ server/router.js | 8 ++--- 10 files changed, 197 insertions(+), 26 deletions(-) create mode 100644 server/api/groups.js delete mode 100644 server/api/locations.js create mode 100644 server/migrations/20180717132233-create-group.js create mode 100644 server/migrations/20180717132333-create-client.js create mode 100644 server/migrations/20180717202333-create-group_x_group.js create mode 100644 server/migrations/20180717202533-create-group_x_client.js create mode 100644 server/models/client.js create mode 100644 server/models/group.js (limited to 'server') diff --git a/server/api/clients.js b/server/api/clients.js index f7fe455..aeb4673 100644 --- a/server/api/clients.js +++ b/server/api/clients.js @@ -4,15 +4,15 @@ var db = require(path.join(__appdir, 'lib', 'sequelize')) module.exports = { get: function (req, res) { - // db.sequelize.authenticate() - // .then(() => { console.log('Connection has been established successfully.'); }) - // .catch(err => { console.error('Unable to connect to the database:', err); }); - - // db.users2.create({ username: "wasd", password: "wasd", email: "w@a.de", name: "wa"}); - db.users2.findOne({ where: { username: 'wasd' } }).then(user => { - console.log(user.get('username')) - }) - res.end() + switch (req.query.action) { + case 'getInfo': + db.client.findOne({ where: { id: req.query.id }}).then(client => { + res.send(client) + }) + break + default: + res.end() + } }, post: function (req, res) { diff --git a/server/api/groups.js b/server/api/groups.js new file mode 100644 index 0000000..2d1182b --- /dev/null +++ b/server/api/groups.js @@ -0,0 +1,40 @@ +/* global __appdir */ +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 'getInfo': + db.group.findOne({ where: { id: req.query.id }}).then(group => { + res.send(group) + }) + 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) + }) + break + default: + res.end() + } + }, + post: function (req, res) { + var id = req.body.id > 0 ? req.body.id : null + switch (req.body.action) { + case 'setInfo': + if (!id) res.end() + db.group.update({ name: req.body.name }, { where: { id: id } }) + res.end() + break + default: + res.end() + } + } +} diff --git a/server/api/locations.js b/server/api/locations.js deleted file mode 100644 index 7595837..0000000 --- a/server/api/locations.js +++ /dev/null @@ -1,13 +0,0 @@ -// /* global __appdir */ -// var path = require('path'); - -module.exports = { - get: function (req, res) { - console.log('You successfully called an authentication call!') - res.send('You successfully called an authentication call!') - }, - post: function (req, res) { - console.log('You successfully called an unauthentication call!') - res.send('You successfully called an unauthentication call!') - } -} diff --git a/server/migrations/20180717132233-create-group.js b/server/migrations/20180717132233-create-group.js new file mode 100644 index 0000000..720a1e7 --- /dev/null +++ b/server/migrations/20180717132233-create-group.js @@ -0,0 +1,19 @@ +'use strict' +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('groups', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + name: { + type: Sequelize.STRING + } + }) + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('groups') + } +} diff --git a/server/migrations/20180717132333-create-client.js b/server/migrations/20180717132333-create-client.js new file mode 100644 index 0000000..79552c4 --- /dev/null +++ b/server/migrations/20180717132333-create-client.js @@ -0,0 +1,28 @@ +'use strict' +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('clients', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + name: { + type: Sequelize.STRING + }, + ip: { + type: Sequelize.STRING + }, + mac: { + type: Sequelize.STRING + }, + uuid: { + type: Sequelize.STRING + } + }) + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('clients') + } +} diff --git a/server/migrations/20180717202333-create-group_x_group.js b/server/migrations/20180717202333-create-group_x_group.js new file mode 100644 index 0000000..4263f9a --- /dev/null +++ b/server/migrations/20180717202333-create-group_x_group.js @@ -0,0 +1,28 @@ +'use strict' +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('group_x_group', { + parentId: { + primaryKey: true, + allowNull: false, + type: Sequelize.INTEGER, + references: { + model: 'groups', + key: 'id' + } + }, + childId: { + primaryKey: true, + allowNull: false, + type: Sequelize.INTEGER, + references: { + model: 'groups', + key: 'id' + } + } + }) + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('group_x_group') + } +} diff --git a/server/migrations/20180717202533-create-group_x_client.js b/server/migrations/20180717202533-create-group_x_client.js new file mode 100644 index 0000000..e3bd490 --- /dev/null +++ b/server/migrations/20180717202533-create-group_x_client.js @@ -0,0 +1,28 @@ +'use strict' +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('group_x_client', { + groupId: { + primaryKey: true, + allowNull: false, + type: Sequelize.INTEGER, + references: { + model: 'groups', + key: 'id' + } + }, + clientId: { + primaryKey: true, + allowNull: false, + type: Sequelize.INTEGER, + references: { + model: 'clients', + key: 'id' + } + } + }) + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('group_x_client') + } +} diff --git a/server/models/client.js b/server/models/client.js new file mode 100644 index 0000000..ad3bc28 --- /dev/null +++ b/server/models/client.js @@ -0,0 +1,22 @@ +'use strict' +module.exports = (sequelize, DataTypes) => { + var client = sequelize.define('client', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: DataTypes.INTEGER + }, + name: DataTypes.STRING, + ip: DataTypes.STRING, + mac: DataTypes.STRING, + uuid: DataTypes.STRING + }, { + timestamps: false + }) + var GroupXClient = sequelize.define('group_x_client', {}, { timestamps: false, freezeTableName: true }) + client.associate = function (models) { + 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 new file mode 100644 index 0000000..9151db5 --- /dev/null +++ b/server/models/group.js @@ -0,0 +1,19 @@ +'use strict' +module.exports = (sequelize, DataTypes) => { + var group = sequelize.define('group', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: DataTypes.INTEGER + }, + name: DataTypes.STRING + }, { + timestamps: false + }) + var GroupXGroup = sequelize.define('group_x_group', {}, { timestamps: false, freezeTableName: true }) + group.associate = function (models) { + group.belongsToMany(group, { as: 'parents', through: GroupXGroup, foreignKey: 'childId', otherKey: 'parentId'}) + } + return group +} diff --git a/server/router.js b/server/router.js index 4f8d0bb..7bffb92 100644 --- a/server/router.js +++ b/server/router.js @@ -14,10 +14,10 @@ router.post('/changepassword', auth.changePassword) var user = require(path.join(__dirname, 'api', 'user')) router.get('/user/info', auth.verifyToken, user.info) -// Locations API -var locations = require(path.join(__dirname, 'api', 'locations')) -router.get('/locations', auth.verifyToken, locations.get) -router.post('/locations', locations.post) +// Groups API +var groups = require(path.join(__dirname, 'api', 'groups')) +router.get('/groups', groups.get) +router.post('/groups', groups.post) // Clients API var clients = require(path.join(__dirname, 'api', 'clients')) -- cgit v1.2.3-55-g7522