From 3d7ac5bbe271e3d2e60f3d22911fca86873b37a1 Mon Sep 17 00:00:00 2001 From: Zeliha Pala Date: Thu, 16 May 2024 21:17:35 +0100 Subject: [PATCH] Add DELETE /api/videos/:id route - Handle non-existent IDs with 404 - Return 204 on successful deletion - Use parameterized queries - Consistent error handling --- server/api.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/server/api.js b/server/api.js index 1bbe52ccef..7f4db1db3f 100644 --- a/server/api.js +++ b/server/api.js @@ -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", [ + videoId, + ]); + + return res.status(204).end(); + } catch (error) { + res + .status(500) + .json({ message: "Internal server error", error: this.error }); + } +}); + export default router;