diff options
author | Marvin Borner | 2020-10-03 12:31:15 +0200 |
---|---|---|
committer | Marvin Borner | 2020-10-03 12:31:27 +0200 |
commit | 7285ce6a534baed7c94c28d6ccaadc959156e94d (patch) | |
tree | fcc3d94a7f270db040a69a0d3627260b87098bde | |
parent | 9e18568e9d51a6e07d71dd0ec987e8acf737fac2 (diff) |
Added quote deletion
-rw-r--r-- | auth/index.js | 2 | ||||
-rw-r--r-- | quotes/index.js | 13 | ||||
-rw-r--r-- | quotes/public/script.js | 12 |
3 files changed, 23 insertions, 4 deletions
diff --git a/auth/index.js b/auth/index.js index 7aeaf77..8e61e51 100644 --- a/auth/index.js +++ b/auth/index.js @@ -59,7 +59,7 @@ app.put("/api/password", checkUser, async (req, res) => { app.get("/api/list", checkUser, async (req, res) => { let users; if (req.query.class === "all") { - users = await db.query("SELECT id, name, middlename, surname FROM users"); + users = await db.query("SELECT id, name, middlename, surname FROM users ORDER BY name"); } else { users = await db.query( "SELECT id, name, middlename, surname FROM users WHERE class_id = (SELECT class_id FROM users WHERE id = ?) ORDER BY name", diff --git a/quotes/index.js b/quotes/index.js index c0014c6..9683009 100644 --- a/quotes/index.js +++ b/quotes/index.js @@ -22,9 +22,20 @@ 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 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", ); res.json(quotes); }); +app.delete("/api/delete/:id", checkUser, async (req, res) => { + if (!req.params.id) return res.send("error"); + try { + await db.query("DELETE FROM quotes WHERE id = ? AND user_id = ?", [req.params.id, req.session.uid]); + res.send("ok"); + } catch (e) { + console.error(e); + res.send("error"); + } +}); + module.exports = app; diff --git a/quotes/public/script.js b/quotes/public/script.js index da1c38f..20587fe 100644 --- a/quotes/public/script.js +++ b/quotes/public/script.js @@ -21,8 +21,16 @@ function appendQuote(response) { "beforeend", `<li>${elem["name"]} ${elem["middlename"] ? elem["middlename"] : " "}${elem["surname"]}: ${ elem["quote"] - }</li>`, + } <span data-id="${elem["id"]}">[Löschen]</span></li>`, ); + + document.querySelector(`li span[data-id="${elem["id"]}"]`).addEventListener("click", (event) => { + fetch("api/delete/" + event.target.getAttribute("data-id"), { method: "DELETE" }) + .then((response) => response.text()) + .then((response) => { + if (response == "ok") event.target.parentNode.remove(); + }); + }); }); } @@ -30,7 +38,7 @@ fetch("/auth/api/list?class=all") .then((response) => response.json()) .then((response) => appendOption(response)); -fetch("/quotes/api/list") +fetch("api/list") .then((response) => response.json()) .then((response) => appendQuote(response)); |