Imp: factored out date processing code

This commit is contained in:
Manish 2023-03-29 14:57:58 +11:00
parent 33356d1fa5
commit d84f12f4ea

View File

@ -65,12 +65,7 @@ app.get("/api/users", (req, res) => {
});
});
app.post("/api/users/:_id/exercises", (req, res) => {
const _id = req.params._id;
const description = req.body.description;
const duration = req.body.duration;
let date = req.body.date;
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
@ -78,6 +73,15 @@ app.post("/api/users/:_id/exercises", (req, res) => {
// 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 = 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` });