101 lines
2.0 KiB
JavaScript
101 lines
2.0 KiB
JavaScript
const errors = require('restify-errors');
|
|
|
|
const Install = require('../models/install');
|
|
|
|
module.exports = function (server, auth) {
|
|
server.post('/installs', auth.basic, (req, res, next) => {
|
|
|
|
let data = req.body || {};
|
|
|
|
let install = new Install(data);
|
|
install.save(function(err) {
|
|
if (err) {
|
|
console.error(err);
|
|
return next(new errors.InternalError(err.message));
|
|
next();
|
|
}
|
|
|
|
res.send(201);
|
|
next();
|
|
});
|
|
});
|
|
|
|
server.get('/installs', auth.manager, (req, res, next) => {
|
|
Install.apiQuery(req.params, function(err, docs) {
|
|
if (err) {
|
|
console.error(err);
|
|
return next(
|
|
new errors.InvalidContentError(err),
|
|
);
|
|
}
|
|
|
|
res.send(docs);
|
|
next();
|
|
});
|
|
});
|
|
|
|
server.get('/installs/:install_id', auth.manager, (req, res, next) => {
|
|
Install.findOne({ _id: req.params.install_id }, function(err, doc) {
|
|
if (err) {
|
|
console.error(err);
|
|
return next(
|
|
new errors.InvalidContentError(err),
|
|
);
|
|
}
|
|
|
|
res.send(doc);
|
|
next();
|
|
});
|
|
});
|
|
|
|
server.put('/installs/:install_id', auth.manager, (req, res, next) => {
|
|
|
|
let data = req.body || {};
|
|
|
|
if (!data._id) {
|
|
data = Object.assign({}, data, { _id: req.params.install_id });
|
|
}
|
|
|
|
Install.findOne({ _id: req.params.install_id }, function(err, doc) {
|
|
if (err) {
|
|
console.error(err);
|
|
return next(
|
|
new errors.InvalidContentError(err),
|
|
);
|
|
} else if (!doc) {
|
|
return next(
|
|
new errors.ResourceNotFoundError(
|
|
'The resource you requested could not be found.',
|
|
),
|
|
);
|
|
}
|
|
|
|
Install.updateOne({ _id: data._id }, data, function(err) {
|
|
if (err) {
|
|
console.error(err);
|
|
return next(
|
|
new errors.InvalidContentError(err),
|
|
);
|
|
}
|
|
|
|
res.send(200, data);
|
|
next();
|
|
});
|
|
});
|
|
});
|
|
|
|
server.del('/installs/:install_id', auth.manager, (req, res, next) => {
|
|
Install.deleteOne({ _id: req.params.install_id }, function(err) {
|
|
if (err) {
|
|
console.error(err);
|
|
return next(
|
|
new errors.InvalidContentError(err),
|
|
);
|
|
}
|
|
|
|
res.send(204);
|
|
next();
|
|
});
|
|
});
|
|
};
|