Skip to content

Instantly share code, notes, and snippets.

@mackwic
Last active December 25, 2015 02:28
Show Gist options
  • Save mackwic/6902350 to your computer and use it in GitHub Desktop.
Save mackwic/6902350 to your computer and use it in GitHub Desktop.
A partial example of one of our controllers
var Sha1 = require('server_common').crypto;
var auth = require('server_common').auth;
module.exports = function(app, models, config) {
var photoController = function () {};
photoController.mountRoutes = function () {
var auth_f = auth.ensure_authenticated({ roles: 'doctor' });
app.get('/api/photo', auth_f, photoController.load);
app.post('/api/photo', auth_f, photoController.save);
app.put('/api/photo/:id/setcover', auth_f, photoController.setCover);
app.delete('/api/photo', auth_f, photoController.delete);
app.get('/api/photo/getToken', auth_f, photoController.generateSign);
};
photoController.save = function(req, res) {
var src = req.body.src || req.query.src;
new models.photo({ ref_id: req.user.ref_id, source:src }).save();
res.send(200);
};
photoController.setCover = function(req, res) {
// First remove cover flag on all pictures
models.photo.update({'ref_id': req.user.ref_id}, {is_cover: false}, {multi: true}, function(err, data) {
if (err) {
return res.send(500, err);
}
// Then set cover flag on given picture
models.photo.update({'ref_id': req.user.ref_id, '_id': req.params.id}, {is_cover: true}, function(err, data) {
if (err) {
return res.send(500, err);
}
return res.send(200);
})
});
}; // (...)
photoController.mountRoutes();
return photoController;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment