Added team methods

This commit is contained in:
Felix Albrigtsen 2022-03-27 22:21:05 +02:00
parent 4e3e4f58e4
commit d844103b2c
2 changed files with 136 additions and 56 deletions

View File

@ -39,6 +39,7 @@ api.get("/tournament/getTournaments", (req, res) => {
.catch(err => res.json({"status": "error", "data": err})); .catch(err => res.json({"status": "error", "data": err}));
}); });
// #region tournament/:tournamentId
api.get("/tournament/:tournamentId", (req, res) => { api.get("/tournament/:tournamentId", (req, res) => {
let tournamentId = req.params.tournamentId; let tournamentId = req.params.tournamentId;
if (isNaN(tournamentId)) { if (isNaN(tournamentId)) {
@ -48,11 +49,8 @@ api.get("/tournament/:tournamentId", (req, res) => {
tmdb.getTournament(parseInt(tournamentId)) tmdb.getTournament(parseInt(tournamentId))
.catch(err => res.json({"status": "error", "data": err})) .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(tournament => res.json({"status": "OK", "data": tournament}));
// .then(console.log("lol"))
}); });
api.get("/tournament/:tournamentId/getMatches", (req, res) => { api.get("/tournament/:tournamentId/getMatches", (req, res) => {
let tournamentId = req.params.tournamentId; let tournamentId = req.params.tournamentId;
if (isNaN(tournamentId)) { if (isNaN(tournamentId)) {
@ -76,8 +74,12 @@ api.get("/tournament/:tournamentId/getTeams", (req, res) => {
.then(teams => res.send({"status": "OK", "data": teams})) .then(teams => res.send({"status": "OK", "data": teams}))
.catch(err => res.send({"status": "error", "data": err})); .catch(err => res.send({"status": "error", "data": err}));
}); });
// #endregion
api.get("/match/:matchId/getMatch", (req, res) => { // #region match/:matchId
api.get("/match/:matchId", (req, res) => {
let matchId = req.params.matchId; let matchId = req.params.matchId;
if (isNaN(matchId)) { if (isNaN(matchId)) {
res.json({"status": "error", "data": "matchId must be a number"}); res.json({"status": "error", "data": "matchId must be a number"});
@ -107,6 +109,40 @@ api.post("/match/:matchId/setWinner", (req, res) => {
.then(match => res.send({"status": "OK", "data": match})) .then(match => res.send({"status": "OK", "data": match}))
.catch(err => res.send({"status": "error", "data": err})); .catch(err => res.send({"status": "error", "data": err}));
}); });
// #endregion
// #region team/:teamId
api.get("/team/:teamId", (req, res) => {
let teamId = req.params.teamId;
if (isNaN(teamId)) {
res.json({"status": "error", "data": "teamId must be a number"});
return
}
teamId = parseInt(teamId);
tmdb.getTeam(teamId)
.then(match => res.send({"status": "OK", "data": match}))
.catch(err => res.send({"status": "error", "data": err}));
});
api.post("/team/:teamId/edit", (req, res) => {
let teamId = req.params.teamId;
let teamName = req.body.teamName;
if (isNaN(teamId)) {
res.json({"status": "error", "data": "teamId must be a number"});
return
}
if (teamName == undefined) {
res.json({"status": "error", "data": "teamName must be a string"});
return
}
teamId = parseInt(teamId);
tmdb.editTeam(teamId, teamName)
.then(match => res.send({"status": "OK", "data": match}))
.catch(err => res.send({"status": "error", "data": err}));
});
// #endregion
//Takes JSON body //Takes JSON body
api.post("/tournament/create", (req, res) => { api.post("/tournament/create", (req, res) => {

View File

@ -5,6 +5,8 @@ module.exports = {
getMatchesByTournamentId: getMatchesByTournamentId, getMatchesByTournamentId: getMatchesByTournamentId,
getTournaments: getTournaments, getTournaments: getTournaments,
getTournament, getTournament, getTournament, getTournament,
getTeam: getTeam,
editTeam: editTeam,
getMatch: getMatch, getMatch: getMatch,
setMatchWinner: setMatchWinner, setMatchWinner: setMatchWinner,
createTournament: createTournament, createTournament: createTournament,
@ -21,55 +23,16 @@ let connection = mysql.createConnection({
database: process.env.DB_DATABASE database: process.env.DB_DATABASE
}); });
function getMatchesByTournamentId(tournamentId) { function escapeString(str) {
return new Promise(function(resolve, reject) { // return mysql.escape(str);
connection.query("SELECT * FROM matches WHERE tournamentId = ?", [mysql.escape(tournamentId)], (err, matches) => { return str;
if (err) {
console.log(err);
reject(err);
} else {
resolve(matches);
}
});
});
}
function getTournaments() {
return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM tournaments", (err, tournaments) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(tournaments);
}
});
});
}
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);
}
});
});
} }
// #region match
// Returns the match of the exact given id. // Returns the match of the exact given id.
function getMatch(matchId) { function getMatch(matchId) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM matches WHERE id = ?", [mysql.escape(matchId)], (err, matches) => { connection.query("SELECT * FROM matches WHERE id = ?", [escapeString(matchId)], (err, matches) => {
if (err) { if (err) {
reject(err); reject(err);
} else { } else {
@ -88,11 +51,11 @@ function getMatch(matchId) {
async function unsetContestant(matchId, teamId) { async function unsetContestant(matchId, teamId) {
let match = await getMatch(matchId); let match = await getMatch(matchId);
if (match.team1Id == teamId) { if (match.team1Id == teamId) {
connection.query("UPDATE matches SET team1Id = NULL WHERE id = ?", [mysql.escape(matchId)], (err, result) => { connection.query("UPDATE matches SET team1Id = NULL WHERE id = ?", [escapeString(matchId)], (err, result) => {
if (err) { console.log(err); } if (err) { console.log(err); }
}); });
} else if (match.team2Id == teamId) { } else if (match.team2Id == teamId) {
connection.query("UPDATE matches SET team2Id = NULL WHERE id = ?", [mysql.escape(matchId)], (err, result) => { connection.query("UPDATE matches SET team2Id = NULL WHERE id = ?", [escapeString(matchId)], (err, result) => {
if (err) { console.log(err); } if (err) { console.log(err); }
}); });
} else { } else {
@ -122,12 +85,12 @@ function setMatchWinner(matchId, winnerId) {
.then(parentMatch => { .then(parentMatch => {
if (parentMatch.team1Id == null) { if (parentMatch.team1Id == null) {
connection.query("UPDATE matches SET team1Id = ? WHERE id = ?", connection.query("UPDATE matches SET team1Id = ? WHERE id = ?",
[mysql.escape(winnerId), mysql.escape(parentMatch.id)], (err, sets) => { [escapeString(winnerId), escapeString(parentMatch.id)], (err, sets) => {
if (err) { reject(err); } if (err) { reject(err); }
}); });
} else if (parentMatch.team2Id == null) { } else if (parentMatch.team2Id == null) {
connection.query("UPDATE matches SET team2Id = ? WHERE id = ?", connection.query("UPDATE matches SET team2Id = ? WHERE id = ?",
[mysql.escape(winnerId), mysql.escape(parentMatch.id)], (err, sets) => { [escapeString(winnerId), escapeString(parentMatch.id)], (err, sets) => {
if (err) { reject(err); } if (err) { reject(err); }
}); });
} else { } else {
@ -138,7 +101,7 @@ function setMatchWinner(matchId, winnerId) {
// Lastly, if all checks passed, actually set the winnerId property // Lastly, if all checks passed, actually set the winnerId property
connection.query("UPDATE matches SET winnerId = ? WHERE id = ?", connection.query("UPDATE matches SET winnerId = ? WHERE id = ?",
[mysql.escape(winnerId), mysql.escape(matchId)], (err, sets) => { [escapeString(winnerId), escapeString(matchId)], (err, sets) => {
if (err) { if (err) {
// If this update fails, we need to undo the parent match update // If this update fails, we need to undo the parent match update
unsetContestant(parentMatchId, winnerId); unsetContestant(parentMatchId, winnerId);
@ -152,13 +115,61 @@ function setMatchWinner(matchId, winnerId) {
}); });
}); });
} }
// #endregion
// #region tournament
function getTournaments() {
return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM tournaments", (err, tournaments) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(tournaments);
}
});
});
}
function getTournament(tournamentId) {
return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM tournaments WHERE id = ?", [escapeString(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);
}
});
});
}
function getMatchesByTournamentId(tournamentId) {
return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM matches WHERE tournamentId = ?", [escapeString(tournamentId)], (err, matches) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(matches);
}
});
});
}
function createTournament(name, description, startDate, endDate, teamLimit) { function createTournament(name, description, startDate, endDate, teamLimit) {
startDate = startDate.toISOString().slice(0, 19).replace('T', ' '); startDate = startDate.toISOString().slice(0, 19).replace('T', ' ');
endDate = endDate.toISOString().slice(0, 19).replace('T', ' '); endDate = endDate.toISOString().slice(0, 19).replace('T', ' ');
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
connection.query("INSERT INTO tournaments (name, description, startTime, endTime, teamLimit) VALUES (?, ?, ?, ?, ?)", connection.query("INSERT INTO tournaments (name, description, startTime, endTime, teamLimit) VALUES (?, ?, ?, ?, ?)",
[mysql.escape(name), mysql.escape(description), startDate, endDate, teamLimit], (err, sets) => { [escapeString(name), escapeString(description), startDate, endDate, teamLimit], (err, sets) => {
if (err) { if (err) {
console.log(err); console.log(err);
reject(err); reject(err);
@ -194,7 +205,7 @@ function editTournament(tournamentId, name, description, startDate, endDate) {
endDate = endDate.toISOString().slice(0, 19).replace('T', ' '); endDate = endDate.toISOString().slice(0, 19).replace('T', ' ');
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
connection.query("UPDATE tournaments SET name = ?, description = ?, startTime = ?, endTime = ? WHERE id = ?", connection.query("UPDATE tournaments SET name = ?, description = ?, startTime = ?, endTime = ? WHERE id = ?",
[mysql.escape(name), mysql.escape(description), startDate, endDate, mysql.escape(tournamentId)], (err, sets) => { [escapeString(name), escapeString(description), startDate, endDate, escapeString(tournamentId)], (err, sets) => {
if (err) { if (err) {
console.log(err); console.log(err);
reject(err); reject(err);
@ -207,7 +218,7 @@ function editTournament(tournamentId, name, description, startDate, endDate) {
function getTeamsByTournamentId(tournamentId) { function getTeamsByTournamentId(tournamentId) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM teams WHERE tournamentId = ?", [mysql.escape(tournamentId)], (err, teams) => { connection.query("SELECT * FROM teams WHERE tournamentId = ?", [escapeString(tournamentId)], (err, teams) => {
if (err) { if (err) {
console.log(err); console.log(err);
reject(err); reject(err);
@ -217,7 +228,40 @@ function getTeamsByTournamentId(tournamentId) {
}); });
}); });
} }
// #endregion
// #region team
function getTeam(teamId) {
return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM teams WHERE id = ?", [escapeString(teamId)], (err, teams) => {
if (err) {
console.log(err);
reject(err);
} else {
if (teams.length == 0) {
reject("No such team exists");
}
resolve(teams[0]);
}
});
});
}
function editTeam(teamId, name) {
return new Promise(function(resolve, reject) {
connection.query("UPDATE teams SET name = ? WHERE id = ?", [escapeString(name), escapeString(teamId)], (err, sets) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve("Team updated");
}
});
});
}
// #endregion
// Dangerous function, use with caution. // Dangerous function, use with caution.
// Used to initialize and manage the database by management tools, not by the main application. // Used to initialize and manage the database by management tools, not by the main application.