From c80656abf09459c8b13c9cdb33c9a8d3a8cee502 Mon Sep 17 00:00:00 2001 From: LarsVomMars Date: Fri, 2 Oct 2020 23:29:06 +0200 Subject: Added basic voting --- app.js | 2 ++ db.js | 11 ++++++++++ mottos.txt | 10 +++++++++ mottovote/index.js | 21 +++++++++++++++++++ mottovote/public/index.html | 15 ++++++++++++++ mottovote/public/script.js | 49 +++++++++++++++++++++++++++++++++++++++++++++ tables.sql | 21 +++++++++++++------ 7 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 mottos.txt create mode 100644 mottovote/index.js create mode 100644 mottovote/public/index.html create mode 100644 mottovote/public/script.js diff --git a/app.js b/app.js index 23e2715..8200713 100644 --- a/app.js +++ b/app.js @@ -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); diff --git a/db.js b/db.js index 58673bc..26e3d97 100644 --- a/db.js +++ b/db.js @@ -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 @@ + + + + + + Motto Vote + + + +
+ + + + + \ 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 diff --git a/tables.sql b/tables.sql index 6161b03..ea86339 100644 --- a/tables.sql +++ b/tables.sql @@ -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; -- cgit v1.2.3