1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

Check if strategy exsit before deleting it

This commit is contained in:
andsandv 2015-02-12 11:41:30 +01:00 committed by Ivar Conradi Østhus
parent 23e9475ca7
commit 0552e588ae
3 changed files with 33 additions and 11 deletions

View File

@ -5,6 +5,7 @@ var strategyDb = require('./strategyDb');
var logger = require('./logger');
var NameExistsError = require('./error/NameExistsError');
var ValidationError = require('./error/ValidationError');
var NotFoundError = require('./error/NotFoundError');
var validateRequest = require('./error/validateRequest');
var extractUser = require('./extractUser');
@ -23,15 +24,28 @@ module.exports = function (app) {
});
app.delete('/strategies/:name', function (req, res) {
eventStore.create({
type: eventType.strategyDeleted,
createdBy: extractUser(req),
data: {
name: req.params.name
}
})
.then(function () { res.status(200).end(); })
.catch(function () { res.status(500).end(); });
var strategyName = req.params.name;
strategyDb.getStrategy(strategyName)
.then(function() {
return eventStore.create({
type: eventType.strategyDeleted,
createdBy: extractUser(req),
data: {
name: strategyName
}
});
})
.then(function() {
res.status(200).end();
})
.catch(NotFoundError, function() {
res.status(404).end();
})
.catch(function(err) {
logger.error('Could not delete strategy='+strategyName, err);
res.status(500).end();
});
});
app.post('/strategies', function (req, res) {

View File

@ -2,6 +2,7 @@ var eventStore = require('./eventStore');
var eventType = require('./eventType');
var logger = require('./logger');
var knex = require('./dbPool');
var NotFoundError = require('./error/NotFoundError');
var STRATEGY_COLUMNS = ['name', 'description', 'parameters_template'];
eventStore.on(eventType.strategyCreated, function (event) {
@ -35,7 +36,7 @@ function getStrategy(name) {
function rowToStrategy(row) {
if (!row) {
throw new Error('invalid row');
throw new NotFoundError('No strategy found');
}
return {

View File

@ -55,7 +55,14 @@ describe('The strategy api', function () {
it('deletes a new strategy', function (done) {
request
.delete('/strategies/deletable')
.delete('/strategies/usersWithEmail')
.expect(200, done);
});
it('can\'t delete a strategy that dose not exist', function(done) {
request
.delete('/strategies/unknown')
.expect(404, done);
});
});