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

Clean up strategyApi with chained-promises

This commit is contained in:
ivaosthu 2014-12-10 19:11:52 +01:00
parent 553f20c3a6
commit 00da8d1e3b
3 changed files with 42 additions and 29 deletions

View File

@ -6,7 +6,7 @@ var featureDb = require('./featureDb');
var NameExistsError = require('./error/NameExistsError');
var NotFoundError = require('./error/NotFoundError');
var ValidationError = require('./error/ValidationError');
var requestValidator = require('./error/requestValidator');
var validateRequest = require('./error/validateRequest');
module.exports = function (app) {
@ -28,7 +28,7 @@ module.exports = function (app) {
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
requestValidator(req)
validateRequest(req)
.then(validateUniqueName)
.then(function() {
return eventStore.create({

View File

@ -1,6 +1,11 @@
var eventStore = require('./eventStore');
var eventType = require('./eventType');
var strategyDb = require('./strategyDb');
var Promise = require("bluebird");
var eventStore = require('./eventStore');
var eventType = require('./eventType');
var strategyDb = require('./strategyDb');
var logger = require('./logger');
var NameExistsError = require('./error/NameExistsError');
var ValidationError = require('./error/ValidationError');
var validateRequest = require('./error/validateRequest');
module.exports = function (app) {
@ -32,34 +37,42 @@ module.exports = function (app) {
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
var errors = req.validationErrors();
if (errors) {
res.status(400).json(errors);
return;
}
var newStrategy = req.body;
var handleStrategyExists = function() {
var errors = [{msg: "A strategy named " + newStrategy.name + " already exists."}];
res.status(403).json(errors);
};
var handleCreateStrategy = function() {
eventStore.create({
type: eventType.strategyCreated,
createdBy: req.connection.remoteAddress,
data: newStrategy
validateRequest(req)
.then(validateStrategyName)
.then(function() {
return eventStore.create({
type: eventType.strategyCreated,
createdBy: req.connection.remoteAddress,
data: newStrategy
});
})
.then(function () { res.status(201).end(); })
.catch(function () { res.status(500).end(); });
};
strategyDb.getStrategy(newStrategy.name)
.then(handleStrategyExists)
.catch(handleCreateStrategy);
.then(function () {
res.status(201).end();
})
.catch(NameExistsError, function() {
res.status(403).json([{msg: "A strategy named '" + req.body.name + "' already exists."}]).end();
})
.catch(ValidationError, function() {
res.status(400).json(req.validationErrors());
})
.catch(function(err) {
logger.error("Could not create strategy", err);
res.status(500).end();
});
});
function validateStrategyName(req) {
return new Promise(function(resolve, reject) {
strategyDb.getStrategy(req.body.name)
.then(function() {
reject(new NameExistsError("Feature name already exist"));
}, function() {
resolve(req);
});
});
}
};