-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
133 lines (110 loc) · 3.51 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
require("dotenv").config();
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const mongoose = require("mongoose");
mongoose.connect(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.use(cors());
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/index.html");
});
const listener = app.listen(process.env.PORT || 3000, () => {
console.log("Your app is listening on port " + listener.address().port);
});
let exerciseSessionSchema = new mongoose.Schema({
description: { type: String, required: true },
duration: { type: Number, required: true },
date: String,
});
let userSchema = new mongoose.Schema({
username: { type: String, required: true },
log: [exerciseSessionSchema],
});
let Session = mongoose.model("Session", exerciseSessionSchema);
let User = mongoose.model("User", userSchema);
app.post(
"/api/exercise/new-user",
bodyParser.urlencoded({ extended: false }),
(request, response) => {
let newUser = new User({ username: request.body.username });
newUser.save((error, savedUser) => {
if (!error) {
let responseObject = {};
responseObject["username"] = savedUser.username;
responseObject["_id"] = savedUser.id;
response.json(responseObject);
}
});
}
);
app.get("/api/exercise/users", (request, response) => {
User.find({}, (error, arrayOfUsers) => {
if (!error) {
response.json(arrayOfUsers);
}
});
});
app.post(
"/api/exercise/add",
bodyParser.urlencoded({ extended: false }),
(request, response) => {
let newSession = new Session({
description: request.body.description,
duration: parseInt(request.body.duration),
date: request.body.date,
});
if (newSession.date === "") {
newSession.date = new Date().toISOString().substring(0, 10);
}
User.findByIdAndUpdate(
request.body.userId,
{ $push: { log: newSession } },
{ new: true },
(error, updatedUser) => {
if (!error) {
let responseObject = {};
responseObject["_id"] = updatedUser.id;
responseObject["username"] = updatedUser.username;
responseObject["date"] = new Date(newSession.date).toDateString();
responseObject["description"] = newSession.description;
responseObject["duration"] = newSession.duration;
response.json(responseObject);
}
}
);
}
);
app.get("/api/exercise/log", (request, response) => {
User.findById(request.query.userId, (error, result) => {
if (!error) {
let responseObject = result;
if (request.query.from || request.query.to) {
let fromDate = new Date(0);
let toDate = new Date();
if (request.query.from) {
fromDate = new Date(request.query.from);
}
if (request.query.to) {
toDate = new Date(request.query.to);
}
fromDate = fromDate.getTime();
toDate = toDate.getTime();
responseObject.log = responseObject.log.filter((session) => {
let sessionDate = new Date(session.date).getTime();
return sessionDate >= fromDate && sessionDate <= toDate;
});
}
if (request.query.limit) {
responseObject.log = responseObject.log.slice(0, request.query.limit);
}
responseObject = responseObject.toJSON();
responseObject["count"] = result.log.length;
response.json(responseObject);
}
});
});