Compare commits

..

13 Commits

2 changed files with 149 additions and 1454 deletions
+149 -3
View File
@@ -19,7 +19,7 @@ const exerciseLogsSchema = new mongoose.Schema({
{ {
description: { type: String, required: true }, description: { type: String, required: true },
duration: { type: Number, required: true }, duration: { type: Number, required: true },
date: { type: String, required: true }, date: { type: Number, required: true },
}, },
], ],
}); });
@@ -51,6 +51,152 @@ app.post("/api/users", (req, res) => {
}); });
}); });
const listener = app.listen(PORT, () => { app.get("/api/users", (req, res) => {
console.log("Your app is listening on port " + listener.address().port); exerciseLogs
.find()
.select({ username: true, _id: true })
.exec()
.then((users) => {
res.json(users);
})
.catch((err) => {
res.sendStatus(500);
res.json({ error: `Error while retrieving users list:\n${err}` });
});
});
const getDateInTicks = (date) => {
// current time in tick if no date specified
if (!date) date = new Date().getTime();
// Convert String to Number if date specified in ticks
if (!Number.isNaN(Number(date))) date = Number(date);
// get date ticks from ticks Number or date String
date = new Date(date).getTime();
// date will be NaN if invalid date String is provided
return date;
};
app.post("/api/users/:_id/exercises", (req, res) => {
const _id = req.params._id;
const description = req.body.description;
const duration = Number(req.body.duration);
let date = getDateInTicks(req.body.date);
if (Number.isNaN(Number(date))) {
res.status(400);
return res.json({ error: `Input date: '${req.body.date}' is not valid` });
}
exerciseLogs
.findById(_id)
.select({ username: 1 })
.then((user) => {
if (user) {
exerciseLogs
.updateOne(
{ _id: _id },
{
$push: {
log: {
description: description,
duration: duration,
date: date,
},
},
},
{ runValidators: true }
)
.then((excLogs) => {
if (excLogs.modifiedCount) {
res.json({
_id: _id,
username: user.username,
description: description,
duration: duration,
date: new Date(date).toDateString(),
});
} else {
res.json(500);
res.json({
error: `Failed to add exercise logs to database:\n${excLogs}`,
});
}
})
.catch((err) => {
res.status(500);
res.json({
error: `Failed to add exercise logs to database:\n${err}`,
});
});
} else {
res.status(403);
res.json({ error: `No user with id: ${_id} exists` });
}
})
.catch((err) => {
res.status(500);
res.json({ error: "Failed to check user in database" });
});
});
app.get("/api/users/:_id/logs", (req, res) => {
const _id = req.params._id;
const limit = req.query.limit
? Math.trunc(Number(req.query.limit))
: Number.MAX_SAFE_INTEGER;
if (Number.isNaN(limit)) {
res.status(400);
return res.json({
error: `Query key 'limit''s value '${req.query.limit}' is not a number`,
});
}
const from = req.query.from ? getDateInTicks(req.query.from) : 0;
if (Number.isNaN(from)) {
res.status(400);
return res.json({
error: `Query key 'from''s value '${req.query.from}' is invalid for date`,
});
}
const to = req.query.to
? getDateInTicks(req.query.to)
: Number.MAX_SAFE_INTEGER;
if (Number.isNaN(to)) {
res.status(400);
return res.json({
error: `Query key 'to''s value '${req.query.to}' is invalid for date`,
});
}
exerciseLogs
.findById(_id)
.select({ "log._id": false, __v: false })
.then((excLogs) => {
if (excLogs) {
let excLogsJson = excLogs.toJSON();
excLogsJson.log = excLogsJson.log.filter(
(log) => log.date >= from && log.date <= to
);
excLogsJson.log.sort((a, b) => a.date - b.date);
excLogsJson.log = excLogsJson.log.slice(0, limit);
excLogsJson.count = excLogsJson.log.length;
excLogsJson.log = excLogsJson.log.map((log) => {
log.date = new Date(log.date).toDateString();
return log;
});
return res.json(excLogsJson);
}
res.status(400);
res.json({ error: `No user with id: '${_id}' exists` });
})
.catch((err) => {
res.status(400);
res.json({ error: `Input user id: '${_id}' is malformed` });
});
});
const listener = app.listen(PORT, "0.0.0.0", () => {
console.log(
`Your app is listening on interface ${
listener.address().address
} and port ${listener.address().port}`
);
}); });
-1451
View File
File diff suppressed because it is too large Load Diff