Coding session - Team manager
This commit is contained in:
parent
d844103b2c
commit
e3dfba5a14
|
@ -126,13 +126,14 @@ api.get("/team/:teamId", (req, res) => {
|
||||||
|
|
||||||
api.post("/team/:teamId/edit", (req, res) => {
|
api.post("/team/:teamId/edit", (req, res) => {
|
||||||
let teamId = req.params.teamId;
|
let teamId = req.params.teamId;
|
||||||
let teamName = req.body.teamName;
|
let teamName = req.body.name;
|
||||||
|
console.log(req.body);
|
||||||
if (isNaN(teamId)) {
|
if (isNaN(teamId)) {
|
||||||
res.json({"status": "error", "data": "teamId must be a number"});
|
res.json({"status": "error", "data": "teamId must be a number"});
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (teamName == undefined) {
|
if (teamName == undefined || teamName == "") {
|
||||||
res.json({"status": "error", "data": "teamName must be a string"});
|
res.json({"status": "error", "data": "teamName must be a non-empty string"});
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
teamId = parseInt(teamId);
|
teamId = parseInt(teamId);
|
||||||
|
@ -157,7 +158,7 @@ api.post("/tournament/create", (req, res) => {
|
||||||
let name = req.body.name;
|
let name = req.body.name;
|
||||||
let description = req.body.description;
|
let description = req.body.description;
|
||||||
let teamLimit = req.body.teamLimit;
|
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;
|
let endDate = req.body.endDate;
|
||||||
if (name == undefined || name == "" || description == undefined || description == "") {
|
if (name == undefined || name == "" || description == undefined || description == "") {
|
||||||
res.json({"status": "error", "data": "name and description must be provided"});
|
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)
|
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) => {
|
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)
|
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
|
// #endregion
|
|
@ -6,6 +6,7 @@ module.exports = {
|
||||||
getTournaments: getTournaments,
|
getTournaments: getTournaments,
|
||||||
getTournament, getTournament,
|
getTournament, getTournament,
|
||||||
getTeam: getTeam,
|
getTeam: getTeam,
|
||||||
|
createTeam: createTeam,
|
||||||
editTeam: editTeam,
|
editTeam: editTeam,
|
||||||
getMatch: getMatch,
|
getMatch: getMatch,
|
||||||
setMatchWinner: setMatchWinner,
|
setMatchWinner: setMatchWinner,
|
||||||
|
@ -121,18 +122,30 @@ function setMatchWinner(matchId, winnerId) {
|
||||||
|
|
||||||
function getTournaments() {
|
function getTournaments() {
|
||||||
return new Promise(function(resolve, reject) {
|
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) {
|
if (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
|
let tournaments = await Promise.all(tournamentIds.map(async function(tournament){
|
||||||
|
return await getTournament(tournament.id);
|
||||||
|
}));
|
||||||
resolve(tournaments);
|
resolve(tournaments);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTournament(tournamentId) {
|
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) {
|
return new Promise(function(resolve, reject) {
|
||||||
connection.query("SELECT * FROM tournaments WHERE id = ?", [escapeString(tournamentId)], (err, tournaments) => {
|
connection.query("SELECT * FROM tournaments WHERE id = ?", [escapeString(tournamentId)], (err, tournaments) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -142,10 +155,14 @@ function getTournament(tournamentId) {
|
||||||
if (tournaments.length == 0) {
|
if (tournaments.length == 0) {
|
||||||
reject("No such tournament exists");
|
reject("No such tournament exists");
|
||||||
}
|
}
|
||||||
//TODO number of competing teams
|
|
||||||
|
getTeamsByTournamentId(tournamentId)
|
||||||
let tournament = tournaments[0];
|
.catch(err => reject(err))
|
||||||
resolve(tournament);
|
.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) {
|
return new Promise(function(resolve, reject) {
|
||||||
connection.query("UPDATE teams SET name = ? WHERE id = ?", [escapeString(name), escapeString(teamId)], (err, sets) => {
|
connection.query("UPDATE teams SET name = ? WHERE id = ?", [escapeString(name), escapeString(teamId)], (err, sets) => {
|
||||||
if (err) {
|
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
|
// #endregion
|
||||||
|
|
||||||
// Dangerous function, use with caution.
|
// Dangerous function, use with caution.
|
||||||
|
|
Loading…
Reference in New Issue