asura-tmdb/src/server/index.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-03-15 14:46:24 +01:00
const path = require("path");
const express = require("express");
2022-03-16 14:33:07 +01:00
const mysql = require("mysql");
require("dotenv").config();
let connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
});
2022-03-15 14:46:24 +01:00
const Match = require("./match.js");
2022-03-16 14:33:07 +01:00
const app = express();
const port = 3000;
app.engine('html', require('ejs').renderFile);
2022-03-15 14:46:24 +01:00
app.listen(port, () => {
console.log(`Listening on port ${port}`)
})
2022-03-16 14:33:07 +01:00
2022-03-15 14:46:24 +01:00
app.get("/", (req, res) => {
2022-03-16 14:33:07 +01:00
res.sendFile(path.join(__dirname, "public", "landing.html"));
});
2022-03-16 15:40:33 +01:00
app.get("/tournament/:tournamentId/getMatches", (req, res) => {
let tournamentId = req.params.tournamentId;
if (isNaN(tournamentId)) {
res.json({"status": "error", "data": "tournamentId must be a number"});
return
}
tournamentId = parseInt(tournamentId);
2022-03-16 15:40:33 +01:00
getMatchesByTournamentId(tournamentId)
.then(matches => res.send({"status": "OK", "data": matches}))
.catch(err => res.send({"status": "error", "data": err}));
2022-03-15 14:46:24 +01:00
});
2022-03-16 15:40:33 +01:00
// app.get("/getMatches", (req, res) => {
// connection.query("SELECT * FROM matches", (err, matches) => {
// if (err) {
// console.log(err);
// } else {
// res.send(matches);
// }
// });
// });
2022-03-15 14:46:24 +01:00
2022-03-16 15:40:33 +01:00
// app.get("/tournament/:tournamentId", (req, res) => {
// res.render(path.join(__dirname, "public", "tournament.html"), {"tournament":tournaments[req.params.tournamentId]});
// });
2022-03-16 14:33:07 +01:00
2022-03-16 15:40:33 +01:00
function getMatchesByTournamentId(tournamentId) {
return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM matches WHERE tournament_id = ?", [mysql.escape(tournamentId)], (err, matches) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(matches);
}
});
});
}