From 069e4d5a3bb03d4fcba46ae40657eb55b8581c53 Mon Sep 17 00:00:00 2001 From: LarsVomMars Date: Sat, 3 Oct 2020 12:59:22 +0200 Subject: Delte owner --- quotes/index.js | 3 ++- quotes/public/script.js | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/quotes/index.js b/quotes/index.js index 9683009..5aa0646 100644 --- a/quotes/index.js +++ b/quotes/index.js @@ -22,7 +22,8 @@ app.post("/api/add", checkUser, async (req, res) => { app.get("/api/list", checkUser, async (req, res) => { const quotes = await db.query( - "SELECT q.id, a.name, a.middlename, a.surname, q.quote, c.name AS class FROM quotes AS q INNER JOIN users AS a ON author_id = a.id INNER JOIN class AS c ON a.class_id = c.id ORDER BY a.name", + "SELECT q.id, a.name, a.middlename, a.surname, q.quote, c.name AS class, (q.user_id = ?) AS owner FROM quotes AS q INNER JOIN users AS a ON author_id = a.id INNER JOIN class AS c ON a.class_id = c.id ORDER BY a.name", + [req.session.uid], ); res.json(quotes); }); diff --git a/quotes/public/script.js b/quotes/public/script.js index 20587fe..8132019 100644 --- a/quotes/public/script.js +++ b/quotes/public/script.js @@ -21,10 +21,11 @@ function appendQuote(response) { "beforeend", `
  • ${elem["name"]} ${elem["middlename"] ? elem["middlename"] : " "}${elem["surname"]}: ${ elem["quote"] - } [Löschen]
  • `, + }${elem["owner"] ? '[Löschen]' : ""}`, ); - document.querySelector(`li span[data-id="${elem["id"]}"]`).addEventListener("click", (event) => { + const span = document.querySelector(`li span[data-id="${elem["id"]}"]`); + if (span) span.addEventListener("click", (event) => { fetch("api/delete/" + event.target.getAttribute("data-id"), { method: "DELETE" }) .then((response) => response.text()) .then((response) => { -- cgit v1.2.3