diff --git a/unleash-server/lib/api.js b/unleash-server/lib/api.js index 533d5acb7d..e71ea8828c 100644 --- a/unleash-server/lib/api.js +++ b/unleash-server/lib/api.js @@ -1,40 +1,36 @@ -var featuresMock = require('./featuresMock'); +var db = require('./db'); -function getFeature(name) { - var featureFound; - featuresMock.forEach(function (feature) { - if (feature.name === name) { - featureFound = feature; - } - }); - return featureFound; -} module.exports = function (app) { app.get('/features', function (req, res) { - res.json(featuresMock); + db.getFeatures().then(function (features) { + res.json(features); + }); }); app.get('/features/:id', function (req, res) { - var feature = getFeature(req.params.id); - - if (feature) { - res.json(feature); - } else { - res.json(404, {error: 'Could not find feature'}); - } + db.getFeature.then(function (feature) { + if (feature) { + res.json(feature); + } else { + res.json(404, {error: 'Could not find feature'}); + } + }); }); app.post('/features', function (req, res) { var newFeature = req.body; - if (!getFeature(newFeature.name)) { - featuresMock.push(newFeature); - res.status(201).end(); - } else { - res.status(500).end(); - } + db.getFeature(newFeature.name).then(function (feature) { + if (feature) { + res.status(500).end(); + } else { + db.addFeature(newFeature).then(function () { + res.status(201).end(); + }); + } + }); }); }; diff --git a/unleash-server/lib/db.js b/unleash-server/lib/db.js new file mode 100644 index 0000000000..37a9fd166b --- /dev/null +++ b/unleash-server/lib/db.js @@ -0,0 +1,28 @@ +var Promise = require('bluebird'); +var featuresMock = require('./featuresMock'); + +function getFeature(name) { + var featureFound; + featuresMock.forEach(function (feature) { + if (feature.name === name) { + featureFound = feature; + } + }); + + return Promise.resolve(featureFound); +} + +function getFeatures() { + return Promise.resolve(featuresMock); +} + +function addFeature(feature) { + featuresMock.push(feature); + return Promise.resolve(); +} + +module.exports = { + getFeature: getFeature, + getFeatures: getFeatures, + addFeature: addFeature +}; \ No newline at end of file