diff --git a/lib/eventType.js b/lib/eventType.js index 1dc8897ea6..029516a712 100644 --- a/lib/eventType.js +++ b/lib/eventType.js @@ -1,5 +1,6 @@ module.exports = { featureCreated : 'feature-created', featureUpdated : 'feature-updated', - strategyCreated: 'strategy-created' + strategyCreated: 'strategy-created', + strategyDeleted: 'strategy-deleted' }; \ No newline at end of file diff --git a/lib/strategyApi.js b/lib/strategyApi.js index ec46c0f411..facc307ebf 100644 --- a/lib/strategyApi.js +++ b/lib/strategyApi.js @@ -16,6 +16,18 @@ module.exports = function (app) { .catch(function () { res.json(404, {error: 'Could not find strategy'}); }); }); + app.delete('/strategies/:name', function (req, res) { + eventStore.create({ + type: eventType.strategyDeleted, + createdBy: req.connection.remoteAddress, + data: { + name: req.params.name + } + }) + .then(function () { res.status(200).end(); }) + .catch(function () { res.status(500).end(); }); + }); + app.post('/strategies', function (req, res) { req.checkBody('name', 'Name is required').notEmpty(); req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i); diff --git a/lib/strategyDb.js b/lib/strategyDb.js index b8f9f3bcc1..9c23053f5c 100644 --- a/lib/strategyDb.js +++ b/lib/strategyDb.js @@ -12,6 +12,15 @@ eventStore.on(eventType.strategyCreated, function (event) { }); }); +eventStore.on(eventType.strategyDeleted, function (event) { + knex('strategies') + .where('name', event.data.name) + .del() + .catch(function (err) { + logger.error('Could not delete strategy, error was: ', err); + }); +}); + function getStrategies() { return knex .select(STRATEGY_COLUMNS) diff --git a/test/strategyApiSpec.js b/test/strategyApiSpec.js index 5da09317ac..78f9cba6aa 100644 --- a/test/strategyApiSpec.js +++ b/test/strategyApiSpec.js @@ -44,5 +44,9 @@ describe('The strategy api', function () { .expect(403, done); }); - + it('deletes a new strategy', function (done) { + request + .delete('/strategies/deletable') + .expect(200, done); + }); }); \ No newline at end of file diff --git a/test/strategyDbMock.js b/test/strategyDbMock.js index ebc1400c64..148e20620e 100644 --- a/test/strategyDbMock.js +++ b/test/strategyDbMock.js @@ -11,6 +11,10 @@ var strategies = [ parametersTemplate: { emails: "String" } + }, + { + name: "deletable", + description: "deletable" } ];