aboutsummaryrefslogtreecommitdiff
path: root/mottovote
diff options
context:
space:
mode:
Diffstat (limited to 'mottovote')
-rw-r--r--mottovote/index.js23
-rw-r--r--mottovote/public/script.js30
2 files changed, 32 insertions, 21 deletions
diff --git a/mottovote/index.js b/mottovote/index.js
index 4806e55..e06f23c 100644
--- a/mottovote/index.js
+++ b/mottovote/index.js
@@ -4,18 +4,27 @@ const { checkUser } = require("../auth");
const app = express.Router();
-app.use("/", express.static(__dirname + "/public/"));
+app.use("/", checkUser, express.static(__dirname + "/public/"));
-app.get("/api/list", async (req, res) => {
- const mottos = await db.query("SELECT id, name, description FROM motto_votes ORDER BY name, description");
+app.get("/api/list", checkUser, async (req, res) => {
+ const mottos = await db.query("SELECT id, name, description FROM mottos ORDER BY name, description");
res.json(mottos);
});
-app.put("/api/vote", async (req, res) => {
- for (const mid in req.body) {
- await db.query("UPDATE motto_votes SET votes = votes + ? WHERE id = ?", [req.body[mid], mid]);
+app.put("/api/vote", checkUser, async (req, res) => {
+ await db.query("DELETE FROM motto_votes WHERE user_id = ?", [req.session.uid]);
+ try {
+ for (const mid in req.body) {
+ await db.query(
+ "INSERT INTO motto_votes (user_id, motto_id, votes) VALUES (?, ?, ?)",
+ [req.session.uid, mid, req.body[mid]]
+ );
+ }
+ res.send("ok");
+ } catch (e) {
+ console.error(e);
+ res.send("error");
}
- res.send("ok");
});
module.exports = app; \ No newline at end of file
diff --git a/mottovote/public/script.js b/mottovote/public/script.js
index 7216af4..9fada2e 100644
--- a/mottovote/public/script.js
+++ b/mottovote/public/script.js
@@ -1,3 +1,4 @@
+const maxVotes = 3;
get();
async function get() {
@@ -7,17 +8,18 @@ async function get() {
for (const motto of mottos) {
const row = document.createElement("div");
const id = motto.id;
-
- const cb = document.createElement("input");
- cb.type = "checkbox";
- cb.id = "motto" + id;
- cb.name = id;
-
- const label = document.createElement("label");
- label.for = "motto" + id;
- label.textContent = `${motto.name} ${motto.description ? "-" : ""} ${motto.description}`
-
- row.append(cb, label);
+
+ for (let i = 0; i < maxVotes; i++) {
+ const cb = document.createElement("input");
+ cb.type = "checkbox";
+ cb.name = id;
+ row.append(cb);
+ }
+
+ const text = document.createElement("span");
+ text.textContent = `${motto.name} ${motto.description ? "-" : ""} ${motto.description}`
+
+ row.append(text);
window.vote.appendChild(row);
}
addListeners();
@@ -29,15 +31,15 @@ function addListeners() {
boxes.forEach((box) => {
box.addEventListener("change", (evt) => {
const checkedSiblings = document.querySelectorAll("input[type=checkbox]:checked");
- if (checkedSiblings.length > 3) evt.target.checked = false;
+ if (checkedSiblings.length > maxVotes) evt.target.checked = false;
});
});
window.voteButton.addEventListener("click", async () => {
const checked = document.querySelectorAll("input[type=checkbox]:checked");
- if (checked.length !== 3) return;
+ if (checked.length > maxVotes) return; // Shouldn't be necessary
const req = {};
- for (const box of checked) req[box.name] = 1; // Amount of votes
+ for (const box of checked) req[box.name] = box.name in req ? req[box.name] + 1 : 1; // Amount of votes
const resp = await fetch("api/vote", {
method: "PUT",
headers: { "Content-Type": "application/json" },