diff --git a/lib/featureApi.js b/lib/featureApi.js index 0c830ad3c2..f0165ca149 100644 --- a/lib/featureApi.js +++ b/lib/featureApi.js @@ -2,7 +2,6 @@ var eventStore = require('./eventStore'), eventType = require('./eventType'), featureDb = require('./featureDb'); - module.exports = function (app) { app.get('/features', function (req, res) { @@ -53,29 +52,29 @@ module.exports = function (app) { featureDb.getFeature(newFeature.name).then(handleFeatureExist, handleCreateFeature); }); - app.patch('/features/:featureName', function (req, res) { - var featureName = req.params.featureName, - createdBy = req.connection.remoteAddress, - changeRequest = req.body; + app.put('/features/:featureName', function (req, res) { + var featureName = req.params.featureName; + var createdBy = req.connection.remoteAddress; + var updatedFeature = req.body; - changeRequest.name = featureName; + updatedFeature.name = featureName; - featureDb.getFeature(featureName).then( - function () { - eventStore.create({ - type: eventType.featureUpdated, - createdBy: createdBy, - data: changeRequest - }).then(function () { - res.status(202).end(); - }, function () { - res.status(500).end(); - }); - }, - function () { + 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 () { res.status(404).end(); - } - ); + }); }); }; diff --git a/lib/featureDb.js b/lib/featureDb.js index abd20f4b78..757cf40924 100644 --- a/lib/featureDb.js +++ b/lib/featureDb.js @@ -24,40 +24,24 @@ eventStore.on(eventType.featureCreated, function (event) { ); eventStore.on(eventType.featureUpdated, function (event) { - var sql, params; - var changeRequest = event.data; + var feature = event.data; - switch (changeRequest.field) { - case 'enabled': - sql = 'UPDATE features SET enabled = $1 WHERE name=$2'; - params = [event.data.value ? 1 : 0, event.data.name]; - break; - case 'strategy': - sql = 'UPDATE features SET strategy_name = $1 WHERE name=$2'; - params = [event.data.value, event.data.name]; - break; - case 'description': - sql = 'UPDATE features SET description = $1 WHERE name=$2'; - params = [event.data.value, event.data.name]; - break; - case 'parameters': - sql = 'UPDATE features SET parameters = $1 WHERE name=$2'; - params = [event.data.value, event.data.name]; - break; - default: - break; - } + var sql = 'UPDATE features SET enabled = $1, strategy_name = $2, ' + + 'description = $3, parameters = $4 WHERE name = $5'; + var params = [ + feature.enabled ? 1 : 0, + feature.strategy, + feature.description, + feature.paramaters, + feature.name + ]; - if(sql && params) { - dbPool.query(sql, params, function(err) { - if(err) { - logger.error('Could not update feature, error was: ', err); - } - }); - } else { - logger.error('Could not handle feature-update event', event); - } + dbPool.query(sql, params, function(err) { + if(err) { + logger.error('Could not update feature, error was: ', err); + } + }); } ); diff --git a/public/js/components/feature/Feature.jsx b/public/js/components/feature/Feature.jsx index 7ef160c6b0..752ae7dba3 100644 --- a/public/js/components/feature/Feature.jsx +++ b/public/js/components/feature/Feature.jsx @@ -13,11 +13,7 @@ var Feature = React.createClass({ }, saveFeature: function(feature) { - this.props.onChange({ - name: feature.name, - field: 'enabled', - value: feature.enabled - }); + this.props.onChange(feature); this.toggleEditMode(); }, diff --git a/public/js/components/feature/FeatureForm.jsx b/public/js/components/feature/FeatureForm.jsx index 037f6f0032..a0ea72b344 100644 --- a/public/js/components/feature/FeatureForm.jsx +++ b/public/js/components/feature/FeatureForm.jsx @@ -56,7 +56,6 @@ var FeatureForm = React.createClass({ type="text" ref="description" defaultValue={feature.description} - disabled={feature.name.length} placeholder="Enter description" /> @@ -64,7 +63,6 @@ var FeatureForm = React.createClass({ diff --git a/public/js/components/feature/FeatureTogglesComponent.jsx b/public/js/components/feature/FeatureTogglesComponent.jsx index cf19dd57e0..d4a0bf762b 100644 --- a/public/js/components/feature/FeatureTogglesComponent.jsx +++ b/public/js/components/feature/FeatureTogglesComponent.jsx @@ -46,17 +46,10 @@ var FeatureTogglesComponent = React.createClass({ this.forceUpdate(); }, - updateFeature: function (changeRequest) { - var newFeatures = this.state.features; - newFeatures.forEach(function(f){ - if(f.name === changeRequest.name) { - f[changeRequest.field] = changeRequest.value; - } - }); - - this.setState({features: newFeatures}); + updateFeature: function (feature) { this.stopFeaturePoller(); - this.state.featureStore.updateFeature(changeRequest) + this.state.featureStore + .updateFeature(feature) .then(this.startFeaturePoller) .catch(this.handleError); }, @@ -141,6 +134,4 @@ var FeatureTogglesComponent = React.createClass({ } }); - - module.exports = FeatureTogglesComponent; \ No newline at end of file diff --git a/public/js/stores/FeatureStore.js b/public/js/stores/FeatureStore.js index 79b9a3646d..7dc52c5d5a 100644 --- a/public/js/stores/FeatureStore.js +++ b/public/js/stores/FeatureStore.js @@ -7,13 +7,13 @@ FeatureStore.TYPE = 'json'; FeatureStore.CONTENT_TYPE = 'application/json'; FeatureStore.prototype = { - updateFeature: function (changeRequest) { + updateFeature: function (feature) { return reqwest({ - url: 'features/' + changeRequest.name, - method: 'patch', + url: 'features/' + feature.name, + method: 'put', type: FeatureStore.TYPE, contentType: FeatureStore.CONTENT_TYPE, - data: JSON.stringify(changeRequest) + data: JSON.stringify(feature) }); }, diff --git a/test/featureApiSpec.js b/test/featureApiSpec.js index 84f6a9b4b9..93467af68d 100644 --- a/test/featureApiSpec.js +++ b/test/featureApiSpec.js @@ -1,5 +1,5 @@ -var request = require('supertest'), - mockery = require('mockery'); +var request = require('supertest'); +var mockery = require('mockery'); describe('The api', function () { var server; @@ -49,24 +49,18 @@ describe('The api', function () { it('can not change status of feature toggle that does not exist', function (done) { request - .patch('/features/shouldNotExsist') - .send({ - 'field': 'enabled', - 'value': true - }) + .put('/features/should-not-exist') + .send({name: 'should-not-exist', enabled: false}) .set('Content-Type', 'application/json') .expect(404, done); }); it('can change status of feature toggle that does exist', function (done) { request - .patch('/features/featureY') - .send({ - 'field': 'enabled', - 'value': true - }) + .put('/features/featureY') + .send({name: 'featureY', enabled: true}) .set('Content-Type', 'application/json') - .expect(202, done); + .expect(200, done); }); }); \ No newline at end of file