Imp: check if input date is valid in submitted exercise log
This commit is contained in:
parent
f223697b88
commit
58c2e2f94e
10
index.js
10
index.js
@ -65,11 +65,21 @@ app.get("/api/users", (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const isValidDate = (dateString) => {
|
||||||
|
if (!Number.isNaN(Number(dateString))) return true;
|
||||||
|
const date = new Date(dateString);
|
||||||
|
return !Number.isNaN(date.getTime());
|
||||||
|
};
|
||||||
|
|
||||||
app.post("/api/users/:_id/exercises", (req, res) => {
|
app.post("/api/users/:_id/exercises", (req, res) => {
|
||||||
const _id = req.params._id;
|
const _id = req.params._id;
|
||||||
const description = req.body.description;
|
const description = req.body.description;
|
||||||
const duration = req.body.duration;
|
const duration = req.body.duration;
|
||||||
const date = req.body.date ? req.body.date : new Date().toDateString();
|
const date = req.body.date ? req.body.date : new Date().toDateString();
|
||||||
|
if (!isValidDate(date)) {
|
||||||
|
res.status(400);
|
||||||
|
return res.json({ error: `Input date: '${date}' is not valid` });
|
||||||
|
}
|
||||||
exerciseLogs
|
exerciseLogs
|
||||||
.exists({ _id: _id })
|
.exists({ _id: _id })
|
||||||
.then((userExists) => {
|
.then((userExists) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user