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) {
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");
next();
});
@ -28,9 +29,6 @@ api.use(require('express-log-url'));
// #region frontend
// api.get("/", (req, res) => {
// res.redirect("https://asura.feal.no/");
// });
// Serve static files from the React app
app.use('/', express.static(path.join(__dirname, 'clientbuild')));
// 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}))
.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
// #region match/:matchId
@ -229,68 +301,6 @@ api.post("/tournament/create", (req, res) => {
.then(msg => res.json({"status": "OK", "data": msg}))
.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

View File

@ -20,7 +20,7 @@ CREATE TABLE teams (
tournamentId INTEGER NOT NULL,
name TEXT NOT NULL,
FOREIGN KEY (tournamentId) REFERENCES tournaments (id)
FOREIGN KEY (tournamentId) REFERENCES tournaments (id) ON DELETE CASCADE
);
CREATE TABLE matches (
@ -32,7 +32,7 @@ CREATE TABLE matches (
winnerId 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 (team2Id) 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,
setMatchWinner: setMatchWinner,
createTournament: createTournament,
deleteTournament: deleteTournament,
editTournament: editTournament,
getTeamsByTournamentId: getTeamsByTournamentId,
}
@ -153,25 +154,30 @@ function getTournament(tournamentId) {
console.log(err);
reject(err);
} else {
if (tournaments.length == 0) {
reject("No such tournament exists");
}
if (tournaments.length == 0) {
reject("No such tournament exists");
return
}
getTeamsByTournamentId(tournamentId)
.catch(err => reject(err))
.then(teams => {
let tournament = tournaments[0];
//TODO: CHeckh this
// /home/felixalb/Documents/NTNU/semester2/sysut_server/src/server/tmdb.js:163
// tournament.teamCount = teams.length;
// ^
getTeamsByTournamentId(tournamentId)
.catch(err => reject(err))
.then(teams => {
let tournament = tournaments[0];
tournament.teamCount = teams.length;
resolve(tournament);
});
}
});
});
}
// TypeError: Cannot set properties of undefined (setting 'teamCount')
// at /home/felixalb/Documents/NTNU/semester2/sysut_server/src/server/tmdb.js:163:34
tournament.teamCount = teams.length;
resolve(tournament);
});
function deleteTournament(tournamentId) {
return new Promise(function(resolve, reject) {
connection.query("DELETE FROM tournaments WHERE id = ?", [escapeString(tournamentId)], (err, result) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});