-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (174 loc) · 4.97 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const express = require('express');
const app = express();
const cors = require('cors');
require('dotenv').config();
const mongoose = require('mongoose');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(express.static('public'));
const Schema = mongoose.Schema;
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const userSchema = new mongoose.Schema({
username: { type: String, unique: true },
});
const logSchema = new Schema({
userId: String,
description: String,
duration: Number,
date: Date,
});
const User = mongoose.model('User', userSchema);
const Log = mongoose.model('Log', logSchema);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html');
});
app.post('/api/users', (req, res) => {
const username = req.body.username;
if (username == '') {
res.send('Please enter a username');
} else {
const newUser = new User({ username: username });
newUser.save((err, data) => {
if (err) {
if (err.name === 'MongoServerError' && err.code === 11000) {
res.json({ error: 'Username already taken' });
} else {
res.json({ error: 'Unable to create user' });
}
} else {
res.json({ username: data.username, _id: data._id });
}
});
}
});
app.get('/api/users', (req, res) => {
User.find({}, { __v: 0 }, (err, data) => {
if (err) {
res.json({ error: 'Unable to retrieve users' });
} else {
res.json(data);
}
});
});
app.post('/api/users/:_id/exercises', (req, res) => {
const userId = req.params._id;
const description = req.body.description;
const duration = req.body.duration;
if (!req.body.date) {
date = new Date();
} else {
date = new Date(req.body.date);
}
if (description == '' || duration == '') {
res.send('Please enter a description and duration');
} else {
User.findById(userId, (err, userData) => {
if (err) {
res.json({ error: 'Unable to find user' });
} else {
const newLog = new Log({
userId: userId,
description: description,
duration: duration,
date: date,
});
newLog.save((err, data) => {
if (err) {
console.log(err);
res.json({ error: 'Unable to create log' });
} else {
res.json({
username: userData.username,
description: data.description,
duration: data.duration,
_id: data.userId,
date: data.date.toDateString(),
});
}
});
}
});
}
});
app.get('/api/users/:_id/logs', (req, res) => {
const userId = req.params._id;
const from = req.query.from;
const to = req.query.to;
const limit = req.query.limit;
User.findById(userId, (err, userData) => {
if (err) {
res.json({ error: 'Unable to find user' });
} else {
Log.find(
{ userId: userId },
{
_id: 0,
__v: 0,
},
(err, data) => {
if (err) {
res.json({ error: 'Unable to find logs' });
} else {
if (from) {
data = data.filter((log) => log.date >= new Date(from));
}
if (to) {
data = data.filter((log) => log.date <= new Date(to));
}
if (limit) {
data = data.slice(0, limit);
}
let logArr = [];
for (let i = 0; i < data.length; i++) {
logArr.push({
description: data[i].description,
duration: data[i].duration,
date: new Date(data[i].date).toDateString().slice(0, 16),
});
}
if (from && to) {
res.json({
_id: userData._id,
username: userData.username,
from: new Date(from).toDateString(),
to: new Date(to).toDateString(),
count: data.length,
log: logArr,
});
} else if (from) {
res.json({
_id: userData._id,
username: userData.username,
from: new Date(from).toDateString(),
count: data.length,
log: logArr,
});
} else if (to) {
res.json({
_id: userData._id,
username: userData.username,
to: new Date(to).toDateString(),
count: data.length,
log: logArr,
});
} else {
res.json({
_id: userId,
username: userData.username,
count: data.length,
log: [...logArr],
});
}
}
}
);
}
});
});
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port);
});