forked from freeCodeCamp/boilerplate-project-exercisetracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (110 loc) · 2.94 KB
/
index.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
const express = require('express')
const app = express()
const cors = require('cors')
require('dotenv').config()
const bodyparser = require('body-parser');
const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.connect(process.env['CONNECTION'])
const UserSchema = new Schema({
username: String,
});
const User = mongoose.model("User", UserSchema);
const ExerciseSchema = new Schema({
user_id: {type: String, required: true },
description: String,
duration: Number,
date: Date,
});
const Exercise = mongoose.model("Exercise", ExerciseSchema);
app.use(bodyparser.urlencoded({extended:true}))
app.use(cors())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
app.get("/api/users" , async(req,res)=> {
const users = await User.find({}).select("_id username");
if (!users) {
res.send("No users");
} else {
res.json(users);
}
})
app.post("/api/users", async(req, res) => {
const userObj = new User({
username: req.body.username
})
try{
const user = await userObj.save()
console.log(user)
res.json(user)
}catch (err){
console.log(err)
}
});
app.post("/api/users/:_id/exercises", async(req, res) => {
const id = req.params._id;
const {description, duration, date } = req.body
try{
const user = await User.findById(id);
if(!user){
res.send("Could not find user")
}else{
const exerciseObj = new Exercise({
user_id: user._id,
description,
duration,
date: date ? new Date(date) : new Date()
})
const exercise = await exerciseObj.save()
res.json({
_id: user._id,
username: user.username,
description: exercise.description,
duration: exercise.duration,
date: new Date(exercise.date).toDateString()
})
}
}catch(err) {
console.log(err);
res.send("There was an error saving the exercise")
}
})
app.get("/api/users/:_id/logs", async (req, res) => {
const { from, to, limit } = req.query;
const id = req.params._id;
const user = await User.findById(id);
if(!user){
res.send("Could not find user")
return;
}
let dateObj = {}
if (from) {
dateObj["$gte"] = new Date(from)
}
if (to){
dateObj["$lte"] = new Date(to)
}
let filter = {
user_id: id
}
if (from || to){
filter.date = dateObj;
}
const exercises = await Exercise.find(filter).limit(+limit ?? 500)
const log = exercises.map(e =>({
description: e.description,
duration: e.duration,
date: e.date.toDateString()
}))
res.json({
username: user.username,
count: exercises.length,
_id: user._id,
log
})
});
const listener = app.listen(process.env['PORT'] || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})