forked from freeCodeCamp/boilerplate-project-exercisetracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (102 loc) · 2.66 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
require('dotenv').config()
const bodyParser = require('body-parser')
const express = require('express')
const app = express()
const cors = require('cors')
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cors())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true
},
});
const exerciseSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'USER'
},
description: {
type: String,
required: true
},
duration: {
type: Number,
required: true,
},
date: {
type: Date,
required: false,
default: Date.now
}
});
const USER = mongoose.model('USER', userSchema);
const EXERCISE = mongoose.model('EXERCISE', exerciseSchema);
app.post('/api/users', async (req, res) => {
const { username } = req.body;
const user = new USER({
username: username,
});
res.json(await user.save());
});
app.get('/api/users', async (req, res) => {
USER.find().lean().exec(function (err, users) {
return res.end(JSON.stringify(users));
});
});
app.post('/api/users/:_id/exercises', async (req, res) => {
const { _id } = req.params;
const { description, duration, date } = req.body;
const user = await USER.findById(_id);
const exercise = new EXERCISE({
description: description,
duration: duration,
date: date ? new Date(date) : Date.now(),
user: user._id,
});
await exercise.save();
res.json({
_id: user._id,
username: user.username,
date: new Date(exercise.date).toDateString(),
duration: exercise.duration,
description: exercise.description,
});
});
app.get('/api/users/:_id/logs', async (req, res) => {
const { _id } = req.params;
const { from, to, limit } = req.query;
const dateQuery = (from && to) ? {
date: {
$gte: new Date(from),
$lte: new Date(to),
},
} : {};
const user = await USER.findById(_id);
const exercises = await EXERCISE
.find({
user: user._id,
...dateQuery,
})
.limit(parseInt(limit))
.lean();
res.json({
_id: user.id,
username: user.username,
count: exercises.length,
log: exercises.map(exercise => ({
description: exercise.description,
duration: exercise.duration,
date: new Date(exercise.date).toDateString(),
})),
});
});
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})