Coding session - Team manager

This commit is contained in:
Felix Albrigtsen 2022-03-28 15:19:55 +02:00
parent d844103b2c
commit e3dfba5a14
2 changed files with 87 additions and 15 deletions

View File

@ -126,13 +126,14 @@ api.get("/team/:teamId", (req, res) => {
api.post("/team/:teamId/edit", (req, res) => {
let teamId = req.params.teamId;
let teamName = req.body.teamName;
let teamName = req.body.name;
console.log(req.body);
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"});
if (teamName == undefined || teamName == "") {
res.json({"status": "error", "data": "teamName must be a non-empty string"});
return
}
teamId = parseInt(teamId);
@ -157,7 +158,7 @@ api.post("/tournament/create", (req, res) => {
let name = req.body.name;
let description = req.body.description;
let teamLimit = req.body.teamLimit;
let startDate = req.body.startDate;
let startDate = req.body.startDate; //TODO: timezones, 2 hr skips
let endDate = req.body.endDate;
if (name == undefined || name == "" || description == undefined || description == "") {
res.json({"status": "error", "data": "name and description must be provided"});
@ -195,8 +196,9 @@ api.post("/tournament/create", (req, res) => {
}
tmdb.createTournament(name, description, startDate, endDate, teamLimit)
.catch(err => res.json({"status": "error", "data": err}))
.then(msg => res.json({"status": "OK", "data": msg}));
.then(msg => res.json({"status": "OK", "data": msg}))
.catch(err => res.json({"status": "error", "data": err}));
});
api.post("/tournament/:tournamentId/edit", (req, res) => {
@ -237,7 +239,28 @@ api.post("/tournament/:tournamentId/edit", (req, res) => {
}
tmdb.editTournament(tournamentId, name, description, startDate, endDate)
.catch(err => res.json({"status": "error", "data": err}))
.then(msg => res.json({"status": "OK", "data": msg}));
.then(msg => res.json({"status": "OK", "data": msg}))
.catch(err => res.json({"status": "error", "data": err}));
});
api.post("/tournament/:tournamentId/createTeam", (req, res) => {
let tournamentId = req.params.tournamentId;
if (isNaN(tournamentId)) {
res.json({"status": "error", "data": "tournamentId must be a number"});
return;
}
tournamentId = parseInt(tournamentId);
let teamName = req.body.name;
if (teamName == undefined || teamName == "") {
res.json({"status": "error", "data": "teamName must be a non-empty string"});
return;
}
tmdb.createTeam(tournamentId, teamName)
.then(msg => res.json({"status": "OK", "data": msg}))
.catch(err => res.json({"status": "error", "data": err}));
});
// #endregion

View File

@ -6,6 +6,7 @@ module.exports = {
getTournaments: getTournaments,
getTournament, getTournament,
getTeam: getTeam,
createTeam: createTeam,
editTeam: editTeam,
getMatch: getMatch,
setMatchWinner: setMatchWinner,
@ -121,18 +122,30 @@ function setMatchWinner(matchId, winnerId) {
function getTournaments() {
return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM tournaments", (err, tournaments) => {
// 1. Get the list of tournament IDs
// 2. getTournament() for each ID
// 3. Return list of tournaments
let tournamentList = [];
connection.query("SELECT id FROM tournaments", async (err, tournamentIds) => {
if (err) {
console.log(err);
reject(err);
} else {
let tournaments = await Promise.all(tournamentIds.map(async function(tournament){
return await getTournament(tournament.id);
}));
resolve(tournaments);
}
});
});
}
function getTournament(tournamentId) {
// 1. Get the tournament
// 2. Get all teams associated with the tournament
// 3. Associate the teams with the tournament
// 4. Return the tournament
return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM tournaments WHERE id = ?", [escapeString(tournamentId)], (err, tournaments) => {
if (err) {
@ -142,10 +155,14 @@ function getTournament(tournamentId) {
if (tournaments.length == 0) {
reject("No such tournament exists");
}
//TODO number of competing teams
getTeamsByTournamentId(tournamentId)
.catch(err => reject(err))
.then(teams => {
let tournament = tournaments[0];
tournament.teamCount = teams.length;
resolve(tournament);
});
}
});
});
@ -194,7 +211,7 @@ function createTournament(name, description, startDate, endDate, teamLimit) {
});
}
}
resolve("Tournament created");
resolve({message: "Tournament created", tournamentId: sets.insertId});
}
});
});
@ -248,7 +265,14 @@ function getTeam(teamId) {
});
}
function editTeam(teamId, name) {
async function editTeam(teamId, name) {
let team = await getTeam(teamId);
if (!team) {
return Promise.reject("No such team exists");
}
if (team.name == name) {
return {message: "Team name unchanged"};
}
return new Promise(function(resolve, reject) {
connection.query("UPDATE teams SET name = ? WHERE id = ?", [escapeString(name), escapeString(teamId)], (err, sets) => {
if (err) {
@ -261,6 +285,31 @@ function editTeam(teamId, name) {
});
}
async function createTeam(tournamentId, name) {
//Check that the tournament exists
let tournament = await getTournament(tournamentId);
return new Promise(function(resolve, reject) {
if (!tournament) {
reject("No such tournament exists");
return;
}
if (tournament.teamLimit <= tournament.teamCount) {
reject("Tournament is full");
return;
}
connection.query("INSERT INTO teams (tournamentId, name) VALUES (?, ?)", [escapeString(tournamentId), escapeString(name)], (err, sets) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve({message: "Team created", teamId: sets.insertId});
}
});
});
}
// #endregion
// Dangerous function, use with caution.