mirror of
https://codeberg.org/andyscott/ExerciseTracker.git
synced 2024-11-09 06:00:48 -05:00
Added delete functionality and updated validation for post
This commit is contained in:
parent
dbb5bcc274
commit
ce1bc18721
1 changed files with 34 additions and 2 deletions
|
@ -19,6 +19,9 @@ function isDateValid(date) {
|
||||||
return format.test(date);
|
return format.test(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Route handler for retrieving all exercises
|
||||||
|
*/
|
||||||
app.get(
|
app.get(
|
||||||
"/exercises",
|
"/exercises",
|
||||||
asyncHandler(async (req, res) => {
|
asyncHandler(async (req, res) => {
|
||||||
|
@ -28,6 +31,9 @@ app.get(
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Route handler for retrieving an exercise by ID
|
||||||
|
*/
|
||||||
app.get(
|
app.get(
|
||||||
"/exercises/:_id",
|
"/exercises/:_id",
|
||||||
asyncHandler(async (req, res) => {
|
asyncHandler(async (req, res) => {
|
||||||
|
@ -40,20 +46,28 @@ app.get(
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Route handler for creating new exercises
|
||||||
|
*/
|
||||||
app.post(
|
app.post(
|
||||||
"/exercises",
|
"/exercises",
|
||||||
asyncHandler(async (req, res) => {
|
asyncHandler(async (req, res) => {
|
||||||
let isValid;
|
let isValid;
|
||||||
if (req.body.name === "" || typeof req.body.name !== "string") {
|
if (req.body.name === "" || typeof req.body.name !== "string") {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
} else if (typeof req.body.reps !== "number" || req.body.reps < 1) {
|
//console.log("name");
|
||||||
|
} else if (req.body.reps < 1) {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
} else if (typeof req.body.weight !== "number" || req.body.weight < 1) {
|
//console.log("reps");
|
||||||
|
} else if (req.body.weight < 1) {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
|
//console.log("weight");
|
||||||
} else if (req.body.unit !== "lbs" && req.body.unit !== "kgs") {
|
} else if (req.body.unit !== "lbs" && req.body.unit !== "kgs") {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
|
//console.log("unit");
|
||||||
} else if (!isDateValid(req.body.date)) {
|
} else if (!isDateValid(req.body.date)) {
|
||||||
isValid = false;
|
isValid = false;
|
||||||
|
//console.log("date");
|
||||||
} else {
|
} else {
|
||||||
isValid = true;
|
isValid = true;
|
||||||
}
|
}
|
||||||
|
@ -72,6 +86,9 @@ app.post(
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Route handler for updating an exercise
|
||||||
|
*/
|
||||||
app.put(
|
app.put(
|
||||||
"/exercises/:_id",
|
"/exercises/:_id",
|
||||||
asyncHandler(async (req, res) => {
|
asyncHandler(async (req, res) => {
|
||||||
|
@ -113,6 +130,21 @@ app.put(
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Route handler for deleting an exercise
|
||||||
|
*/
|
||||||
|
app.delete(
|
||||||
|
"/exercises/:_id",
|
||||||
|
asyncHandler(async (req, res) => {
|
||||||
|
const resultVal = await exercises.deleteExercise({ _id: req.params._id });
|
||||||
|
if (resultVal > 0) {
|
||||||
|
res.status(204).json({ Success: "Deleted" });
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ Error: "Not found" });
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server listening on port ${PORT}...`);
|
console.log(`Server listening on port ${PORT}...`);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue