mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	#13 Sending in a patch request when updating enabled flag
This commit is contained in:
		
							parent
							
								
									50b56fd7a1
								
							
						
					
					
						commit
						8c643d4e8c
					
				@ -27,7 +27,7 @@ module.exports = function (app) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        db.getFeature(newFeature.name).then(function (feature) {
 | 
					        db.getFeature(newFeature.name).then(function (feature) {
 | 
				
			||||||
            if (feature) {
 | 
					            if (feature) {
 | 
				
			||||||
                res.status(500).end();
 | 
					                res.status(403).end();
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                db.addFeature(newFeature).then(function () {
 | 
					                db.addFeature(newFeature).then(function () {
 | 
				
			||||||
                    res.status(201).end();
 | 
					                    res.status(201).end();
 | 
				
			||||||
@ -36,20 +36,20 @@ module.exports = function (app) {
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    app.patch('/features/:id', function (req, res) {
 | 
					    app.patch('/features/:featureName', function (req, res) {
 | 
				
			||||||
        var body = req.body;
 | 
					        var featureName = req.params.featureName;
 | 
				
			||||||
        body.data.name = req.params.id;
 | 
					        db.getFeature(featureName).then(function (feature) {
 | 
				
			||||||
 | 
					            if (feature) {
 | 
				
			||||||
 | 
					                var changeRequest = req.body;
 | 
				
			||||||
                var event = {};
 | 
					                var event = {};
 | 
				
			||||||
 | 
					                event.type = 'feature-update';
 | 
				
			||||||
                event.user = req.connection.remoteAddress;
 | 
					                event.user = req.connection.remoteAddress;
 | 
				
			||||||
        event.comment = body.comment;
 | 
					                event.data = changeRequest;
 | 
				
			||||||
        event.data = body.data;
 | 
					                res.status(202).end();
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
        // console.log(event);
 | 
					                res.status(404).end();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        // db.save(event).then(function () {
 | 
					        });
 | 
				
			||||||
        res.status(204).end();
 | 
					 | 
				
			||||||
        // });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -29,19 +29,20 @@ var FeatureList = React.createClass({
 | 
				
			|||||||
        this.setState({features: data.features});
 | 
					        this.setState({features: data.features});
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    updateFeature: function (feature) {
 | 
					    updateFeature: function (changeRequest) {
 | 
				
			||||||
        var newFeatures = this.state.features;
 | 
					        var newFeatures = this.state.features;
 | 
				
			||||||
        newFeatures.forEach(function(f){
 | 
					        newFeatures.forEach(function(f){
 | 
				
			||||||
            if(f.name === feature.name) {
 | 
					            if(f.name === changeRequest.name) {
 | 
				
			||||||
                f = feature;
 | 
					                f[changeRequest.field] = changeRequest.value;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					        console.log(changeRequest);
 | 
				
			||||||
        reqwest({
 | 
					        reqwest({
 | 
				
			||||||
            url: 'features/' + feature.name,
 | 
					            url: 'features/' + changeRequest.name,
 | 
				
			||||||
            method: 'post',
 | 
					            method: 'patch',
 | 
				
			||||||
            type: 'json',
 | 
					            type: 'json',
 | 
				
			||||||
            data: feature
 | 
					            contentType: 'application/json',
 | 
				
			||||||
 | 
					            data: JSON.stringify(changeRequest)
 | 
				
			||||||
        }).then(function() {
 | 
					        }).then(function() {
 | 
				
			||||||
            this.setState({features: newFeatures});
 | 
					            this.setState({features: newFeatures});
 | 
				
			||||||
        }.bind(this), function() {
 | 
					        }.bind(this), function() {
 | 
				
			||||||
@ -78,9 +79,11 @@ var Feature = React.createClass({
 | 
				
			|||||||
*/
 | 
					*/
 | 
				
			||||||
    handleEnableChange: function(event) {
 | 
					    handleEnableChange: function(event) {
 | 
				
			||||||
        var feature = this.props.feature;
 | 
					        var feature = this.props.feature;
 | 
				
			||||||
 | 
					        this.props.updateFeature({
 | 
				
			||||||
        feature.enabled = event.target.checked;
 | 
					            name: feature.name,
 | 
				
			||||||
        this.props.updateFeature(feature);
 | 
					            field: "enabled",
 | 
				
			||||||
 | 
					            value: event.target.checked
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    render: function () {
 | 
					    render: function () {
 | 
				
			||||||
 | 
				
			|||||||
@ -22,22 +22,31 @@ describe('The api', function () {
 | 
				
			|||||||
    it('creates new feature toggle', function (done) {
 | 
					    it('creates new feature toggle', function (done) {
 | 
				
			||||||
        request
 | 
					        request
 | 
				
			||||||
            .post('/features')
 | 
					            .post('/features')
 | 
				
			||||||
            .send({name: 'com.test.feature', 'status': 'off'})
 | 
					            .send({name: 'com.test.feature', 'enabled': false})
 | 
				
			||||||
            .set('Content-Type', 'application/json')
 | 
					            .set('Content-Type', 'application/json')
 | 
				
			||||||
            .expect(201, done);
 | 
					            .expect(201, done);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it('change status of feature toggle', function (done) {
 | 
					    it('can not change status of feature toggle that dose not exsist', function (done) {
 | 
				
			||||||
        request
 | 
					        request
 | 
				
			||||||
            .patch('/features/com.test.feature')
 | 
					            .patch('/features/shouldNotExsist')
 | 
				
			||||||
            .send({
 | 
					            .send({
 | 
				
			||||||
                'comment': 'patch test of com.test.feature',
 | 
					                'field': 'enabled',
 | 
				
			||||||
                data: {
 | 
					                'value': true
 | 
				
			||||||
                    'status': 'on'
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
            .set('Content-Type', 'application/json')
 | 
					            .set('Content-Type', 'application/json')
 | 
				
			||||||
            .expect(204, done);
 | 
					            .expect(404, done);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it('can change status of feature toggle that dose exsist', function (done) {
 | 
				
			||||||
 | 
					        request
 | 
				
			||||||
 | 
					            .patch('/features/featureY')
 | 
				
			||||||
 | 
					            .send({
 | 
				
			||||||
 | 
					                'field': 'enabled',
 | 
				
			||||||
 | 
					                'value': true
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
 | 
					            .set('Content-Type', 'application/json')
 | 
				
			||||||
 | 
					            .expect(202, done);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user