asura-tmdb/src/server/index.js

83 lines
2.1 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;
getMatchesByTournamentId(tournamentId)
.then((result) => res.send({"status": "OK", "data": result}))
.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 14:33:07 +01:00
2022-03-16 15:40:33 +01:00
// let tournaments = {
// "1": {
// "name": "Tournament 1",
// "description": "This is the first tournament",
// "matches":[
// {"id": "2",
// "player1": "Player 1",
// "player2": "Player 2",
// "winner": "Player 1",
// }
// ]
// },
// "2": {
// "name": "Tournament 2",
// "description": "This is the second tournament",
// "matches":[
// {"id": "2",
// "player1": "Player 1",
// "player2": "Player 2",
// "winner": "Player 1",
// }]
// }
// };
// 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);
}
});
});
}