aboutsummaryrefslogtreecommitdiff
path: root/quotes
diff options
context:
space:
mode:
Diffstat (limited to 'quotes')
-rw-r--r--quotes/index.js13
-rw-r--r--quotes/public/script.js12
2 files changed, 22 insertions, 3 deletions
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));