Skip to content
Snippets Groups Projects
Commit 6db52d74 authored by Markus Grigull's avatar Markus Grigull
Browse files

Remove unneeded widget model and route

parent eadb7cd1
No related branches found
No related tags found
No related merge requests found
......@@ -22,37 +22,14 @@
// include
var mongoose = require('mongoose');
//var Widget = require('./widget');
var Schema = mongoose.Schema;
// visualization model
var visualizationSchema = new Schema({
name: { type: String, required: true },
project: { type: Schema.Types.ObjectId, ref: 'Project'/*, required: true*/ },
widgets: { type: Array, default: [] }
/*widgets: [{ type: Schema.Types.ObjectId, ref: 'Widget' }],
rows: { type: Number, default: 1 }*/
});
// execute before the visualization is deleted
visualizationSchema.pre('remove', function(callback) {
// delete all widgets belonging to this visualization
/*this.widgets.forEach(function(id) {
Widget.findOne({ _id: id }, function(err, widget) {
if (err) {
return console.log(err);
}
widget.remove(function(err) {
if (err) {
return console.log(err);
}
});
});
});*/
callback();
widgets: { type: Array, default: [] },
grid: { type: Number, default: 1 }
});
module.exports = mongoose.model('Visualization', visualizationSchema);
/**
* File: widget.js
* Author: Markus Grigull <mgrigull@eonerc.rwth-aachen.de>
* Date: 28.06.2016
*
* This file is part of VILLASweb-backend.
*
* VILLASweb-backend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VILLASweb-backend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VILLASweb-backend. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
// include
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// widget model
var widgetSchema = new Schema({
name: { type: String },
widgetData: { type: Schema.Types.Mixed, default: {} },
width: { type: Number, required: true },
height: { type: Number, required: true },
type: { type: String, required: true },
x: { type: Number, default: 0 },
y: { type: Number, default: 0 },
z: { type: Number, default: 0 },
visualization: { type: Schema.Types.ObjectId, ref: 'Visualization' }
});
module.exports = mongoose.model('Widget', widgetSchema);
/**
* File: widgets.js
* Author: Markus Grigull <mgrigull@eonerc.rwth-aachen.de>
* Date: 28.06.2016
*
* This file is part of VILLASweb-backend.
*
* VILLASweb-backend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VILLASweb-backend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VILLASweb-backend. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
// include
var express = require('express');
var auth = require('../auth');
var logger = require('../utils/logger');
// models
var Widget = require('../models/widget');
var Visualization = require('../models/visualization');
// create router
var router = express.Router();
// all widget routes need authentication
router.use('/widgets', auth.validateToken);
// routes
router.get('/widgets', /*auth.validateRole('visualization', 'read'),*/ function(req, res) {
// get all widgets
Widget.find(function(err, widgets) {
if (err) {
logger.error('Unable to receive widgets', err);
return res.send(err);
}
res.send({ widgets: widgets });
});
});
router.post('/widgets', /*auth.validateRole('visualization', 'create'),*/ function(req, res) {
// create new widget
var widget = new Widget(req.body.widget);
widget.save(function(err) {
if (err) {
logger.error('Unable to create widget', err);
return res.send(err);
}
res.send({ widget: widget });
});
// add widget to visualization
Visualization.findOne({ _id: widget.visualization }, function(err, visualization) {
if (err) {
logger.log('verbose', 'Unknown visualization for id: ' + widget.visualization);
return;
}
visualization.widgets.push(widget._id);
visualization.save(function(err) {
if (err) {
logger.error('Unable to save visualization', visualization);
return;
}
});
});
});
router.put('/widgets/:id', /*auth.validateRole('visualization', 'update'),*/ function(req, res) {
// get widget
Widget.findOne({ _id: req.params.id }, function(err, widget) {
if (err) {
logger.log('verbose', 'PUT Unknown widget for id: ' + req.params.id);
return res.send(err);
}
// update all properties
for (property in req.body.widget) {
widget[property] = req.body.widget[property];
}
// save the changes
widget.save(function(err) {
if (err) {
logger.error('Unable to save widget', widget);
return res.send(err);
}
res.send({ widget: widget });
});
});
});
router.get('/widgets/:id', /*auth.validateRole('visualization', 'read'),*/ function(req, res) {
Widget.findOne({ _id: req.params.id }, function(err, widget) {
if (err) {
logger.log('verbose', 'GET Unknown widget for id: ' + req.params.id);
return res.send(err);
}
res.send({ widget: widget });
});
});
router.delete('/widgets/:id', /*auth.validateRole('visualization', 'delete'),*/ function(req, res) {
Widget.findOne({ _id: req.params.id }, function(err, widget) {
if (err) {
logger.log('verbose', 'DELETE Unknown widget for id: ' + req.params.id);
return res.send(err);
}
// remove from visualization's list
Visualization.findOne({ _id: widget.visualization }, function(err, visualization) {
if (err) {
logger.log('verbose', 'Unknown visualization for id: ' + widget.visualization);
return;
}
for (var i = 0; i < visualization.widgets.length; i++) {
var id = String(visualization.widgets[i]);
if (id == widget._id) {
visualization.widgets.splice(i, 1);
}
}
visualization.save(function(err) {
if (err) {
logger.error('Unable to save visualization', visualization);
return;
}
});
});
widget.remove(function(err) {
if (err) {
logger.error('Unable to remove widget', widget);
return res.send(err);
}
res.send({});
});
});
});
module.exports = router;
......@@ -32,7 +32,6 @@ var logger = require('./utils/logger');
var users = require('./routes/users');
var projects = require('./routes/projects');
var visualizations = require('./routes/visualizations');
var widgets = require('./routes/widgets');
var simulations = require('./routes/simulations');
var upload = require('./routes/upload');
var files = require('./routes/files');
......@@ -86,7 +85,6 @@ mongoose.connect(config.databaseURL + config.databaseName, { useMongoClient: tru
app.use('/api/v1', users);
app.use('/api/v1', projects);
app.use('/api/v1', visualizations);
app.use('/api/v1', widgets);
app.use('/api/v1', simulations);
app.use('/api/v1', upload);
app.use('/api/v1', files);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment