-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
283 lines (202 loc) · 5.68 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const express = require('express');
const app = express();
const cors = require('cors');
const bodyParser = require('body-parser');
const crypto = require('crypto');
require('dotenv').config();
app.use(cors());
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
// app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
let users = [];
let exercises = [];
app.get('/api/users', (req, res) => {
res.json(users);
});
app.post('/api/users', (req, res) => {
const username = req.body.username;
if (username !== "") {
const id = crypto.createHash('sha1').update(username, 'utf8').digest('hex');
const newUserObj = {"_id": id, "username": username };
console.log("New user: " + username + " and id: " + id);
users.push(newUserObj);
res.json(newUserObj);
return;
}
});
const findUserById = function(id) {
for (var i=0; i<users.length; i++) {
if (users[i]["_id"] == id) {
return users[i];
}
}
return null;
};
app.get('/api/users/exercises', (req, res) => {
res.sendStatus(404);
});
app.post('/api/users/:_id/exercises', (req, res) => {
const id = req.params["_id"];
const description = req.body["description"];
const durationStr = req.body["duration"];
const duration = parseInt(durationStr);
const dateStr = req.body["date"];
// if empty -> 404 not found
if (id == "") {
res.sendStatus(404);
console.log("New exercise failed: no id given\n");
return;
}
const user = findUserById(id);
if (user == null) {
res.sendStatus(404);
console.log("New exercise failed: couldn't find user\n");
return;
}
const username = user["username"];
if (description == "") {
res.status(400).send("Path `description` is required.");
console.log("New exercise failed: description is required\n");
return;
}
if (durationStr == "") {
res.status(400).send("Path `duration` is required.");
console.log("New exercise failed: duration is required\n");
return;
}
if (isNaN(duration)) {
res.status(400).send("Path `duration` is not a number.");
console.log("New exercise failed: duration is not a number\n");
return;
}
let date = new Date(dateStr);
if (dateStr == "" || dateStr == undefined) {
date = new Date();
}
if (date == "Invalid Date") {
res.status(400).send("Path `date` is not a valid date.");
console.log("New exercise failed: date is not valid " + dateStr + "\n");
return;
}
let exercise = JSON.parse(JSON.stringify(user));
exercise["date"] = date.toDateString();
exercise["duration"] = duration;
exercise["description"] = description;
console.log("New exercise for id: " + id);
exercises.push(exercise);
console.log(exercise);
console.log("\n");
res.json(exercise);
return;
});
const findExercisesById = function(id, from, to, limit) {
let findings = [];
for (var i=0; i < exercises.length; i++) {
if (exercises[i]["_id"] == id) {
const dateStr = exercises[i]["date"];
const date = new Date(dateStr);
if (from != null) {
if (date < from) {
continue;
}
}
if (to != null) {
if (date > to) {
continue;
}
}
if (limit != null && findings.length >= limit) {
break;
}
const finding = {
"description": exercises[i]["description"],
"duration": parseInt(exercises[i]["duration"]),
"date": dateStr
};
// console.log(finding);
findings.push(finding);
}
}
return findings;
};
app.get('/api/users/:id/logs', (req, res) => {
const id = req.params["id"];
let logString = "Log request for: " + id + "/logs";
let firstQuery = true;
// handle from query
const fromStr = req.query["from"];
let from = null;
if (fromStr != undefined) {
from = new Date(fromStr);
if (from == "Invalid Date") {
res.status(400).send("Query `from` is not a valid date.");
console.log("Log request failed: " + id + " from: " + fromStr);
return;
}
if (firstQuery == true) {
firstQuery = false;
logString += "?";
}
logString += "from="+fromStr+"&";
}
// handle to query
const toStr = req.query["to"];
let to = null;
if (toStr != undefined) {
to = new Date(toStr);
if (to == "Invalid Date") {
res.status(400).send("Query `to` is not a valid date.");
console.log("Log request failed: " + id + " to: " + toStr);
return;
}
if (firstQuery == true) {
firstQuery = false;
logString += "?";
}
logString += "to="+toStr+"&";
}
// handle limit query
const limitStr = req.query["limit"];
let limit = null;
if (limitStr != undefined) {
limit = parseInt(limitStr);
if (isNaN(limit)) {
res.status(400).send("Query `limit` is not number.");
console.log("Log request failed: " + id + " limit: " + limitStr);
return;
}
if (firstQuery == true) {
firstQuery = false;
logString += "?";
}
logString += "limit="+limit+"&";
}
// remove last &
logString = logString.substr(0, logString.length - 1);
console.log(logString);
// if empty -> 404 not found
if (id == "") {
res.sendStatus(404);
return;
}
const user = findUserById(id);
if (user == null) {
res.sendStatus(404);
return;
}
const findings = findExercisesById(id, from, to, limit);
let logs = {
"username": user["username"],
"count": findings.length,
"_id": id,
"log": findings
};
res.json(logs);
return;
});
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})