2014-10-23 15:17:04 +02:00
|
|
|
var eventStore = require('./eventStore'),
|
2014-10-23 09:50:23 +02:00
|
|
|
eventType = require('./eventType'),
|
|
|
|
featureDb = require('./featureDb');
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2014-10-23 15:17:04 +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
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/features/:id', function (req, res) {
|
2014-10-23 15:17:04 +02:00
|
|
|
featureDb.getFeature(req.params.id).then(function (feature) {
|
2014-10-21 13:10:15 +02:00
|
|
|
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,
|
2014-10-23 12:03:46 +02:00
|
|
|
createdBy = req.connection.remoteAddress;
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2014-10-23 15:17:04 +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 13:03:43 +02:00
|
|
|
});
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2014-10-22 15:52:43 +02:00
|
|
|
app.patch('/features/:featureName', function (req, res) {
|
|
|
|
var featureName = req.params.featureName;
|
2014-10-23 15:17:04 +02:00
|
|
|
|
|
|
|
featureDb.getFeature(featureName).then(
|
|
|
|
function () {
|
2014-10-22 15:52:43 +02:00
|
|
|
var changeRequest = req.body;
|
|
|
|
var event = {};
|
2014-10-23 15:17:04 +02:00
|
|
|
event.type = eventType.featureUpdated;
|
2014-10-22 15:52:43 +02:00
|
|
|
event.user = req.connection.remoteAddress;
|
|
|
|
event.data = changeRequest;
|
|
|
|
res.status(202).end();
|
2014-10-23 15:17:04 +02:00
|
|
|
},
|
|
|
|
function () {
|
2014-10-22 15:52:43 +02:00
|
|
|
res.status(404).end();
|
|
|
|
}
|
2014-10-23 15:17:04 +02:00
|
|
|
);
|
2014-10-21 15:28:10 +02:00
|
|
|
});
|
|
|
|
|
2014-10-20 15:12:30 +02:00
|
|
|
};
|
|
|
|
|