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

Add project simulation relationship on change

Add missing simulator attribute in plot model
parent 73a78023
Branches
No related tags found
No related merge requests found
......@@ -15,4 +15,3 @@ This is the backend for the VILLASweb frontend. It is build upon NodeJS, Express
- Handle missing objects (e.g. visualization is removed and belonging project does not exist anymore)
- Save objects creation date
- Add object sharing
- Add project to simulation when updating
......@@ -16,6 +16,7 @@ var Schema = mongoose.Schema;
var plotSchema = new Schema({
name: { type: String, required: true },
signal: { type: String, required: true },
simulator: { type: Number, required: true },
width: { type: Number, required: true },
height: { type: Number, required: true },
title: { type: String },
......
......@@ -11,6 +11,7 @@
var mongoose = require('mongoose');
var SimulationModel = require('./simulationModel');
var Project = require('./project');
var Schema = mongoose.Schema;
......@@ -24,7 +25,7 @@
});
simulationSchema.pre('remove', function(callback) {
// delete all models belonging to this project
// delete all models belonging to this simulation
this.models.forEach(function(id) {
SimulationModel.findOne({ _id: id }, function(err, model) {
if (err) {
......@@ -39,6 +40,21 @@
});
});
// delete all projects belonging to this simulation
this.projects.forEach(function(id) {
Project.findOne({ _id: id }, function(err, project) {
if (err) {
return console.log(err);
}
project.remove(function(err) {
if (err) {
return console.log(err);
}
});
});
});
callback();
});
......
......@@ -15,10 +15,10 @@ var Schema = mongoose.Schema;
// simulation model model
var simulationModelSchema = new Schema({
name: { type: String, required: true },
simulator: { type: Number, required: true },
simulator: { type: Schema.Types.ObjectId, ref: 'Simulator', required: true },
length: { type: Number, default: 1 },
mapping: [{ type: String, default: [] }],
simulation: { type: Schema.Types.ObjectId, ref: 'Simulation' }
simulation: { type: Schema.Types.ObjectId, ref: 'Simulation', required: true }
});
module.exports = mongoose.model('SimulationModel', simulationModelSchema);
......@@ -86,6 +86,79 @@ router.put('/projects/:id', function(req, res) {
return res.status(400).send(err);
}
// update relationships
if (req.body.project.owner && req.body.project.owner !== project.owner) {
// remove from old user
User.findOne({ _id: project.owner }, function(err, user) {
if (err) {
return console.log(err);
}
for (var i = 0; i < user.projects; i++) {
if (user.projects[i] === project._id) {
user.projects.splice(i, 1);
}
}
user.save(function(err) {
if (err) {
return console.log(err);
}
});
});
// add to new user
User.findOne({ _id: req.body.project.owner }, function(err, user) {
if (err) {
return console.log(err);
}
user.projects.push(project._id);
user.save(function(err) {
if (err) {
return console.log(err);
}
});
});
}
if (req.body.project.simulation && req.body.project.simulation !== project.simulation) {
// remove from old simulation
Simulation.findOne({ _id: project.simulation }, function(err, simulation) {
if (err) {
return console.log(err);
}
for (var i = 0; i < simulation.projects; i++) {
if (simulation.projects[i] === project._id) {
simulation.projects.splice(i, 1);
}
}
simulation.save(function(err) {
if (err) {
return console.log(err);
}
});
});
// add to new user
Simulation.findOne({ _id: req.body.project.simulation }, function(err, simulation) {
if (err) {
return console.log(err);
}
simulation.projects.push(project._id);
simulation.save(function(err) {
if (err) {
return console.log(err);
}
});
});
}
// update all properties
for (property in req.body.project) {
project[property] = req.body.project[property];
......
......@@ -70,6 +70,43 @@ router.put('/simulations/:id', function(req, res) {
return res.status(400).send(err);
}
// update relationships
if (req.body.simulation.owner && req.body.simulation.owner !== simulation.owner) {
// remove from old user
User.findOne({ _id: simulation.owner }, function(err, user) {
if (err) {
return console.log(err);
}
for (var i = 0; i < user.simulations; i++) {
if (user.simulations[i] === simulation._id) {
user.simulations.splice(i, 1);
}
}
user.save(function(err) {
if (err) {
return console.log(err);
}
});
});
// add to new user
User.findOne({ _id: req.body.simulation.owner }, function(err, user) {
if (err) {
return console.log(err);
}
user.simulations.push(simulation._id);
user.save(function(err) {
if (err) {
return console.log(err);
}
});
});
}
// update all properties
for (property in req.body.simulation) {
simulation[property] = req.body.simulation[property];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment