Semaphore/lib/routes/playbook/playbook.js

64 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-08-25 01:02:34 +02:00
var models = require('../../models')
var mongoose = require('mongoose')
var express = require('express')
exports.unauthorized = function (app, template) {
template([
'view'
], {
prefix: 'playbook'
});
}
exports.httpRouter = function (app) {
}
exports.router = function (app) {
var playbook = express.Router();
playbook.get('/', view)
.put('/', save)
.delete('/', remove)
app.param('playbook_id', getPlaybook)
app.use('/playbook/:playbook_id', playbook);
}
function getPlaybook (req, res, next, id) {
models.Playbook.findOne({
_id: id
2014-08-25 12:35:49 +02:00
}).select('-vault_password').exec(function (err, playbook) {
2014-08-25 01:02:34 +02:00
if (err || !playbook) {
return res.send(404);
}
req.playbook = playbook;
next();
});
}
function view (req, res) {
res.send(req.playbook);
}
function save (req, res) {
req.playbook.name = req.body.name;
req.playbook.location = req.body.location;
if (req.body.vault_password.length > 0) {
req.playbook.vault_password = req.body.vault_password;
}
try {
req.playbook.credential = mongoose.Types.ObjectId(req.body.credential);
} catch (e) {}
req.playbook.save();
res.send(201);
}
function remove (req, res) {
req.playbook.remove(function (err) {
res.send(201);
})
}