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 @@ + + +
+ + +