From 94f78797dfbc19b32105476d4a6a2845fcb7ae3d Mon Sep 17 00:00:00 2001 From: andsandv Date: Thu, 12 Feb 2015 11:41:30 +0100 Subject: [PATCH] Check if strategy exsit before deleting it --- lib/strategyApi.js | 32 +++++++++++++++++++++++--------- lib/strategyDb.js | 3 ++- test/strategyApiSpec.js | 9 ++++++++- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/strategyApi.js b/lib/strategyApi.js index a5ebb8fac0..460889640c 100644 --- a/lib/strategyApi.js +++ b/lib/strategyApi.js @@ -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) { diff --git a/lib/strategyDb.js b/lib/strategyDb.js index b5c2b4527c..8a0bec2d9b 100644 --- a/lib/strategyDb.js +++ b/lib/strategyDb.js @@ -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 { diff --git a/test/strategyApiSpec.js b/test/strategyApiSpec.js index 65a9f28d0e..7882ec64d5 100644 --- a/test/strategyApiSpec.js +++ b/test/strategyApiSpec.js @@ -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); + }); + }); \ No newline at end of file