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

Move mock feature data behind a naive db interface.

This commit is contained in:
Jari Bakken 2014-10-21 13:10:15 +02:00 committed by Ivar Conradi Østhus
parent b77112d948
commit f7fb65329c
2 changed files with 48 additions and 24 deletions

View File

@ -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();
});
}
});
});
};

28
unleash-server/lib/db.js Normal file
View File

@ -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
};