1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

Replace PATCH with PUT, which enables editing multiple fields at once.

Closes #47.
This commit is contained in:
Jari Bakken 2014-11-14 12:56:23 +01:00 committed by Ivar Conradi Østhus
parent dc14bfc173
commit d2ce90b394
7 changed files with 50 additions and 88 deletions

View File

@ -2,7 +2,6 @@ var eventStore = require('./eventStore'),
eventType = require('./eventType'), eventType = require('./eventType'),
featureDb = require('./featureDb'); featureDb = require('./featureDb');
module.exports = function (app) { module.exports = function (app) {
app.get('/features', function (req, res) { app.get('/features', function (req, res) {
@ -53,29 +52,29 @@ module.exports = function (app) {
featureDb.getFeature(newFeature.name).then(handleFeatureExist, handleCreateFeature); featureDb.getFeature(newFeature.name).then(handleFeatureExist, handleCreateFeature);
}); });
app.patch('/features/:featureName', function (req, res) { app.put('/features/:featureName', function (req, res) {
var featureName = req.params.featureName, var featureName = req.params.featureName;
createdBy = req.connection.remoteAddress, var createdBy = req.connection.remoteAddress;
changeRequest = req.body; var updatedFeature = req.body;
changeRequest.name = featureName; updatedFeature.name = featureName;
featureDb.getFeature(featureName).then( var event = {
function () { type: eventType.featureUpdated,
eventStore.create({ createdBy: createdBy,
type: eventType.featureUpdated, data: updatedFeature
createdBy: createdBy, };
data: changeRequest
}).then(function () { featureDb.getFeature(featureName)
res.status(202).end(); .then(function () {
}, function () { eventStore
res.status(500).end(); .create(event)
}); .then(function () { res.status(200).end(); })
}, .catch(function () { res.status(500).end(); });
function () { })
.catch(function () {
res.status(404).end(); res.status(404).end();
} });
);
}); });
}; };

View File

@ -24,40 +24,24 @@ eventStore.on(eventType.featureCreated, function (event) {
); );
eventStore.on(eventType.featureUpdated, function (event) { eventStore.on(eventType.featureUpdated, function (event) {
var sql, params; var feature = event.data;
var changeRequest = event.data;
switch (changeRequest.field) { var sql = 'UPDATE features SET enabled = $1, strategy_name = $2, ' +
case 'enabled': 'description = $3, parameters = $4 WHERE name = $5';
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 params = [
feature.enabled ? 1 : 0,
feature.strategy,
feature.description,
feature.paramaters,
feature.name
];
if(sql && params) { dbPool.query(sql, params, function(err) {
dbPool.query(sql, params, function(err) { if(err) {
if(err) { logger.error('Could not update feature, error was: ', err);
logger.error('Could not update feature, error was: ', err); }
} });
});
} else {
logger.error('Could not handle feature-update event', event);
}
} }
); );

View File

@ -13,11 +13,7 @@ var Feature = React.createClass({
}, },
saveFeature: function(feature) { saveFeature: function(feature) {
this.props.onChange({ this.props.onChange(feature);
name: feature.name,
field: 'enabled',
value: feature.enabled
});
this.toggleEditMode(); this.toggleEditMode();
}, },

View File

@ -56,7 +56,6 @@ var FeatureForm = React.createClass({
type="text" type="text"
ref="description" ref="description"
defaultValue={feature.description} defaultValue={feature.description}
disabled={feature.name.length}
placeholder="Enter description" /> placeholder="Enter description" />
</div> </div>
@ -64,7 +63,6 @@ var FeatureForm = React.createClass({
<select id="strategy" <select id="strategy"
ref="strategy" ref="strategy"
className="" className=""
disabled={feature.name.length}
defaultValue={feature.strategy}> defaultValue={feature.strategy}>
{strategyNodes} {strategyNodes}
</select> </select>

View File

@ -46,17 +46,10 @@ var FeatureTogglesComponent = React.createClass({
this.forceUpdate(); this.forceUpdate();
}, },
updateFeature: function (changeRequest) { updateFeature: function (feature) {
var newFeatures = this.state.features;
newFeatures.forEach(function(f){
if(f.name === changeRequest.name) {
f[changeRequest.field] = changeRequest.value;
}
});
this.setState({features: newFeatures});
this.stopFeaturePoller(); this.stopFeaturePoller();
this.state.featureStore.updateFeature(changeRequest) this.state.featureStore
.updateFeature(feature)
.then(this.startFeaturePoller) .then(this.startFeaturePoller)
.catch(this.handleError); .catch(this.handleError);
}, },
@ -141,6 +134,4 @@ var FeatureTogglesComponent = React.createClass({
} }
}); });
module.exports = FeatureTogglesComponent; module.exports = FeatureTogglesComponent;

View File

@ -7,13 +7,13 @@ FeatureStore.TYPE = 'json';
FeatureStore.CONTENT_TYPE = 'application/json'; FeatureStore.CONTENT_TYPE = 'application/json';
FeatureStore.prototype = { FeatureStore.prototype = {
updateFeature: function (changeRequest) { updateFeature: function (feature) {
return reqwest({ return reqwest({
url: 'features/' + changeRequest.name, url: 'features/' + feature.name,
method: 'patch', method: 'put',
type: FeatureStore.TYPE, type: FeatureStore.TYPE,
contentType: FeatureStore.CONTENT_TYPE, contentType: FeatureStore.CONTENT_TYPE,
data: JSON.stringify(changeRequest) data: JSON.stringify(feature)
}); });
}, },

View File

@ -1,5 +1,5 @@
var request = require('supertest'), var request = require('supertest');
mockery = require('mockery'); var mockery = require('mockery');
describe('The api', function () { describe('The api', function () {
var server; var server;
@ -49,24 +49,18 @@ describe('The api', function () {
it('can not change status of feature toggle that does not exist', function (done) { it('can not change status of feature toggle that does not exist', function (done) {
request request
.patch('/features/shouldNotExsist') .put('/features/should-not-exist')
.send({ .send({name: 'should-not-exist', enabled: false})
'field': 'enabled',
'value': true
})
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.expect(404, done); .expect(404, done);
}); });
it('can change status of feature toggle that does exist', function (done) { it('can change status of feature toggle that does exist', function (done) {
request request
.patch('/features/featureY') .put('/features/featureY')
.send({ .send({name: 'featureY', enabled: true})
'field': 'enabled',
'value': true
})
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.expect(202, done); .expect(200, done);
}); });
}); });