diff --git a/src/server/index.js b/src/server/index.js index 63df944..5c3c8a8 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -8,7 +8,6 @@ let tmdb = require("./tmdb.js"); // #region Express setup const app = express(); const port = 3000; -//app.engine('html', require('ejs').renderFile); app.listen(port, () => { console.log(`Listening on port ${port}`) }) @@ -16,6 +15,13 @@ app.use(express.json()); app.use(express.urlencoded({ extended: true })); let api = express.Router(); app.use("/api", api); + +api.use(function(req, res, next) { + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + next(); +}); + // #endregion // #region frontend @@ -28,8 +34,21 @@ api.get("/", (req, res) => { // #region API api.get("/tournament/getTournaments", (req, res) => { tmdb.getTournaments() - .then(tournaments => {res.json({"status": "OK", "data": tournaments}); }) - .catch(err => {res.json({"status": "error", "data": err}); }); + .then(tournaments => res.json({"status": "OK", "data": tournaments})) + .catch(err => res.json({"status": "error", "data": err})); +}); + +api.get("/tournament/:tournamentId", (req, res) => { + let tournamentId = req.params.tournamentId; + if (isNaN(tournamentId)) { + res.json({"status": "error", "data": "Invalid tournament id"}); + return; + } + tmdb.getTournament(parseInt(tournamentId)) + .catch(err => res.json({"status": "error", "data": err})) + .then(tournament => res.json({"status": "OK", "data": tournament})); + // .then(tournament => res.json({"status": "OK", "data": tournament})); + // .then(console.log("lol")) }); api.get("/tournament/:tournamentId/getMatches", (req, res) => { @@ -40,8 +59,8 @@ api.get("/tournament/:tournamentId/getMatches", (req, res) => { } tournamentId = parseInt(tournamentId); tmdb.getMatchesByTournamentId(tournamentId) - .then(matches => res.send({"status": "OK", "data": matches})) - .catch(err => res.send({"status": "error", "data": err})); + .then(matches => res.send({"status": "OK", "data": matches})) + .catch(err => res.send({"status": "error", "data": err})); }); api.get("/match/:matchId/getMatch", (req, res) => { @@ -52,11 +71,11 @@ api.get("/match/:matchId/getMatch", (req, res) => { } matchId = parseInt(matchId); tmdb.getMatch(matchId) - .then(match => res.send({"status": "OK", "data": match})) - .catch(err => res.send({"status": "error", "data": err})); + .then(match => res.send({"status": "OK", "data": match})) + .catch(err => res.send({"status": "error", "data": err})); }); -// JSON body: {"winner": "teamId"} +// JSON body: {"winner": teamId} api.post("/match/:matchId/setWinner", (req, res) => { let matchId = req.params.matchId; let winnerId = req.body.winnerId; @@ -68,11 +87,66 @@ api.post("/match/:matchId/setWinner", (req, res) => { res.json({"status": "error", "data": "winnerId must be a number"}); return } - + matchId = parseInt(matchId); winnerId = parseInt(winnerId); tmdb.setMatchWinner(matchId, winnerId) - .then(match => res.send({"status": "OK", "data": match})) - .catch(err => res.send({"status": "error", "data": err})); + .then(match => res.send({"status": "OK", "data": match})) + .catch(err => res.send({"status": "error", "data": err})); +}); + +//Takes JSON body +api.post("/tournament/create", (req, res) => { + //Check that req body is valid + if (req.body.name == undefined || req.body.name == "") { + res.json({"status": "error", "data": "No data supplied"}); + return + } + //Check that req is json + // if (req.get("Content-Type") != "application/json") { + console.log(req.get("Content-Type")); + let name = req.body.name; + let description = req.body.description; + let teamLimit = req.body.teamLimit; + let startDate = req.body.startDate; + let endDate = req.body.endDate; + if (name == undefined || name == "" || description == undefined || description == "") { + res.json({"status": "error", "data": "name and description must be provided"}); + return + } + if (teamLimit == undefined ) { + res.json({"status": "error", "data": "teamLimit must be provided"}); + return + } + try { + teamLimit = parseInt(teamLimit); + } catch (err) { + res.json({"status": "error", "data": "teamLimit must be a number"}); + return + } + if (startDate == undefined || endDate == undefined) { + res.json({"status": "error", "data": "startDate and endDate must be defined"}); + return + } + try { + startDate = new Date(startDate); + endDate = new Date(endDate); + } catch (err) { + res.json({"status": "error", "data": "startDate and endDate must be valid dates"}); + return + } + let today = new Date(); + if (startDate < today) { + res.json({"status": "error", "data": "startDate cannot be in the past"}); + return + } + if (startDate > endDate) { + res.json({"status": "error", "data": "startDate cannot be after endDate"}); + return + } + + tmdb.createTournament(name, description, startDate, endDate, teamLimit) + .catch(err => res.json({"status": "error", "data": err})) + .then(msg => res.json({"status": "OK", "data": msg})); }); // #endregion \ No newline at end of file diff --git a/src/server/management/initDB.sql b/src/server/management/initDB.sql index 53fd002..392dc4d 100644 --- a/src/server/management/initDB.sql +++ b/src/server/management/initDB.sql @@ -10,6 +10,7 @@ CREATE TABLE tournaments ( id INTEGER PRIMARY KEY AUTO_INCREMENT, name TEXT NOT NULL, description TEXT, + teamLimit INTEGER NOT NULL, startTime DATETIME NOT NULL, endTime DATETIME NOT NULL ); @@ -44,8 +45,8 @@ CREATE TABLE players ( ); -- Example data (Two tournaments, 4 teams, single elimination) -INSERT INTO tournaments (name, description, startTime, endTime) VALUES ('Tournament 1', 'First tournament, single elimination', '2022-04-01 16:00:00', '2022-04-01 20:00:00'); -INSERT INTO tournaments (name, description, startTime, endTime) VALUES ('Tournament 2', 'Second tournament, four teams', '2022-04-03 17:30:00', '2022-04-02 21:30:00'); +INSERT INTO tournaments (name, description, startTime, endTime, teamLimit) VALUES ('Tournament 1', 'First tournament, single elimination', '2022-04-01 16:00:00', '2022-04-01 20:00:00', 4); +INSERT INTO tournaments (name, description, startTime, endTime, teamLimit) VALUES ('Tournament 2', 'Second tournament, four teams', '2022-04-03 17:30:00', '2022-04-04 21:30:00', 4); INSERT INTO teams (tournamentId, name) VALUES (1, 'Fnatic'); -- 1 INSERT INTO teams (tournamentId, name) VALUES (1, 'Cloud 9'); -- 2 diff --git a/src/server/package-lock.json b/src/server/package-lock.json index 1081da5..30e2427 100644 --- a/src/server/package-lock.json +++ b/src/server/package-lock.json @@ -1217,9 +1217,9 @@ } }, "node_modules/minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz", + "integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==", "dev": true }, "node_modules/moment": { @@ -3073,9 +3073,9 @@ } }, "minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz", + "integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==", "dev": true }, "moment": { diff --git a/src/server/tmdb.js b/src/server/tmdb.js index a19c977..ad42b56 100644 --- a/src/server/tmdb.js +++ b/src/server/tmdb.js @@ -4,11 +4,12 @@ module.exports = { getMatchesByTournamentId: getMatchesByTournamentId, getTournaments: getTournaments, + getTournament, getTournament, getMatch: getMatch, setMatchWinner: setMatchWinner, + createTournament: createTournament, } -const { query } = require("express"); const mysql = require("mysql"); let connection = mysql.createConnection({ @@ -44,6 +45,25 @@ function getTournaments() { }); } +function getTournament(tournamentId) { + return new Promise(function(resolve, reject) { + connection.query("SELECT * FROM tournaments WHERE id = ?", [mysql.escape(tournamentId)], (err, tournaments) => { + if (err) { + console.log(err); + reject(err); + } else { + if (tournaments.length == 0) { + reject("No such tournament exists"); + } + //TODO number of competing teams + + let tournament = tournaments[0]; + resolve(tournament); + } + }); + }); +} + // Returns the match of the exact given id. function getMatch(matchId) { return new Promise(function(resolve, reject) { @@ -54,7 +74,9 @@ function getMatch(matchId) { if (matches.length == 0) { reject("No such match exists"); } - resolve(matches[0]); + + let match = matches[0]; + resolve(match); } }); }); @@ -126,6 +148,22 @@ function setMatchWinner(matchId, winnerId) { }); } +function createTournament(name, description, startDate, endDate, teamLimit) { + startDate = startDate.toISOString().slice(0, 19).replace('T', ' '); + endDate = endDate.toISOString().slice(0, 19).replace('T', ' '); + return new Promise(function(resolve, reject) { + connection.query("INSERT INTO tournaments (name, description, startTime, endTime, teamLimit) VALUES (?, ?, ?, ?, ?)", + [mysql.escape(name), mysql.escape(description), startDate, endDate, teamLimit], (err, sets) => { + if (err) { + console.log(err); + reject(err); + } else { + resolve("Tournament created"); + } + }); + }); +} + // Dangerous function, use with caution. // Used to initialize and manage the database by management tools, not by the main application.