forked from freeCodeCamp/boilerplate-project-exercisetracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
211 lines (179 loc) · 5.12 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const cors = require('cors')
const mongoose = require('mongoose')
mongoose.connect(process.env.MLAB_URI || 'mongodb://localhost/exercise-track' )
app.use(cors())
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
// Star my code ************************************************************************
const userSchema = new Schema({
username: {
type: String,
required: true,
unique: true,
maxlength: [20, 'username too long']
}
});
const Users = mongoose.model('Users', userSchema);
const exerciseSchema = new Schema({
description: {
type: String,
required: true,
maxlength: [20, 'description too long']
},
duration: {
type: Number,
required: true,
min: [1, 'duration too short']
},
date: {
type: Date,
default: Date.now
},
username: String,
userId: {
type: String,
ref: 'Users',
index: true
}
});
// validate userId, and add "username" to the exercise instance
exerciseSchema.pre('save', function(next) {
mongoose.model('Users').findById(this.userId, (err, user) => {
if (err) return next(err);
if (!user) {
const err = new Error('unknown userId')
err.status = 400
return next(err);
}
this.username = user.username
console.log('before', this.date);
if(!this.date) {
this.date = Date.now();
}
console.log('after', this.date);
next();
});
});
const Exercises = mongoose.model('Exercises', exerciseSchema);
app.get('/api/exercise/users', (req, res, next) => {
Users.find({}, (err, data) => {
if (err) next(err);
res.json(data);
})
});
app.post('/api/exercise/new-user', (req, res, next) => {
const user = new Users(req.body);
user.save((err, savedUser) => {
if(err) {
if(err.code == 11000) {
// uniqueness error (no custom message)
return next({
status: 400,
message: 'Username already taken'
})
} else {
return next(err)
}
}
res.json({
username: savedUser.username,
_id: savedUser._id
})
})
});
app.post('/api/exercise/add', (req, res, next) => {
Users.findById(req.body.userId, (err, user) => {
if(err) return next(err)
if(!user) {
return next({
status: 400,
message: 'Unknown userId'
})
}
const exercise = new Exercises(req.body)
exercise.username = user.username
exercise.save((err, savedExercise) => {
if(err) return next(err)
savedExercise = savedExercise.toObject()
delete savedExercise.__v
delete savedExercise._id
console.log('post', savedExercise.date);
savedExercise.date = (new Date(savedExercise.date)).toDateString()
console.log('toString', savedExercise.date);
res.json(savedExercise)
})
})
})
app.get('/api/exercise/log', (req, res, next) => {
const from = new Date(req.query.from)
const to = new Date(req.query.to)
Users.findById(req.query.userId, (err, user) => {
if(err) return next(err);
if(!user) {
return next({ status:400, message: 'Unknown userId' });
}
Exercises
.find({
userId: req.query.userId,
date: {
// $lte: to != 'Invalid Date' ? to.getDate() : Date.now() ,
// $gte: from != 'Invalid Date' ? from.getTime() : 0
$lte: to != 'Invalid Date' ? to.toISOString() : Date.now() ,
$gte: from != 'Invalid Date' ? from.toISOString() : 0
}
}, {
__v: 0,
_id: 0
})
.sort('-date')
.limit(parseInt(req.query.limit))
.exec((err, exercises) => {
if(err) return next(err)
const out = {
userId: req.query.userId,
username: user.username,
from : from != 'Invalid Date' ? from.toDateString() : undefined,
to : to != 'Invalid Date' ? to.toDateString(): undefined,
count: exercises.length,
log: exercises.map(e => ({
description : e.description,
duration : e.duration,
date: e.date.toDateString()
}))
}
res.json(out)
})
})
});
// End my code *************************************************************************
// Not found middleware
app.use((req, res, next) => {
return next({status: 404, message: 'not found'})
})
// Error Handling middleware
app.use((err, req, res, next) => {
let errCode, errMessage
if (err.errors) {
// mongoose validation error
errCode = 400 // bad request
const keys = Object.keys(err.errors)
// report the first validation error
errMessage = err.errors[keys[0]].message
} else {
// generic or custom error
errCode = err.status || 500
errMessage = err.message || 'Internal Server Error'
}
res.status(errCode).type('txt')
.send(errMessage)
})
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})