Delete tournaments, clean up

This commit is contained in:
Felix Albrigtsen 2022-03-30 00:10:10 +02:00
parent 12199d5f12
commit a95a2f6ea8
3 changed files with 101 additions and 85 deletions

View File

@ -19,6 +19,7 @@ app.use("/api", api);
api.use(function(req, res, next) { api.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, DELETE");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next(); next();
}); });
@ -28,9 +29,6 @@ api.use(require('express-log-url'));
// #region frontend // #region frontend
// api.get("/", (req, res) => {
// res.redirect("https://asura.feal.no/");
// });
// Serve static files from the React app // Serve static files from the React app
app.use('/', express.static(path.join(__dirname, 'clientbuild'))); app.use('/', express.static(path.join(__dirname, 'clientbuild')));
// app.use('/tournament/', express.static(path.join(__dirname, 'clientbuild', 'index.html'))); // app.use('/tournament/', express.static(path.join(__dirname, 'clientbuild', 'index.html')));
@ -87,6 +85,80 @@ 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}));
}); });
api.post("/tournament/:tournamentId/edit", (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 name = req.body.name;
let description = req.body.description;
let startDate = req.body.startDate;
let endDate = req.body.endDate;
console.log(startDate);
if (name == undefined || name == "" || description == undefined || description == "") {
res.json({"status": "error", "data": "name and description must be provided"});
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.editTournament(tournamentId, name, description, startDate, endDate)
.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}));
});
api.delete("/tournament/:tournamentId", (req, res) => {
let tournamentId = req.params.tournamentId;
if (isNaN(tournamentId)) {
res.json({"status": "error", "data": "tournamentId must be a number"});
return;
}
tournamentId = parseInt(tournamentId);
tmdb.deleteTournament(tournamentId)
.then(msg => res.json({"status": "OK", "data": msg}))
.catch(err => res.json({"status": "error", "data": err}));
});
// #endregion // #endregion
// #region match/:matchId // #region match/:matchId
@ -229,68 +301,6 @@ api.post("/tournament/create", (req, res) => {
.then(msg => res.json({"status": "OK", "data": msg})) .then(msg => res.json({"status": "OK", "data": msg}))
.catch(err => res.json({"status": "error", "data": err})); .catch(err => res.json({"status": "error", "data": err}));
}); });
api.post("/tournament/:tournamentId/edit", (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 name = req.body.name;
let description = req.body.description;
let startDate = req.body.startDate;
let endDate = req.body.endDate;
console.log(startDate);
if (name == undefined || name == "" || description == undefined || description == "") {
res.json({"status": "error", "data": "name and description must be provided"});
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.editTournament(tournamentId, name, description, startDate, endDate)
.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

View File

@ -20,7 +20,7 @@ CREATE TABLE teams (
tournamentId INTEGER NOT NULL, tournamentId INTEGER NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
FOREIGN KEY (tournamentId) REFERENCES tournaments (id) FOREIGN KEY (tournamentId) REFERENCES tournaments (id) ON DELETE CASCADE
); );
CREATE TABLE matches ( CREATE TABLE matches (
@ -32,7 +32,7 @@ CREATE TABLE matches (
winnerId INTEGER, winnerId INTEGER,
tier INTEGER, tier INTEGER,
FOREIGN KEY (tournamentId) REFERENCES tournaments (id), FOREIGN KEY (tournamentId) REFERENCES tournaments (id) ON DELETE CASCADE,
FOREIGN KEY (team1Id) REFERENCES teams (id) ON DELETE SET NULL, FOREIGN KEY (team1Id) REFERENCES teams (id) ON DELETE SET NULL,
FOREIGN KEY (team2Id) REFERENCES teams (id) ON DELETE SET NULL, FOREIGN KEY (team2Id) REFERENCES teams (id) ON DELETE SET NULL,
FOREIGN KEY (winnerId) REFERENCES teams (id) ON DELETE SET NULL FOREIGN KEY (winnerId) REFERENCES teams (id) ON DELETE SET NULL

View File

@ -12,6 +12,7 @@ module.exports = {
getMatch: getMatch, getMatch: getMatch,
setMatchWinner: setMatchWinner, setMatchWinner: setMatchWinner,
createTournament: createTournament, createTournament: createTournament,
deleteTournament: deleteTournament,
editTournament: editTournament, editTournament: editTournament,
getTeamsByTournamentId: getTeamsByTournamentId, getTeamsByTournamentId: getTeamsByTournamentId,
} }
@ -153,25 +154,30 @@ function getTournament(tournamentId) {
console.log(err); console.log(err);
reject(err); reject(err);
} else { } else {
if (tournaments.length == 0) { if (tournaments.length == 0) {
reject("No such tournament exists"); reject("No such tournament exists");
} return
}
getTeamsByTournamentId(tournamentId) getTeamsByTournamentId(tournamentId)
.catch(err => reject(err)) .catch(err => reject(err))
.then(teams => { .then(teams => {
let tournament = tournaments[0]; let tournament = tournaments[0];
//TODO: CHeckh this tournament.teamCount = teams.length;
// /home/felixalb/Documents/NTNU/semester2/sysut_server/src/server/tmdb.js:163 resolve(tournament);
// tournament.teamCount = teams.length; });
// ^ }
});
});
}
// TypeError: Cannot set properties of undefined (setting 'teamCount') function deleteTournament(tournamentId) {
// at /home/felixalb/Documents/NTNU/semester2/sysut_server/src/server/tmdb.js:163:34 return new Promise(function(resolve, reject) {
connection.query("DELETE FROM tournaments WHERE id = ?", [escapeString(tournamentId)], (err, result) => {
tournament.teamCount = teams.length; if (err) {
resolve(tournament); reject(err);
}); } else {
resolve();
} }
}); });
}); });