forked from freeCodeCamp/boilerplate-project-exercisetracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (112 loc) · 3.28 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
const express = require('express');
const app = express();
const cors = require('cors');
require('dotenv').config();
const mongoose = require('mongoose');
app.use(cors());
app.use(express.static('public'));
app.use(express.urlencoded({ extended: false }));
// Connect to MongoDB database
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Create User schema and model
const userSchema = new mongoose.Schema({
username: String,
});
const User = mongoose.model('User', userSchema);
// Create Exercise schema and model
const exerciseSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
description: String,
duration: Number,
date: {
type: Date,
default: Date.now,
},
});
const Exercise = mongoose.model('Exercise', exerciseSchema);
// POST endpoint to create a new user
app.post('/api/users', async (req, res) => {
try {
const username = req.body.username;
const user = new User({ username });
await user.save();
res.json({ username: user.username, _id: user._id });
} catch (error) {
res.status(400).send('Error creating user');
}
});
// GET endpoint to retrieve all users
app.get('/api/users', async (req, res) => {
try {
const users = await User.find({}, '_id username');
res.json(users);
} catch (error) {
res.status(400).send('Error fetching users');
}
});
// POST endpoint to add an exercise for a user
app.post('/api/users/:_id/exercises', async (req, res) => {
try {
const userId = req.params._id;
const { description, duration, date } = req.body;
const exercise = new Exercise({
userId,
description,
duration,
date: date ? new Date(date) : Date.now(),
});
await exercise.save();
// Get user object with the exercise fields added
const user = await User.findById(userId, '_id username');
user.exercise = {
description: exercise.description,
duration: exercise.duration,
date: exercise.date.toDateString(),
};
res.json(user);
} catch (error) {
res.status(400).send('Error adding exercise');
}
});
// GET endpoint to retrieve exercise log of a user
app.get('/api/users/:_id/logs', async (req, res) => {
try {
const userId = req.params._id;
let { from, to, limit } = req.query;
const query = { userId };
if (from || to) {
query.date = {};
if (from) query.date.$gte = new Date(from);
if (to) query.date.$lte = new Date(to);
}
let exercises = await Exercise.find(query)
.select('description duration date -_id')
.limit(limit ? parseInt(limit) : null);
exercises = exercises.map(exercise => ({
description: exercise.description,
duration: exercise.duration,
date: exercise.date.toDateString(),
}));
const user = await User.findById(userId, '_id username');
res.json({
_id: user._id,
username: user.username,
count: exercises.length,
log: exercises,
});
} catch (error) {
res.status(400).send('Error fetching exercise log');
}
});
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);
});