-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
149 lines (136 loc) · 3.82 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
const express = require('express')
const app = express()
const cors = require('cors')
require('dotenv').config()
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true});
app.use(cors())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
app.use(bodyParser.urlencoded({extended: false}));
//SCHEMAS AND MODELS
const userSchema = new mongoose.Schema({
username: {type: String, required: true}
});
const exerciseSchema = new mongoose.Schema({
userId: {type: String, required: true},
description: String,
duration: Number,
date: Date
});
const User = mongoose.model("User", userSchema);
const Exercise = mongoose.model("Exercise", exerciseSchema);
//CREATE AND SAVE NEW USER
app.post("/api/users", (req, res) => {
const newUser = new User({
username: req.body.username
});
newUser.save((err, data) => {
if (err || !data) {
res.send("Unable to save user");
} else {
res.json({"username": data.username, "_id": data["_id"]});
}
});
});
//ADD NEW EXERCISE
app.post("/api/users/:_id/exercises", (req, res) => {
const id = req.params["_id"];
const {description, duration} = req.body;
let date = req.body.date + "T12:00:00Z";
if (!req.body.date) {//if no date provided
date = new Date();
}
User.findById(id, (err, userData) => {
if (err || !userData) {
res.send("User not found");
} else {
const newExercise = new Exercise({
userId: id,
description,
duration,
date: new Date(date)
});
newExercise.save((err, data) => {
if (err || !data) {
res.send("Error saving exercise");
} else {
const {description, duration, date, _id} = data;
res.json({
username: userData.username,
description,
duration,
date: date.toDateString(),
_id: userData.id
})
}
});
}
});
});
//GET EXERCISE LOGS
app.get("/api/users/:_id/logs", (req, res) => {
const {from, to, limit} = req.query;
const id = req.params["_id"];
User.findById(id, (err, userData) => {
if (err || !userData) {
res.send("User not found");
} else {
let dateObj = {};
if (from) {
dateObj["$gte"] = new Date(from);
}
if (to) {
dateObj["$lte"] = new Date(to);
}
let filter = {
userId: id
};
if (from || to) {
filter.date = dateObj;
}
let nonNullLim = limit ?? 50;
Exercise.find(filter).limit(nonNullLim).exec((err, data) => {
if (err || !data) {
res.json([]);
} else {
const count = data.length;
const rawLog = data;
const {username, _id} = userData;
const log = rawLog.map((l) => ({
description: l.description,
duration: l.duration,
date: l.date.toDateString()
}));
res.json({username, count, _id, log});
}
});
}
});
});
//GET LIST OF ALL USERS
app.get("/api/users", (req, res) => {
User.find({}, (err, data) => {
if (err || !data) {
res.send("No users");
} else {
res.json(data);
}
});
});
/* //DELETE TEST USERS/EXERCISES
const delTests = () => {
User.deleteMany({username: /test/gi}, function(err, data) {
// use description prop for deleting exercises
//or username prop for deleting users
if (err) return console.error(err);
});
};
delTests();
*/
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})