aboutsummaryrefslogtreecommitdiff
path: root/quotes/index.js
blob: 66aeb5ef85e41b1be604efc84b35ab2e25b9b083 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const express = require("express");
const db = require("../db");
const app = express.Router();

app.use("/", express.static(__dirname + "/public"));

app.post("/api/add", async (req, res) => {
    if (!req.body.author || !req.body.quote) return res.send("error");
    try {
        await db.query("INSERT INTO quotes (user_id, author_id, quote) VALUE (?,?,?)", [
            72, // TODO: Add actual user identification
            parseInt(req.body.author),
            req.body.quote,
        ]);
        res.redirect("/quotes");
    } catch (e) {
        console.error(e);
        res.json("error");
    }
});

app.get("/api/list", async (req, res) => {
    const quotes = await db.query(
        "SELECT quotes.id, name, middlename, surname, quote FROM quotes INNER JOIN users AS a ON author_id = a.id"
    );
    res.json(quotes);
});

module.exports = app;