-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
84 lines (63 loc) · 2.13 KB
/
app.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
72
73
74
75
76
77
78
79
80
81
82
83
var express = require('express');
var Db = require('./node_modules/mongodb').Db,
Connection = require('./node_modules/mongodb').Connection,
Server = require('./node_modules/mongodb').Server;
var dbURL = "mongodb://localhost:27017/test";
var app = express.createServer();
app.listen(3000);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/ndireport.js', function (req, res) {
res.sendfile(__dirname + '/ndireport.js');
});
app.use(express.static(__dirname + '/public'));
app.get('/baseball.html', function (req, res) {
res.sendfile(__dirname + '/baseball.html');
});
app.get('/bb/:position/:year', function (req, res) {
switch(req.params.year) {
case "2013":
get2013Projections(req.params.position,res);
break;
case "Spring":
getSpringStats(req.params.position,res);
break;
default:
getActualStats(req.params.position,req.params.year,res);
}
});
function get2013Projections(position,res) {
Db.connect(dbURL, function(err, db) {
var collection = db.collection(position);
var sortKey = position=="pitchers"? ['mSOA',-1] :['mOPS',-1];
collection.find({"$or":[{'LG':'AL'},{'lg':'AL'}]}, {'sort':[['mOPS', -1]]}).toArray(function(err, documents) {
res.json(documents);
db.close();
res.end();
});
});
}
function getActualStats(position,year,res) {
Db.connect(dbURL, function(err, db) {
var collection = db.collection(position + year + "Web");
var sortKey = position=="pitchers"? ['p_ip',-1] :['ops',-1];
collection.find({}, {'sort':[sortKey]}).toArray(function(err, documents) {
res.json(documents);
db.close();
res.end();
});
});
}
function getSpringStats(position,res) {
Db.connect(dbURL, function(err, db) {
position = position == "batters" ? "Batters" : "Pitchers";
var collection = db.collection("spring" + position + "2012Web");
var sortKey = position=="Pitchers"? ['p_ip',-1] :['ops',-1];
collection.find({}, {'sort':[sortKey]}).toArray(function(err, documents) {
res.json(documents);
db.close();
res.end();
});
});
}