From 3d7ac5bbe271e3d2e60f3d22911fca86873b37a1 Mon Sep 17 00:00:00 2001 From: Zeliha Pala Date: Thu, 16 May 2024 21:17:35 +0100 Subject: [PATCH 1/4] 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; From 676ab6ecf5dfa9048505e35b509adedfb093c61a Mon Sep 17 00:00:00 2001 From: Zeliha Pala Date: Sat, 25 May 2024 16:46:24 +0100 Subject: [PATCH 2/4] updated --- server/api.js | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/server/api.js b/server/api.js index 7f4db1db3f..ec7bccbc66 100644 --- a/server/api.js +++ b/server/api.js @@ -1,5 +1,6 @@ import { Router } from "express"; import db from "./db.js"; + const router = Router(); router.get("/videos", async (_, res) => { @@ -14,17 +15,28 @@ router.get("/videos", async (_, res) => { }); router.post("/videos", async (req, res) => { - if (!req.body.title) { + const { title, src } = req.body; + + if (!title) { return res.status(422).json({ message: "Title field is required" }); } - if (!req.body.src) { + if (!src) { return res.status(422).json({ message: "src field is required" }); } - const result = await db.query( - `INSERT INTO videos (title,src) VALUES ('${req.body.title}','${req.body.src}') RETURNING id` - ); - const newVideoId = result.rows[0].id; - res.status(200).json({ success: true, data: { id: newVideoId } }); + + try { + const result = await db.query( + "INSERT INTO videos (title, src) VALUES ($1, $2) RETURNING id", + [title, src] + ); + + const newVideoId = result.rows[0].id; + res.status(200).json({ success: true, data: { id: newVideoId } }); + } catch (error) { + res + .status(500) + .json({ success: false, error: "Failed to insert video into database" }); + } }); router.delete("/videos/:id", async (req, res) => { @@ -32,23 +44,19 @@ router.delete("/videos/:id", async (req, res) => { try { const checkQuery = await db.query("SELECT * FROM videos WHERE id = $1", [ - `${videoId}`, + videoId, ]); if (checkQuery.rows.length === 0) { - return res.status(404).json({ - message: "Video not found", - }); + return res.status(404).json({ message: "Video not found" }); } - const deleteQuery = await db.query("DELETE FROM videos WHERE id = $1", [ - videoId, - ]); - + await db.query("DELETE FROM videos WHERE id = $1", [videoId]); return res.status(204).end(); } catch (error) { + console.error(error); res .status(500) - .json({ message: "Internal server error", error: this.error }); + .json({ message: "An error occurred while deleting the video" }); } }); From 2b59f1066d8bee13244ae743f7d01f1d1298c7b1 Mon Sep 17 00:00:00 2001 From: Zeliha Pala Date: Sat, 25 May 2024 18:33:56 +0100 Subject: [PATCH 3/4] post videos bug related to database (rating column configuration which is "not null") fixed, ratings added to post endpoint --- server/api.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/api.js b/server/api.js index ec7bccbc66..58e750ddd8 100644 --- a/server/api.js +++ b/server/api.js @@ -15,7 +15,7 @@ router.get("/videos", async (_, res) => { }); router.post("/videos", async (req, res) => { - const { title, src } = req.body; + const { title, src, rating } = req.body; if (!title) { return res.status(422).json({ message: "Title field is required" }); @@ -26,8 +26,8 @@ router.post("/videos", async (req, res) => { try { const result = await db.query( - "INSERT INTO videos (title, src) VALUES ($1, $2) RETURNING id", - [title, src] + "INSERT INTO videos (title, src, rating) VALUES ($1, $2, $3) RETURNING id", + [title, src, rating] ); const newVideoId = result.rows[0].id; From 574e88d16eb99efd0d8060abac784ea620dd6e2a Mon Sep 17 00:00:00 2001 From: Zeliha Pala Date: Sat, 25 May 2024 18:37:59 +0100 Subject: [PATCH 4/4] ?? operator added for rating value to post route --- server/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/api.js b/server/api.js index 58e750ddd8..c17a9b18a1 100644 --- a/server/api.js +++ b/server/api.js @@ -27,7 +27,7 @@ router.post("/videos", async (req, res) => { try { const result = await db.query( "INSERT INTO videos (title, src, rating) VALUES ($1, $2, $3) RETURNING id", - [title, src, rating] + [title, src, rating ?? 0] ); const newVideoId = result.rows[0].id;