From b1ccb2e999034cc2809e2b9fad795eeac051a05e Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Wed, 10 Dec 2014 18:49:08 +0100 Subject: [PATCH] Validation logic should probably not be in the db-code --- lib/featureApi.js | 15 ++++++++++++++- lib/featureDb.js | 16 +--------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/featureApi.js b/lib/featureApi.js index b34b6e9079..e123485816 100644 --- a/lib/featureApi.js +++ b/lib/featureApi.js @@ -5,6 +5,8 @@ var featureDb = require('./featureDb'); var NameExistsError = require('./error/NameExistsError'); var NotFoundError = require('./error/NotFoundError'); var ValidationError = require('./error/ValidationError'); +var Promise = require("bluebird"); + var requestValidator = require('./error/requestValidator'); module.exports = function (app) { @@ -28,7 +30,7 @@ module.exports = function (app) { req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i); requestValidator(req) - .then(featureDb.validateUniqueName) + .then(validateUniqueName) .then(function() { return eventStore.create({ type: eventType.featureCreated, @@ -77,5 +79,16 @@ module.exports = function (app) { res.status(500).end(); }); }); + + function validateUniqueName(req) { + return new Promise(function(resolve, reject) { + featureDb.getFeature(req.body.name) + .then(function() { + reject(new NameExistsError("Feature name already exist")); + }, function() { + resolve(req); + }); + }); + } }; diff --git a/lib/featureDb.js b/lib/featureDb.js index 3b94289b3a..0c132fa7b4 100644 --- a/lib/featureDb.js +++ b/lib/featureDb.js @@ -2,9 +2,7 @@ var eventStore = require('./eventStore'); var eventType = require('./eventType'); var logger = require('./logger'); var knex = require('./dbPool'); -var Promise = require("bluebird"); var NotFoundError = require('./error/NotFoundError'); -var NameExistsError = require('./error/NameExistsError'); var FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategy_name', 'parameters']; eventStore.on(eventType.featureCreated, function (event) { @@ -64,20 +62,8 @@ function eventToRow(event) { }; } -function validateUniqueName(req) { - return new Promise(function(resolve, reject) { - getFeature(req.body.name) - .then(function() { - reject(new NameExistsError("Feature name already exist")); - }, function() { - resolve(req); - }); - }); -} - module.exports = { getFeatures: getFeatures, - getFeature: getFeature, - validateUniqueName: validateUniqueName + getFeature: getFeature };