2014-11-25 15:28:08 +01:00
|
|
|
var eventStore = require('./eventStore');
|
|
|
|
var eventType = require('./eventType');
|
|
|
|
var featureDb = require('./featureDb');
|
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);
|
|
|
|
|
|
|
|
var errors = req.validationErrors();
|
|
|
|
|
|
|
|
if (errors) {
|
2014-11-03 12:43:23 +01:00
|
|
|
res.status(400).json(errors);
|
2014-11-01 11:47:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var newFeature = req.body;
|
2014-10-20 15:12:30 +02:00
|
|
|
|
2014-10-23 15:17:04 +02:00
|
|
|
var handleFeatureExist = function() {
|
2014-11-14 16:58:05 +01:00
|
|
|
var errors = [
|
|
|
|
{msg: "A feature named '" + newFeature.name + "' already exists."}
|
|
|
|
];
|
|
|
|
|
|
|
|
res.status(403).json(errors).end();
|
2014-10-23 15:17:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var handleCreateFeature = function () {
|
|
|
|
eventStore.create({
|
|
|
|
type: eventType.featureCreated,
|
2014-11-01 11:47:21 +01:00
|
|
|
createdBy: req.connection.remoteAddress,
|
2014-10-23 15:17:04 +02:00
|
|
|
data: newFeature
|
2014-11-25 15:28:08 +01:00
|
|
|
})
|
|
|
|
.then(function () { res.status(201).end(); })
|
|
|
|
.catch(function () { res.status(500).end(); });
|
2014-10-23 15:17:04 +02:00
|
|
|
};
|
|
|
|
|
2014-11-14 16:58:05 +01:00
|
|
|
featureDb.getFeature(newFeature.name)
|
|
|
|
.then(handleFeatureExist)
|
|
|
|
.catch(handleCreateFeature);
|
2014-10-20 13:03:43 +02:00
|
|
|
});
|
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;
|
|
|
|
var createdBy = req.connection.remoteAddress;
|
|
|
|
var updatedFeature = req.body;
|
|
|
|
|
|
|
|
updatedFeature.name = featureName;
|
|
|
|
|
|
|
|
var event = {
|
|
|
|
type: eventType.featureUpdated,
|
|
|
|
createdBy: createdBy,
|
|
|
|
data: updatedFeature
|
|
|
|
};
|
|
|
|
|
|
|
|
featureDb.getFeature(featureName)
|
|
|
|
.then(function () {
|
|
|
|
eventStore
|
|
|
|
.create(event)
|
|
|
|
.then(function () { res.status(200).end(); })
|
|
|
|
.catch(function () { res.status(500).end(); });
|
|
|
|
})
|
|
|
|
.catch(function () {
|
2014-10-22 15:52:43 +02:00
|
|
|
res.status(404).end();
|
2014-11-14 12:56:23 +01:00
|
|
|
});
|
2014-10-21 15:28:10 +02:00
|
|
|
});
|
|
|
|
|
2014-10-20 15:12:30 +02:00
|
|
|
};
|
|
|
|
|