Commit 7dbcfa6e by Jose Antonio

Del, Update, copy working on scenes

parent 3c8c565f
......@@ -34,9 +34,27 @@ module.exports = {
// Update a scene data
//
update: function (req, res) {
var params= req.allParams();
if (typeof req.params.id == 'undefined' || !req.params.id)
return res.badRequest("scene id not defined");
Scene.findOne({ id: req.params.id }).then(function (scene) {
//If active field is true, check if other scene is active before
if(params.active){
Scene.findOne({active:true, student:params.id_stu})
.then(function(scene){
if(scene.id != req.params.id){
//Is not same scene so it might be deactivated
scene.active = false;
scene.save(function(err){
if(err){
return res.serverError("Could not deactivate previos active_scene");
}
});
}
}).catch(function(err){
return res.serverError("Could not find active_scene");
});
}
Scene.findOne({ id: params.id }).then(function (scene) {
if (!scene) {
res.badRequest();
throw new Error('Scene not found');
......@@ -44,33 +62,38 @@ module.exports = {
delete scene.categories;//To avoid update these fields
delete scene.supervisor;
delete scene.student;
scene.name = req.param('name') || scene.name;
scene.active = req.param('active') || scene.active;
scene.name = params.name || scene.name;
if (typeof params.active == 'undefined' || !params.active)
scene.active = scene.active;
else
scene.active = params.active;
scene.save(function (error) {
if (error) {
res.serverError();
} else {
res.ok(scene);
return res.serverError("Could not save scene "+ error);
}
var cat = false;
if(scene.active){
Student.findOne({id:scene.student})
.then(student => {
student.attributes.categories=scene.categories;
delete student.password;
student.save(function(error){
if(error){
res.serverError("Error saving student data");
}
else{
return res.ok(scene);
}
})
});
cat=true;
}
Student.findOne({id:scene.student})
.then(student => {
student.attributes.categories=cat;
delete student.password;
student.save(function(error){
if(error){
return res.serverError("Error saving student data");
}
else{
return res.ok(scene);
}
})
});
});
})
.catch(function () {
res.serverError();
.catch(function (err) {
res.serverError("Could not find scene "+err);
});
},
......@@ -131,28 +154,19 @@ module.exports = {
categories: scene.categories,
supervisor: scene.supervisor,
student: scene.student
}).then(scene=>{
async.forEach(scene.stuPictos, function (stuPicto, cb) {
}).then(newScene=>{
scene.stuPictos.forEach(function (stuPicto, cb) {
StuPicto.create({
student: stuPicto.student,
picto: stuPicto.picto,
scene: scene.id,
scene: newScene.id,
attributes: stuPicto.attributes
}).catch(function (err){
console.log("Error creating stu_picto "+err.details);
sails.log.error(err.details);
});
},
function (err) { // function called when loop is done
if (err) {
console.log(err.details);
sails.log.error(err.details);
return res.json({
'error': err.details
});
} else
return res.ok(scene);
});
return res.ok(newScene);
}).catch(function (err){
return res.serverError("Error creating scene: " + err);
......
......@@ -258,12 +258,13 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
});
};
// Delete student picto
// Update student scene
$scope.update_scene = function (scene) {
$http.put(config.backend + '/scene/' + scene.id, {
name: scene.name,
active: scene.active})
active: scene.active,
id_stu:scene.student})
.success(function () {
var data= {name: scene.name,
active: scene.active,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment