Commit 7c9d9a5b by Jose Antonio

Working on scenes - collections controller

parent 7e362156
...@@ -44,14 +44,29 @@ module.exports = { ...@@ -44,14 +44,29 @@ module.exports = {
delete scene.categories;//To avoid update these fields delete scene.categories;//To avoid update these fields
delete scene.supervisor; delete scene.supervisor;
delete scene.student; delete scene.student;
scene.name = req.param('name') || instruction.name; scene.name = req.param('name') || scene.name;
scene.active = req.param('active') || instruction.active; scene.active = req.param('active') || scene.active;
scene.save(function (error) { scene.save(function (error) {
if (error) { if (error) {
res.serverError(); res.serverError();
} else { } else {
res.ok(scene); res.ok(scene);
} }
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);
}
})
});
}
}); });
}) })
.catch(function () { .catch(function () {
...@@ -100,6 +115,30 @@ module.exports = { ...@@ -100,6 +115,30 @@ module.exports = {
}).catch(function (err){ }).catch(function (err){
return res.serverError("Error finding scene "+err); return res.serverError("Error finding scene "+err);
}); });
} },
//
// Logs a scene action and broadcast to anyone subscribed to this student
scene: function (req, res) {
var action = req.param('action');
var scene = req.param('scene');
var roomName = 'studentRoom' + scene.student;
if (req.isSocket) {
sails.log.debug("Inside scene - isSocket");
// Send to all sockets subscribed to this room except the own socket that sends the message
// Parameters: room, action, data to send, socket to avoid sending (the socket that send this)
sails.sockets.broadcast(roomName, 'scene', {
"action": action,
"scene": scene
}, req.socket);
res.json({
msg: "Action " + action + " in scene " + scene.id + " from student " + scene.student
});
}
},
}; };
...@@ -147,6 +147,22 @@ module.exports = function eventsHook(sails) { ...@@ -147,6 +147,22 @@ module.exports = function eventsHook(sails) {
attributes: attributes attributes: attributes
} }
}; };
},
/**
* Scene is updated
* @param {action} type of the action
* @param {attributes} attributes of the action (id_stu, stu_picto)
* @return {Object} {name, data}
*/
scene: function (action, scene) {
return {
name: 'scene',
data: {
scene: scene,
action: action
}
};
} }
}; };
}; };
...@@ -73,6 +73,9 @@ module.exports.routes = { ...@@ -73,6 +73,9 @@ module.exports.routes = {
'DELETE /picto/:id': 'PictoController.destroy', 'DELETE /picto/:id': 'PictoController.destroy',
'DELETE /picto/:id_sup/tag/:id_tag': 'PictoController.del_tag', 'DELETE /picto/:id_sup/tag/:id_tag': 'PictoController.del_tag',
/// Websocket request for propagating actions add, delete, modify...
'POST /scene': 'SceneController.scene',
'GET /scene/:id': 'SceneController.getScene', 'GET /scene/:id': 'SceneController.getScene',
'POST /scene/:id': 'SceneController.create', 'POST /scene/:id': 'SceneController.create',
'PUT /scene/:id': 'SceneController.update', 'PUT /scene/:id': 'SceneController.update',
......
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