diff options
author | LarsVomMars | 2020-10-03 11:31:04 +0200 |
---|---|---|
committer | LarsVomMars | 2020-10-03 11:31:04 +0200 |
commit | 7639f176abe404cad1a3c9a3b8e2b4918b86087e (patch) | |
tree | 8efca368a711180e7c8b8d801ce62a49fd45c67c | |
parent | 467eb283888328b977871b6899bede744cedfdd8 (diff) |
Store/change selected votes
-rw-r--r-- | auth/index.js | 2 | ||||
-rw-r--r-- | mottovote/index.js | 8 | ||||
-rw-r--r-- | mottovote/public/script.js | 1 |
3 files changed, 10 insertions, 1 deletions
diff --git a/auth/index.js b/auth/index.js index 0891fc5..0bcbdf5 100644 --- a/auth/index.js +++ b/auth/index.js @@ -37,6 +37,8 @@ app.post("/api/login", async (req, res) => { res.redirect("/auth"); }); +app.use("/api/logout", (req, res) => req.session.destroy() & res.redirect("/")); + app.put("/api/password", checkUser, async (req, res) => { const { pwd, newPwd } = req.body; if (!(pwd && newPwd)) return res.redirect("/auth"); diff --git a/mottovote/index.js b/mottovote/index.js index e06f23c..a485a8b 100644 --- a/mottovote/index.js +++ b/mottovote/index.js @@ -7,7 +7,13 @@ const app = express.Router(); app.use("/", checkUser, express.static(__dirname + "/public/")); app.get("/api/list", checkUser, async (req, res) => { - const mottos = await db.query("SELECT id, name, description FROM mottos ORDER BY name, description"); + const mottos = await db.query("SELECT id, name, description FROM mottos"); + const votes = await db.query("SELECT motto_id, votes FROM motto_votes WHERE user_id = ?", [req.session.uid]); + + for (const vote of votes) { + const mid = mottos.findIndex((motto) => motto.id === vote.motto_id); + if (mid) mottos[mid].votes = vote.votes; + } res.json(mottos); }); diff --git a/mottovote/public/script.js b/mottovote/public/script.js index 9fada2e..338b26b 100644 --- a/mottovote/public/script.js +++ b/mottovote/public/script.js @@ -13,6 +13,7 @@ async function get() { const cb = document.createElement("input"); cb.type = "checkbox"; cb.name = id; + cb.checked = motto.votes && motto.votes-- > 0 row.append(cb); } |