-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
125 lines (92 loc) · 3.86 KB
/
routes.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// app/routes.js
// grab the nerd model we just created
var Wod = require('./models/wod');
var WodLog = require('./models/wodlog');
module.exports = function(app) {
app.get('/api/wods', function(req, res) {
// use mongoose to get all wods in the database
Wod.find(function(err, wods) {
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
res.send(err);
res.json(wods); // return all wods in JSON format
});
});
// route to handle creating goes here (app.post)
app.post('/api/wods', function (req, res) {
var wod = new Wod();
wod.name = req.body.name;
wod.description = req.body.description;
wod.imgs = req.body.imgs;
console.log('wod = ' + wod);
wod.save(function(err, wod) {
if (err) return console.error(err);
res.json({ message: 'wod created!'});
});
});
app.post('/api/wods/:wod_id', function(req, res) {
Wod.update({ _id:req.params.wod_id}, { $set: {imgs: req.body.imgs}}, function(error, wod) {
console.log("updated!")
res.json(wod);
});
});
// route to handle delete goes here (app.delete)
app.delete('/api/wods/:wod_id', function(req, res) {
Wod.remove({
_id: req.params.wod_id}, function(err,bear) {
if (err)
res.send(err);
res.json({ message: 'successfully deleted'});
});
});
app.get('/api/wodlogs', function(req, res) {
// use mongoose to get all wodlogs in the database
var begDate = req.query.begDate;
var endDate = req.query.endDate;
WodLog.find(
{
"timeCompleted" :
{
"$gte": begDate, "$lte": endDate
}
},function(err, wodlogs) {
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
res.send(err);
res.json(wodlogs); // return all wodlogs in JSON format
});
});
// route to handle creating goes here (app.post)
app.post('/api/wodlogs', function (req, res) {
var wodlog = new WodLog();
wodlog.wodId = req.body.wodId
wodlog.wodName = req.body.wodName;
wodlog.timeCompleted = req.body.timeCompleted;
console.log('created wodlog = ' + wodlog);
wodlog.save(function(err, wodlog) {
if (err) return console.error(err);
res.json({ wodLog: wodlog});
});
});
// route to handle delete goes here (app.delete)
app.delete('/api/wodlogs/:wodlog_id', function(req, res) {
console.log("made it into routes.js logging");
console.log(req.params.wodlog_id);
WodLog.remove({
_id: req.params.wodlog_id}, function(err,bear) {
if (err)
res.send(err);
res.json({ message: 'successfully deleted'});
});
});
// frontend routes =========================================================
// route to handle all angular requests
// NOTE: you need to place other HTTP requests above this since this is a catch all and will
// re-route all your requests to the home page.
app.get('*', function(req, res) {
console.log("serving up view index.html");
res.sendFile('/public/index.html', { root : __dirname}); // load our public/index.html file
});
};