Skip to content

Commit

Permalink
Add DELETE /api/videos/:id route
Browse files Browse the repository at this point in the history
- Handle non-existent IDs with 404
- Return 204 on successful deletion
- Use parameterized queries
- Consistent error handling
  • Loading branch information
zelihapala committed May 16, 2024
1 parent bd0d054 commit 3d7ac5b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,29 @@ router.post("/videos", async (req, res) => {
res.status(200).json({ success: true, data: { id: newVideoId } });
});

router.delete("/videos/:id", async (req, res) => {
const videoId = req.params.id;

try {
const checkQuery = await db.query("SELECT * FROM videos WHERE id = $1", [
`${videoId}`,
]);
if (checkQuery.rows.length === 0) {
return res.status(404).json({
message: "Video not found",
});
}

const deleteQuery = await db.query("DELETE FROM videos WHERE id = $1", [

Check failure on line 43 in server/api.js

View workflow job for this annotation

GitHub Actions / run-linter

'deleteQuery' is assigned a value but never used
videoId,
]);

return res.status(204).end();
} catch (error) {
res
.status(500)
.json({ message: "Internal server error", error: this.error });
}
});

export default router;

0 comments on commit 3d7ac5b

Please sign in to comment.