From 91f4af6e9a3a5d770f98d4a5b75c729c18861cc1 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 1 Oct 2020 17:33:08 +0200 Subject: Some thingies --- quotes/index.js | 22 ++++++++++++++++++++-- quotes/public/index.html | 6 +++--- quotes/public/script.js | 17 ++++------------- 3 files changed, 27 insertions(+), 18 deletions(-) (limited to 'quotes') diff --git a/quotes/index.js b/quotes/index.js index e90dd80..66aeb5e 100644 --- a/quotes/index.js +++ b/quotes/index.js @@ -4,8 +4,26 @@ const app = express.Router(); app.use("/", express.static(__dirname + "/public")); -app.get("/api/list", (req, res) => { - res.send("ok\n"); +app.post("/api/add", async (req, res) => { + if (!req.body.author || !req.body.quote) return res.send("error"); + try { + await db.query("INSERT INTO quotes (user_id, author_id, quote) VALUE (?,?,?)", [ + 72, // TODO: Add actual user identification + parseInt(req.body.author), + req.body.quote, + ]); + res.redirect("/quotes"); + } catch (e) { + console.error(e); + res.json("error"); + } +}); + +app.get("/api/list", async (req, res) => { + const quotes = await db.query( + "SELECT quotes.id, name, middlename, surname, quote FROM quotes INNER JOIN users AS a ON author_id = a.id" + ); + res.json(quotes); }); module.exports = app; diff --git a/quotes/public/index.html b/quotes/public/index.html index abe85cc..0c7b327 100644 --- a/quotes/public/index.html +++ b/quotes/public/index.html @@ -14,13 +14,13 @@ Zitate -
+
Zitate - + - +
diff --git a/quotes/public/script.js b/quotes/public/script.js index 3fbc676..7a87486 100644 --- a/quotes/public/script.js +++ b/quotes/public/script.js @@ -6,7 +6,7 @@ function append(response) { response.forEach((elem) => { dropdown.insertAdjacentHTML( "beforeend", - `` ); @@ -14,15 +14,6 @@ function append(response) { } // TODO: Add api list endpoint -// fetch("/auth/api/list") -// .then((response) => response.json()) -// .then((response) => append(response)); - -const exampleJson = [ - { id: 1, name: "Lars", middlename: null, surname: "Baum" }, - { id: 2, name: "Marvin", middlename: null, surname: "Giraffe" }, - { id: 3, name: "Dominik", middlename: null, surname: "Apfel" }, - { id: 4, name: "Daniel", middlename: null, surname: "Torte" }, -]; - -append(exampleJson); +fetch("/auth/api/list") + .then((response) => response.json()) + .then((response) => append(response)); -- cgit v1.2.3