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

Add simulation route and model

Simulations are the container for simulation models
parent 951c269d
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
...@@ -18,7 +18,8 @@ var Schema = mongoose.Schema; ...@@ -18,7 +18,8 @@ var Schema = mongoose.Schema;
var projectSchema = new Schema({ var projectSchema = new Schema({
name: { type: String, required: true }, name: { type: String, required: true },
owner: { type: Schema.Types.ObjectId, ref: 'User', required: true }, owner: { type: Schema.Types.ObjectId, ref: 'User', required: true },
visualizations: [{ type: Schema.Types.ObjectId, ref: 'Visualization' }] visualizations: [{ type: Schema.Types.ObjectId, ref: 'Visualization', default: [] }],
simulation: [{ type: Schema.Types.ObjectId, ref: 'Simulation' }]
}); });
projectSchema.pre('remove', function(callback) { projectSchema.pre('remove', function(callback) {
......
/**
* File: simulation.js
* Author: Markus Grigull <mgrigull@eonerc.rwth-aachen.de>
* Date: 26.07.2016
* Copyright: 2016, Institute for Automation of Complex Power Systems, EONERC
* This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential.
* Unauthorized copying of this file, via any medium is strictly prohibited.
**********************************************************************************/
// include
var mongoose = require('mongoose');
var SimulationModel = require('./simulationModel');
var Schema = mongoose.Schema;
// simulation model
var simulationSchema = new Schema({
name: { type: String, required: true },
running: { type: Boolean, default: false },
owner: { type: Schema.Types.ObjectId, ref: 'User' },
models: [{ type: Schema.Types.ObjectId, ref: 'SimulationModel', default: [] }],
projects: [{ type: Schema.Types.ObjectId, ref: 'Project', default: [] }]
});
simulationSchema.pre('remove', function(callback) {
// delete all models belonging to this project
this.models.forEach(function(id) {
SimulationModel.findOne({ _id: id }, function(err, model) {
if (err) {
return console.log(err);
}
model.remove(function(err) {
if (err) {
return console.log(err);
}
});
});
});
callback();
});
module.exports = mongoose.model('Simulation', simulationSchema);
...@@ -15,9 +15,10 @@ var Schema = mongoose.Schema; ...@@ -15,9 +15,10 @@ var Schema = mongoose.Schema;
// simulation model model // simulation model model
var simulationModelSchema = new Schema({ var simulationModelSchema = new Schema({
name: { type: String, required: true }, name: { type: String, required: true },
running: { type: Boolean, default: false }, simulator: { type: Number, required: true },
projects: [{ type: Schema.Types.ObjectId, ref: 'Project' }], length: { type: Number, default: 1 },
owner: { type: Schema.Types.ObjectId, ref: 'User' } mapping: [{ type: String, default: [] }],
simulation: { type: Schema.Types.ObjectId, ref: 'Simulation' }
}); });
module.exports = mongoose.model('SimulationModel', simulationModelSchema); module.exports = mongoose.model('SimulationModel', simulationModelSchema);
...@@ -12,7 +12,7 @@ var mongoose = require('mongoose'); ...@@ -12,7 +12,7 @@ var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs'); var bcrypt = require('bcrypt-nodejs');
var Project = require('./project'); var Project = require('./project');
var SimulationModel = require('./simulationModel'); var Simulation = require('./simulation');
var Schema = mongoose.Schema; var Schema = mongoose.Schema;
...@@ -23,7 +23,7 @@ var userSchema = new Schema({ ...@@ -23,7 +23,7 @@ var userSchema = new Schema({
adminLevel: { type: Number, default: 0 }, adminLevel: { type: Number, default: 0 },
projects: [{ type: Schema.Types.ObjectId, ref: 'Project', default: [] }], projects: [{ type: Schema.Types.ObjectId, ref: 'Project', default: [] }],
mail: { type: String, default: "" }, mail: { type: String, default: "" },
simulationModel: [{ type: Schema.Types.ObjectId, ref: 'Model', default: [] }] simulations: [{ type: Schema.Types.ObjectId, ref: 'Simulation', default: [] }]
}); });
userSchema.methods.verifyPassword = function(password, callback) { userSchema.methods.verifyPassword = function(password, callback) {
...@@ -83,14 +83,14 @@ userSchema.pre('remove', function(callback) { ...@@ -83,14 +83,14 @@ userSchema.pre('remove', function(callback) {
}); });
}); });
// delete all models belonging to this user // delete all simulations belonging to this user
this.simulationModels.forEach(function(id) { this.simulations.forEach(function(id) {
SimulationModel.findOne({ _id: id }, function(err, model) { Simulation.findOne({ _id: id }, function(err, simulation) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
model.remove(function(err) { simulation.remove(function(err) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
......
...@@ -14,7 +14,7 @@ var auth = require('../auth'); ...@@ -14,7 +14,7 @@ var auth = require('../auth');
// models // models
var SimulationModel = require('../models/simulationModel'); var SimulationModel = require('../models/simulationModel');
var User = require('../models/user'); var Simulation = require('../models/simulation');
// create router // create router
var router = express.Router(); var router = express.Router();
...@@ -46,15 +46,15 @@ router.post('/simulationModels', function(req, res) { ...@@ -46,15 +46,15 @@ router.post('/simulationModels', function(req, res) {
res.send({ simulationModel: model }); res.send({ simulationModel: model });
}); });
// add model to user // add model to simulation
User.findOne({ _id: model.owner }, function(err, user) { Simulation.findOne({ _id: model.simulation }, function(err, simulation) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
user.simulationModels.push(model._id); simulation.models.push(model._id);
user.save(function(err) { simulation.save(function(err) {
if (err) { if (err) {
console.log(err); console.log(err);
} }
...@@ -101,20 +101,20 @@ router.delete('/simulationModels/:id', function(req, res) { ...@@ -101,20 +101,20 @@ router.delete('/simulationModels/:id', function(req, res) {
return res.send(err); return res.send(err);
} }
// remove from owner's list // remove from simulation's list
User.findOne({ _id: model.owner }, function(err, user) { Simulation.findOne({ _id: model.simulation }, function(err, simulation) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
for (var i = 0; user.simulationModels.length; i++) { for (var i = 0; simulation.models.length; i++) {
var id = String(user.simulationModels[i]); var id = String(simulation.models[i]);
if (id == model._id) { if (id == model._id) {
user.simulationModels.splice(i, 1); simulation.models.splice(i, 1);
} }
} }
user.save(function(err) { simulation.save(function(err) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
......
/**
* File: simulations.js
* Author: Markus Grigull <mgrigull@eonerc.rwth-aachen.de>
* Date: 26.07.2016
* Copyright: 2016, Institute for Automation of Complex Power Systems, EONERC
* This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential.
* Unauthorized copying of this file, via any medium is strictly prohibited.
**********************************************************************************/
// include
var express = require('express');
var auth = require('../auth');
// models
var Simulation = require('../models/simulation');
var User = require('../models/user');
// create router
var router = express.Router();
// all model routes need authentication
router.use('/simulations', auth.validateToken);
// routes
router.get('/simulations', function(req, res) {
// get all simulations
Simulation.find(function(err, simulations) {
if (err) {
return res.send(err);
}
res.send({ simulations: simulations });
});
});
router.post('/simulations', function(req, res) {
// create new simulation
var simulation = new Simulation(req.body.simulation);
simulation.save(function(err) {
if (err) {
return res.send(err);
}
res.send({ simulation: simulation });
});
// add simulation to user
User.findOne({ _id: simulation.owner }, function(err, user) {
if (err) {
return console.log(err);
}
user.simulations.push(simulation._id);
user.save(function(err) {
if (err) {
console.log(err);
}
});
});
});
router.put('/simulations/:id', function(req, res) {
// get simulation
Simulation.findOne({ _id: req.params.id }, function(err, simulation) {
if (err) {
return res.send(err);
}
// update all properties
for (property in req.body.simulation) {
simulation[property] = req.body.simulation[property];
}
// save the changes
simulation.save(function(err) {
if (err) {
return res.send(err);
}
res.send({ simulation: simulation });
});
});
});
router.get('/simulations/:id', function(req, res) {
Simulation.findOne({ _id: req.params.id }, function(err, simulation) {
if (err) {
return res.send(err);
}
res.send({ simulation: simulation });
});
});
router.delete('/simulations/:id', function(req, res) {
Simulation.findOne({ _id: req.params.id }, function(err, simulation) {
if (err) {
return res.send(err);
}
// remove from owner's list
User.findOne({ _id: simulation.owner }, function(err, user) {
if (err) {
return console.log(err);
}
for (var i = 0; user.simulations.length; i++) {
var id = String(user.simulations[i]);
if (id == simulation._id) {
user.simulations.splice(i, 1);
}
}
user.save(function(err) {
if (err) {
return console.log(err);
}
});
});
simulation.remove(function(err) {
if (err) {
return res.send(err);
}
res.send({});
});
});
});
module.exports = router;
...@@ -21,6 +21,7 @@ var users = require('./routes/users'); ...@@ -21,6 +21,7 @@ var users = require('./routes/users');
var projects = require('./routes/projects'); var projects = require('./routes/projects');
var visualizations = require('./routes/visualizations'); var visualizations = require('./routes/visualizations');
var plots = require('./routes/plots'); var plots = require('./routes/plots');
var simulations = require('./routes/simulations');
var simulationModels = require('./routes/simulationModels'); var simulationModels = require('./routes/simulationModels');
var User = require('./models/user'); var User = require('./models/user');
...@@ -42,6 +43,7 @@ app.use('/api/v1', users); ...@@ -42,6 +43,7 @@ app.use('/api/v1', users);
app.use('/api/v1', projects); app.use('/api/v1', projects);
app.use('/api/v1', visualizations); app.use('/api/v1', visualizations);
app.use('/api/v1', plots); app.use('/api/v1', plots);
app.use('/api/v1', simulations);
app.use('/api/v1', simulationModels); app.use('/api/v1', simulationModels);
// catch 404 and forward to error handler // catch 404 and forward to error handler
......
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