1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/unleash-server/lib/featureApi.js

74 lines
2.1 KiB
JavaScript
Raw Normal View History

var eventStore = require('./eventStore'),
eventType = require('./eventType'),
featureDb = require('./featureDb');
2014-10-20 15:12:30 +02:00
module.exports = function (app) {
2014-10-20 15:12:30 +02:00
app.get('/features', function (req, res) {
featureDb.getFeatures().then(function (features) {
2014-10-21 15:56:17 +02:00
res.json({features: features});
});
2014-10-20 15:12:30 +02:00
});
2014-10-23 16:00:51 +02:00
app.get('/features/:featureName', function (req, res) {
featureDb.getFeature(req.params.featureName).then(function (feature) {
if (feature) {
res.json(feature);
} else {
res.json(404, {error: 'Could not find feature'});
}
});
2014-10-20 15:12:30 +02:00
});
app.post('/features', function (req, res) {
2014-10-22 16:53:15 +02:00
var newFeature = req.body,
createdBy = req.connection.remoteAddress;
2014-10-20 15:12:30 +02:00
var handleFeatureExist = function() {
res.status(403).end();
};
var handleCreateFeature = function () {
eventStore.create({
type: eventType.featureCreated,
createdBy: createdBy,
data: newFeature
}).then(function () {
res.status(201).end();
}, function () {
res.status(500).end();
});
};
featureDb.getFeature(newFeature.name).then(handleFeatureExist, handleCreateFeature);
});
2014-10-20 15:12:30 +02:00
app.patch('/features/:featureName', function (req, res) {
2014-10-23 16:00:51 +02:00
var featureName = req.params.featureName,
createdBy = req.connection.remoteAddress,
changeRequest = req.body;
changeRequest.name = featureName;
featureDb.getFeature(featureName).then(
function () {
2014-10-23 16:00:51 +02:00
eventStore.create({
type: eventType.featureUpdated,
createdBy: createdBy,
data: changeRequest
}).then(function () {
res.status(202).end();
}, function () {
res.status(500).end();
});
},
function () {
res.status(404).end();
}
);
2014-10-21 15:28:10 +02:00
});
2014-10-20 15:12:30 +02:00
};