2014-12-10 18:51:27 +01:00
|
|
|
var Promise = require("bluebird");
|
|
|
|
var logger = require('./logger');
|
|
|
|
var eventStore = require('./eventStore');
|
|
|
|
var eventType = require('./eventType');
|
|
|
|
var featureDb = require('./featureDb');
|
|
|
|
var NameExistsError = require('./error/NameExistsError');
|
|
|
|
var NotFoundError = require('./error/NotFoundError');
|
|
|
|
var ValidationError = require('./error/ValidationError');
|
2014-12-17 21:56:27 +01:00
|
|
|
var validateRequest = require('./error/validateRequest');
|
2015-02-05 13:39:00 +01:00
|
|
|
var extractUser = require('./extractUser');
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2014-10-20 13:03:43 +02:00
|
|
|
module.exports = function (app) {
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2014-10-20 13:03:43 +02:00
|
|
|
app.get('/features', function (req, res) {
|
2014-10-23 09:50:23 +02:00
|
|
|
featureDb.getFeatures().then(function (features) {
|
2014-10-21 15:56:17 +02:00
|
|
|
res.json({features: features});
|
2014-10-21 13:10:15 +02:00
|
|
|
});
|
2014-10-20 15:12:30 +02:00
|
|
|
});
|
|
|
|
|
2014-10-23 16:00:51 +02:00
|
|
|
app.get('/features/:featureName', function (req, res) {
|
2014-11-14 16:58:05 +01:00
|
|
|
featureDb.getFeature(req.params.featureName)
|
|
|
|
.then(function (feature) { res.json(feature); })
|
|
|
|
.catch(function () {
|
2014-11-03 12:43:23 +01:00
|
|
|
res.status(404).json({error: 'Could not find feature'});
|
2014-11-14 16:58:05 +01:00
|
|
|
});
|
2014-10-20 15:12:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/features', function (req, res) {
|
2014-11-01 11:47:21 +01:00
|
|
|
req.checkBody('name', 'Name is required').notEmpty();
|
|
|
|
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
|
|
|
|
|
2014-12-10 19:11:52 +01:00
|
|
|
validateRequest(req)
|
2014-12-10 18:49:08 +01:00
|
|
|
.then(validateUniqueName)
|
2014-12-10 18:45:02 +01:00
|
|
|
.then(function() {
|
|
|
|
return eventStore.create({
|
|
|
|
type: eventType.featureCreated,
|
2015-02-05 13:39:00 +01:00
|
|
|
createdBy: extractUser(req),
|
2014-12-10 18:45:02 +01:00
|
|
|
data: req.body
|
|
|
|
});
|
2014-11-25 15:28:08 +01:00
|
|
|
})
|
2014-12-10 18:45:02 +01:00
|
|
|
.then(function () {
|
|
|
|
res.status(201).end();
|
|
|
|
})
|
|
|
|
.catch(NameExistsError, function() {
|
2014-12-17 21:56:27 +01:00
|
|
|
res.status(403).json([{
|
|
|
|
msg: "A feature named '" + req.body.name + "' already exists. It could be archived."
|
|
|
|
}]).end();
|
2014-12-10 18:45:02 +01:00
|
|
|
})
|
|
|
|
.catch(ValidationError, function() {
|
|
|
|
res.status(400).json(req.validationErrors());
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
logger.error("Could not create feature toggle", err);
|
|
|
|
res.status(500).end();
|
|
|
|
});
|
|
|
|
});
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2014-11-14 12:56:23 +01:00
|
|
|
app.put('/features/:featureName', function (req, res) {
|
|
|
|
var featureName = req.params.featureName;
|
2015-02-05 13:39:00 +01:00
|
|
|
var userName = extractUser(req);
|
2014-11-14 12:56:23 +01:00
|
|
|
var updatedFeature = req.body;
|
|
|
|
|
|
|
|
updatedFeature.name = featureName;
|
|
|
|
|
|
|
|
featureDb.getFeature(featureName)
|
|
|
|
.then(function () {
|
2014-12-10 18:45:02 +01:00
|
|
|
return eventStore.create({
|
|
|
|
type: eventType.featureUpdated,
|
|
|
|
createdBy: userName,
|
|
|
|
data: updatedFeature
|
|
|
|
});
|
2014-11-14 12:56:23 +01:00
|
|
|
})
|
2014-12-10 18:45:02 +01:00
|
|
|
.then(function () {
|
|
|
|
res.status(200).end();
|
|
|
|
})
|
|
|
|
.catch(NotFoundError, function () {
|
2014-10-22 15:52:43 +02:00
|
|
|
res.status(404).end();
|
2014-12-10 18:45:02 +01:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error("Could not update feature toggle="+featureName, err);
|
|
|
|
res.status(500).end();
|
2014-11-14 12:56:23 +01:00
|
|
|
});
|
2014-10-21 15:28:10 +02:00
|
|
|
});
|
2014-12-10 18:49:08 +01:00
|
|
|
|
2014-12-15 22:40:07 +01:00
|
|
|
app.delete('/features/:featureName', function (req, res) {
|
|
|
|
var featureName = req.params.featureName;
|
2015-02-05 13:39:00 +01:00
|
|
|
var userName = extractUser(req);
|
2014-12-15 22:40:07 +01:00
|
|
|
|
|
|
|
featureDb.getFeature(featureName)
|
|
|
|
.then(function () {
|
|
|
|
return eventStore.create({
|
2015-02-09 18:28:51 +01:00
|
|
|
type: eventType.featureArchived,
|
2014-12-15 22:40:07 +01:00
|
|
|
createdBy: userName,
|
|
|
|
data: {
|
|
|
|
name: featureName
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
res.status(200).end();
|
|
|
|
})
|
|
|
|
.catch(NotFoundError, function () {
|
|
|
|
res.status(404).end();
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error("Could not archive feature="+featureName, err);
|
|
|
|
res.status(500).end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-10 18:49:08 +01:00
|
|
|
function validateUniqueName(req) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
featureDb.getFeature(req.body.name)
|
|
|
|
.then(function() {
|
|
|
|
reject(new NameExistsError("Feature name already exist"));
|
|
|
|
}, function() {
|
|
|
|
resolve(req);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-10-20 15:12:30 +02:00
|
|
|
};
|