From 86db24928d848638b2a584f8fdaf581bfa8d81ab Mon Sep 17 00:00:00 2001 From: Marcus Date: Fri, 26 Apr 2024 00:47:22 +0100 Subject: [PATCH] Completed adding post video endpoint --- db/initdb.sql | 5 ++++- server/api.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/db/initdb.sql b/db/initdb.sql index bbca627914..bb40b5058f 100644 --- a/db/initdb.sql +++ b/db/initdb.sql @@ -1,6 +1,9 @@ DROP TABLE IF EXISTS videos CASCADE; -CREATE TABLE videos (title VARCHAR, src VARCHAR); +CREATE TABLE videos ( + id SERIAL PRIMARY KEY, + title VARCHAR, + src VARCHAR); INSERT INTO videos (title,src) VALUES ('Never Gonna Give You Up','https://www.youtube.com/embed/dQw4w9WgXcQ?si=sdvqEritjOTwN2Af'); INSERT INTO videos (title,src) VALUES ('The Coding Train','https://www.youtube.com/embed/HerCR8bw_GE?si=5Xfqx9K1JMB_QCBh'); diff --git a/server/api.js b/server/api.js index 7cd7ed25c5..38b523a9f7 100644 --- a/server/api.js +++ b/server/api.js @@ -11,4 +11,15 @@ router.get("/videos", async (_, res) => { .send({ success: "false", error: "Could not connect to database" }); }); +router.post("/videos", async (req, res) => { + const newVideo = await db.query( + `INSERT INTO videos (title, src) VALUES ('${req.body.title}', '${req.body.src}')` + ); + res.send( + newVideo + ? res.send({ success: "Video added successfully" }) + : res.send({ error: "Video could not be added" }) + ); +}); + export default router;