-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (64 loc) · 2.04 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var okJson = function(res) {
res.writeHead(200, {'Content-Type': 'application/json'});
};
var jsonArray = function(cursor, res) {
okJson(res);
res.write('[');
var first = true;
cursor.each(function(err, item) {
if (item) {
if (!first)
res.write(',');
res.write(JSON.stringify(item));
first = false;
} else {
res.write(']');
res.end();
}
});
};
var exercisesFull = function(workouts, cb) {
workouts.aggregate([
{$unwind: '$exercises'},
{$unwind: '$exercises.sets'},
{$group: {_id:{name:'$exercises.name',start:'$start'},
sets:{$push:'$exercises.sets'},
volume:{
$sum:{
$add:[{$multiply:['$exercises.sets.weight','$exercises.sets.reps']},
{$ifNull:[{$multiply:['$exercises.sets.set.weight','$exercises.sets.set.reps']},0]}]
}
}
}},
{$sort: {'_id.start':-1}},
{$group: {_id:'$_id.name', workouts:{$push:{start:'$_id.start',volume:'$volume',sets:'$sets'}}}},
{$project: {_id:0, name:'$_id', workouts:'$workouts'}},
{$sort:{name:1}}
], cb);
};
MongoClient.connect('mongodb://127.0.0.1:27017/lift', function(err, db) {
var workouts = db.collection('workouts');
var server = http.createServer(function(request, response) {
if (request.url == '/api/workouts/full') {
jsonArray(workouts.find().sort({start:-1}), response);
} else if (request.url == '/api/workouts') {
jsonArray(workouts.find({},{"_id":1,start:-1}).sort({start:1}), response);
} else if (request.url == '/api/exercises/full') {
exercisesFull(workouts, function(err, result) {
okJson(response);
// TODO Stream?
response.write(JSON.stringify(result));
response.end();
});
} else {
response.writeHead(404);
response.end();
}
});
server.on('close', function() {
db.close();
});
server.listen(8282, 'localhost');
});