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'),
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({
var event = {
type: eventType.featureUpdated,
createdBy: createdBy,
data: changeRequest
}).then(function () {
res.status(202).end();
}, function () {
res.status(500).end();
});
},
function () {
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();
}
);
});
});
};

View File

@ -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);
}
}
);

View File

@ -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();
},

View File

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

View File

@ -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;

View File

@ -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)
});
},

View File

@ -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);
});
});