Delete tournaments, clean up
This commit is contained in:
parent
12199d5f12
commit
a95a2f6ea8
|
@ -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
|
||||||
|
@ -231,66 +303,4 @@ api.post("/tournament/create", (req, res) => {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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,
|
||||||
}
|
}
|
||||||
|
@ -155,20 +156,13 @@ function getTournament(tournamentId) {
|
||||||
} 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
|
|
||||||
// /home/felixalb/Documents/NTNU/semester2/sysut_server/src/server/tmdb.js:163
|
|
||||||
// tournament.teamCount = teams.length;
|
|
||||||
// ^
|
|
||||||
|
|
||||||
// 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;
|
tournament.teamCount = teams.length;
|
||||||
resolve(tournament);
|
resolve(tournament);
|
||||||
});
|
});
|
||||||
|
@ -177,6 +171,18 @@ function getTournament(tournamentId) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function getMatchesByTournamentId(tournamentId) {
|
function getMatchesByTournamentId(tournamentId) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
connection.query("SELECT * FROM matches WHERE tournamentId = ?", [escapeString(tournamentId)], (err, matches) => {
|
connection.query("SELECT * FROM matches WHERE tournamentId = ?", [escapeString(tournamentId)], (err, matches) => {
|
||||||
|
|
Loading…
Reference in New Issue