-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (144 loc) · 3.3 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
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
const express = require('express')
const app = express()
const cors = require('cors')
const User = require('./models/mongoose.js')
const Exercise = require('./models/exercise.js')
require('dotenv').config()
let bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({extended: false}))
app.use(cors())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
//create new user
app.post("/api/users", async(req, res)=>{
let data = new User({
username:req.body.username
})
try{
const user = await data.save()
res.send(user)
}catch(err){
console.log(err)
}
})
app.get("/api/users", async(req, res)=>{
let users = await User.find()
res.send(users)
})
let Id
let Username
let loog
app.post("/api/users/:_id/exercises", async(req, res)=>{
let {description,duration,date}=req.body;
Id = req.params._id
try{
const foundUser = await User.findById(Id)
Username = foundUser.username
if(!foundUser){
console.log("user not found")
}
else{
let exerciseObj = await Exercise({
userid:Id,
username:foundUser.username,
description,
duration: parseInt(duration),
date: date ? new Date(date) : new Date
})
exercise = await exerciseObj.save()
res.json({
_id:Id,
username:foundUser.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")
}
})
//add new exercise for this user
// get log of all users excercises
app.get("/api/users/:_id/logs", async(req, res)=>{
Id = req.params._id
const foundUser = await User.findById(Id)
console.log(foundUser)
if(!foundUser){
console.log("no user found")
}
else{
Username = foundUser.username
let {from,to,limit} = req.query
let dateObj = {}
if(!limit){
limit = 100
}
if(from){
dateObj["$gte"] = new Date(from)
}
if(to){
dateObj["$lte"] = new Date(to)
}
let filter = { Id }
if(from || to){
filter.date = dateObj
}
const exercises = await Exercise.find({userid: Id}).limit(limit)
const log = exercises.map(e => ({
description: e.description,
duration: e.duration,
date: e.date = new Date(e.date).toDateString()
}))
res.json(
{
username:Username,
count: exercises.length,
_id: Id,
log
}
)
}
})
const listener = app.listen(3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})
/*
app.post("/api/users/:_id/exercises", async(req, res)=>{
let {description,duration,date}=req.body;
Id = req.params._id
if(Id.length != 24){
console.log("id must be 24 characters long")
}
else{
const foundUser = await User.findById(Id)
Username = foundUser.username
if(!foundUser){
console.log("user not found")
}
if (!date){
date = new Date()
}
else{
date = new Date(date)
}
let exercise = await Exercise.create({
username:foundUser.username,
description,
duration,
date,
userid:Id
})
res.send({
username:foundUser.username,
description:description,
duration:duration,
date:date.toDateString(),
_id:Id
})
}
})
*/