diff options
author | LarsVomMars | 2020-10-02 23:29:06 +0200 |
---|---|---|
committer | LarsVomMars | 2020-10-02 23:29:06 +0200 |
commit | c80656abf09459c8b13c9cdb33c9a8d3a8cee502 (patch) | |
tree | eb050aa708a1ab5e951e247153b8abe58401261b | |
parent | 970dfec33a2cd53ad72ba0da59c129f65d6032f6 (diff) |
Added basic voting
-rw-r--r-- | app.js | 2 | ||||
-rw-r--r-- | db.js | 11 | ||||
-rw-r--r-- | mottos.txt | 10 | ||||
-rw-r--r-- | mottovote/index.js | 21 | ||||
-rw-r--r-- | mottovote/public/index.html | 15 | ||||
-rw-r--r-- | mottovote/public/script.js | 49 | ||||
-rw-r--r-- | tables.sql | 21 |
7 files changed, 123 insertions, 6 deletions
@@ -4,6 +4,7 @@ const session = require("express-session"); const { auth, checkUser } = require("./auth"); const motto = require("./motto"); +const mottovote = require("./mottovote"); const quotes = require("./quotes"); const poll = require("./poll"); @@ -28,6 +29,7 @@ app.use(express.json()); app.use("/", express.static(__dirname + "/overview/public")); app.use("/motto", checkUser, motto); +app.use("/mottovote", mottovote); app.use("/quotes", checkUser, quotes); app.use("/poll", checkUser, poll); app.use("/auth", auth); @@ -50,6 +50,17 @@ class DB { }); }); + fs.readFile(__dirname + "/mottos.txt", "utf8", (err, data) => { + if (err) throw err; + + const mottos = data.split("\n"); + mottos.forEach(async (motto) => { + const [name, desc] = motto.split(" - "); + if (motto) + await this.query("INSERT INTO motto_votes (name, description) VALUES (?, ?)", [name, desc]); + }); + }); + const classes = data.split("--"); const userPasswords = {}; console.log("Generating users"); diff --git a/mottos.txt b/mottos.txt new file mode 100644 index 0000000..d0519da --- /dev/null +++ b/mottos.txt @@ -0,0 +1,10 @@ +ABIsexuell - Offen für alles +ABItamin - Der Stoff kam aus dem Lehrerzimmer +KohlrABI - Wir machen uns vom Acker +Suit up! It's gonnABI legendary! - +Westminster ABI - Der Adel geht +WubbalABIdubdub - { Rick and Morty } +cannABIs - Mit einer Tüte fing alles an +kABItalismus - 13 Jahre Klassenkampf +kABItän blaubär - Immer blau und trotzdem schlau +kokABIn - Wir haben die Nase voll diff --git a/mottovote/index.js b/mottovote/index.js new file mode 100644 index 0000000..4806e55 --- /dev/null +++ b/mottovote/index.js @@ -0,0 +1,21 @@ +const express = require("express"); +const db = require("../db"); +const { checkUser } = require("../auth"); +const app = express.Router(); + + +app.use("/", 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"); + 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]); + } + res.send("ok"); +}); + +module.exports = app;
\ No newline at end of file diff --git a/mottovote/public/index.html b/mottovote/public/index.html new file mode 100644 index 0000000..27ea837 --- /dev/null +++ b/mottovote/public/index.html @@ -0,0 +1,15 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Motto Vote</title> +</head> +<body> + + <div id="vote"></div> + <button id="voteButton">Vote!</button> + + <script src="script.js"></script> +</body> +</html>
\ No newline at end of file diff --git a/mottovote/public/script.js b/mottovote/public/script.js new file mode 100644 index 0000000..7216af4 --- /dev/null +++ b/mottovote/public/script.js @@ -0,0 +1,49 @@ +get(); + +async function get() { + const resp = await fetch("api/list"); + const mottos = await resp.json(); + + 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); + window.vote.appendChild(row); + } + addListeners(); +} + +function addListeners() { + // Only allow 3 votes + const boxes = document.querySelectorAll("input[type=checkbox]"); + boxes.forEach((box) => { + box.addEventListener("change", (evt) => { + const checkedSiblings = document.querySelectorAll("input[type=checkbox]:checked"); + if (checkedSiblings.length > 3) evt.target.checked = false; + }); + }); + + window.voteButton.addEventListener("click", async () => { + const checked = document.querySelectorAll("input[type=checkbox]:checked"); + if (checked.length !== 3) return; + const req = {}; + for (const box of checked) req[box.name] = 1; // Amount of votes + const resp = await fetch("api/vote", { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(req), + }); + console.log(await resp.text()); + + }); +}
\ No newline at end of file @@ -9,12 +9,13 @@ CREATE TABLE IF NOT EXISTS theme( ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- TODO: Remove dropping -DROP TABLE IF EXISTS quotes; -DROP TABLE IF EXISTS ranking_questions; -DROP TABLE IF EXISTS ranking_answers; -DROP TABLE IF EXISTS users; -DROP TABLE IF EXISTS types; -DROP TABLE IF EXISTS class; +-- DROP TABLE IF EXISTS motto_votes; +-- DROP TABLE IF EXISTS quotes; +-- DROP TABLE IF EXISTS ranking_questions; +-- DROP TABLE IF EXISTS ranking_answers; +-- DROP TABLE IF EXISTS users; +-- DROP TABLE IF EXISTS types; +-- DROP TABLE IF EXISTS class; CREATE TABLE IF NOT EXISTS types( id INTEGER PRIMARY KEY AUTO_INCREMENT, @@ -72,3 +73,11 @@ CREATE TABLE IF NOT EXISTS ranking_answers( CONSTRAINT `fk_user_answer1` FOREIGN KEY (user_id) REFERENCES users (id), CONSTRAINT `fk_user_answer2` FOREIGN KEY (answer_id) REFERENCES users (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + + +CREATE TABLE IF NOT EXISTS motto_votes( + id INTEGER PRIMARY KEY AUTO_INCREMENT, + name VARCHAR(255) NOT NULL, + description VARCHAR(255) NOT NULL DEFAULT '', + votes INTEGER DEFAULT 0 +) ENGINE=InnoDB DEFAULT CHARSET=utf8; |