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 @@