Skip to content

Commit bce0e5c

Browse files
committed
dictifies model on retreival
1 parent 089cd17 commit bce0e5c

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

fba-api.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ var App = global.App = require('./lib/App');
1010
// ==== DB Connection ====
1111
connection = App.Lib('connection')
1212
connection.mongodb(); //wat
13-
mysqlConn = new Object();
14-
connection.mysql(function(connection) {
15-
mysqlConn = connection;
16-
});
13+
// mysqlConn = new Object();
14+
// connection.mysql(function(connection) {
15+
// mysqlConn = connection;
16+
// });
1717

1818
// above hasn't finished yet which is this is still {}. #callbacks #yolo
19-
console.log(mysqlConn)
19+
// console.log(mysqlConn)
2020

2121
// ==== Apply global middleware ====
2222
App.MW('global-middleware').apply(app);

lib/router.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
var fs = require('fs');
22
var path = require('path');
33

4-
var routerLogger = App.Lib('init-routers-logger');
4+
var routerLogger = App.Lib('init-routers-logger');
55

66
var routesDir = path.resolve('routes');
77
var validRouterFile = 'index.js'
8-
var ignoredInvalidRouters = ['.swp'];
8+
var ignoredInvalidRouters = ['.swp', '.DS_Store'];
99

1010
function pathmaker (rPath) {
1111
// remove everything up to and including '/route'
@@ -16,7 +16,13 @@ function pathmaker (rPath) {
1616
for (var i = 0; i < rPathArray.length - 1; i++) {
1717
i > 0 ? rPath += '/' + rPathArray[i] : rPath += rPathArray[i];
1818
}
19-
return rPath;
19+
20+
// replace all '$'s with ':'s
21+
while(rPath.indexOf('$') !== -1) {
22+
rPath = rPath.replace('$', ':');
23+
}
24+
25+
return rPath;
2026
}
2127

2228

@@ -29,19 +35,19 @@ function findRouters (rPath, app) {
2935
findRouters(fullPath, app);
3036
} else {
3137
var fileName = fullPath.split('/')[fullPath.split('/').length - 1];
32-
38+
3339
var currentPath = pathmaker(fullPath);
3440

35-
if (fileName === validRouterFile) {
41+
if (fileName === validRouterFile) {
3642
var currentRouter = require(fullPath);
3743
currentRouter.stack.forEach(function(router){
38-
routerLogger(router, currentPath);
44+
routerLogger(router, currentPath);
3945
});
4046
console.log(currentRouter.stack[0].name)
4147
app.use(currentPath, currentRouter);
4248
} else {
4349
if (ignoredInvalidRouters.indexOf(path.extname(fileName)) === -1) {
44-
console.log('Error'.red + ': encountered invalid router file → '+ currentPath.red + '/'.red + fileName.red);
50+
console.log('Error'.red + ': encountered invalid router file → '+ currentPath.red + '/'.red + fileName.red);
4551
}
4652
}
4753
}

models/metabolicmodel.js

+16
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,20 @@ var MetabolicModelSchema = new mongoose.Schema({
3636
id: String
3737
});
3838

39+
MetabolicModelSchema.methods.dictifyReactionMetabolites = function dictifyReactionMetabolites(cb) {
40+
var model = JSON.parse(JSON.stringify(this));
41+
42+
model.reactions.forEach(function(reaction) {
43+
metabolitesDict = new Object();
44+
45+
reaction.metabolites.forEach(function(metabolite) {
46+
metabolitesDict[metabolite.id] = metabolite.stoichiometric_coefficient;
47+
});
48+
49+
reaction.metabolites = metabolitesDict;
50+
});
51+
52+
cb(model);
53+
}
54+
3955
module.exports = mongoose.model('metabolicmodel', MetabolicModelSchema);

routes/model/retrieve/index.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var router = require('express').Router();
22

33
var MetabolicModel = App.Model('metabolicmodel');
44

5-
function find(req, res, next) {
5+
function findAll(req, res, next) {
66
MetabolicModel.find(function(err, models) {
77
var ids = new Array();
88
models.forEach(function(model) {
@@ -13,6 +13,23 @@ function find(req, res, next) {
1313
})
1414
}
1515

16-
router.get('/', find)
16+
router.get('/', findAll);
17+
18+
router.get('/:id', function(req,res, next) {
19+
MetabolicModel.findOne({id: req.params.id}, function(err, model) {
20+
if (err) {
21+
res.status(500).send('500 Internal Server Error\n');
22+
return;
23+
}
24+
25+
if (!model) {
26+
res.status(403).send('204 no content. That model does not exist.\n');
27+
} else {
28+
model.dictifyReactionMetabolites(function(model) {
29+
res.send(model);
30+
});
31+
}
32+
});
33+
});
1734

1835
module.exports = router;

0 commit comments

Comments
 (0)