Impl: API: fetch user's exercise logs

This commit is contained in:
Manish 2023-03-29 13:41:49 +11:00
parent 58c2e2f94e
commit ed0c3ac118

View File

@ -130,6 +130,26 @@ app.post("/api/users/:_id/exercises", (req, res) => {
}); });
}); });
app.get("/api/users/:_id/logs", (req, res) => {
const _id = req.params._id;
exerciseLogs
.findById(_id)
.select({ "log._id": false })
.then((excLogs) => {
if (excLogs) {
const excLogsJson = excLogs.toJSON();
excLogsJson.count = excLogsJson.log.length;
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, () => { const listener = app.listen(PORT, () => {
console.log("Your app is listening on port " + listener.address().port); console.log("Your app is listening on port " + listener.address().port);
}); });