2022-03-15 14:46:24 +01:00
|
|
|
const path = require("path");
|
|
|
|
const express = require("express");
|
2022-03-16 14:33:07 +01:00
|
|
|
require("dotenv").config();
|
2022-03-15 14:46:24 +01:00
|
|
|
|
2022-03-20 15:34:13 +01:00
|
|
|
// Our self-written module for handling database operations
|
|
|
|
let tmdb = require("./tmdb.js");
|
2022-03-15 14:46:24 +01:00
|
|
|
|
2022-03-20 15:34:13 +01:00
|
|
|
// #region Express setup
|
2022-03-16 14:33:07 +01:00
|
|
|
const app = express();
|
2022-03-25 02:43:57 +01:00
|
|
|
const port = 3001;
|
2022-03-15 14:46:24 +01:00
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Listening on port ${port}`)
|
|
|
|
})
|
2022-03-20 21:06:35 +01:00
|
|
|
app.use(express.json());
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
2022-03-21 09:28:26 +01:00
|
|
|
let api = express.Router();
|
|
|
|
app.use("/api", api);
|
2022-03-24 18:47:35 +01:00
|
|
|
|
|
|
|
api.use(function(req, res, next) {
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
|
|
next();
|
|
|
|
});
|
2022-03-25 15:01:36 +01:00
|
|
|
api.use(require('express-log-url'));
|
2022-03-24 18:47:35 +01:00
|
|
|
|
2022-03-20 15:34:13 +01:00
|
|
|
// #endregion
|
2022-03-15 14:46:24 +01:00
|
|
|
|
2022-03-20 15:34:13 +01:00
|
|
|
// #region frontend
|
|
|
|
// Serve static files from the React app
|
2022-03-28 19:32:11 +02:00
|
|
|
app.use('/static', express.static(path.join(__dirname, 'clientbuild')));
|
|
|
|
|
2022-03-20 15:34:13 +01:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region API
|
2022-03-21 09:28:26 +01:00
|
|
|
api.get("/tournament/getTournaments", (req, res) => {
|
2022-03-20 15:34:13 +01:00
|
|
|
tmdb.getTournaments()
|
2022-03-24 18:47:35 +01:00
|
|
|
.then(tournaments => res.json({"status": "OK", "data": tournaments}))
|
|
|
|
.catch(err => res.json({"status": "error", "data": err}));
|
|
|
|
});
|
|
|
|
|
2022-03-27 22:21:05 +02:00
|
|
|
// #region tournament/:tournamentId
|
2022-03-24 18:47:35 +01:00
|
|
|
api.get("/tournament/:tournamentId", (req, res) => {
|
|
|
|
let tournamentId = req.params.tournamentId;
|
|
|
|
if (isNaN(tournamentId)) {
|
|
|
|
res.json({"status": "error", "data": "Invalid tournament id"});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tmdb.getTournament(parseInt(tournamentId))
|
|
|
|
.catch(err => res.json({"status": "error", "data": err}))
|
|
|
|
.then(tournament => res.json({"status": "OK", "data": tournament}));
|
2022-03-20 15:34:13 +01:00
|
|
|
});
|
2022-03-16 14:33:07 +01:00
|
|
|
|
2022-03-21 09:28:26 +01:00
|
|
|
api.get("/tournament/:tournamentId/getMatches", (req, res) => {
|
2022-03-16 15:40:33 +01:00
|
|
|
let tournamentId = req.params.tournamentId;
|
2022-03-16 15:46:04 +01:00
|
|
|
if (isNaN(tournamentId)) {
|
|
|
|
res.json({"status": "error", "data": "tournamentId must be a number"});
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tournamentId = parseInt(tournamentId);
|
2022-03-20 15:34:13 +01:00
|
|
|
tmdb.getMatchesByTournamentId(tournamentId)
|
2022-03-24 18:47:35 +01:00
|
|
|
.then(matches => res.send({"status": "OK", "data": matches}))
|
|
|
|
.catch(err => res.send({"status": "error", "data": err}));
|
2022-03-15 14:46:24 +01:00
|
|
|
});
|
2022-03-20 21:06:35 +01:00
|
|
|
|
2022-03-24 21:45:47 +01:00
|
|
|
api.get("/tournament/:tournamentId/getTeams", (req, res) => {
|
|
|
|
let tournamentId = req.params.tournamentId;
|
|
|
|
if (!tournamentId || isNaN(tournamentId)) {
|
|
|
|
res.json({"status": "error", "data": "tournamentId must be a number"});
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tournamentId = parseInt(tournamentId);
|
|
|
|
tmdb.getTeamsByTournamentId(tournamentId)
|
|
|
|
.then(teams => res.send({"status": "OK", "data": teams}))
|
|
|
|
.catch(err => res.send({"status": "error", "data": err}));
|
|
|
|
});
|
2022-03-27 22:21:05 +02:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region match/:matchId
|
2022-03-24 21:45:47 +01:00
|
|
|
|
2022-03-27 22:21:05 +02:00
|
|
|
|
|
|
|
api.get("/match/:matchId", (req, res) => {
|
2022-03-20 21:06:35 +01:00
|
|
|
let matchId = req.params.matchId;
|
|
|
|
if (isNaN(matchId)) {
|
|
|
|
res.json({"status": "error", "data": "matchId must be a number"});
|
|
|
|
return
|
|
|
|
}
|
|
|
|
matchId = parseInt(matchId);
|
|
|
|
tmdb.getMatch(matchId)
|
2022-03-24 18:47:35 +01:00
|
|
|
.then(match => res.send({"status": "OK", "data": match}))
|
|
|
|
.catch(err => res.send({"status": "error", "data": err}));
|
2022-03-20 21:06:35 +01:00
|
|
|
});
|
|
|
|
|
2022-03-21 09:28:26 +01:00
|
|
|
api.post("/match/:matchId/setWinner", (req, res) => {
|
2022-03-20 21:06:35 +01:00
|
|
|
let matchId = req.params.matchId;
|
|
|
|
let winnerId = req.body.winnerId;
|
|
|
|
if (isNaN(matchId)) {
|
|
|
|
res.json({"status": "error", "data": "matchId must be a number"});
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (winnerId == undefined || isNaN(winnerId)) {
|
|
|
|
res.json({"status": "error", "data": "winnerId must be a number"});
|
|
|
|
return
|
|
|
|
}
|
2022-03-24 18:47:35 +01:00
|
|
|
|
2022-03-20 21:06:35 +01:00
|
|
|
matchId = parseInt(matchId);
|
|
|
|
winnerId = parseInt(winnerId);
|
|
|
|
tmdb.setMatchWinner(matchId, winnerId)
|
2022-03-24 18:47:35 +01:00
|
|
|
.then(match => res.send({"status": "OK", "data": match}))
|
|
|
|
.catch(err => res.send({"status": "error", "data": err}));
|
|
|
|
});
|
2022-03-27 22:21:05 +02:00
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region team/:teamId
|
|
|
|
api.get("/team/:teamId", (req, res) => {
|
|
|
|
let teamId = req.params.teamId;
|
|
|
|
if (isNaN(teamId)) {
|
|
|
|
res.json({"status": "error", "data": "teamId must be a number"});
|
|
|
|
return
|
|
|
|
}
|
|
|
|
teamId = parseInt(teamId);
|
|
|
|
tmdb.getTeam(teamId)
|
|
|
|
.then(match => res.send({"status": "OK", "data": match}))
|
|
|
|
.catch(err => res.send({"status": "error", "data": err}));
|
|
|
|
});
|
|
|
|
|
|
|
|
api.post("/team/:teamId/edit", (req, res) => {
|
|
|
|
let teamId = req.params.teamId;
|
2022-03-28 15:19:55 +02:00
|
|
|
let teamName = req.body.name;
|
|
|
|
console.log(req.body);
|
2022-03-27 22:21:05 +02:00
|
|
|
if (isNaN(teamId)) {
|
|
|
|
res.json({"status": "error", "data": "teamId must be a number"});
|
|
|
|
return
|
|
|
|
}
|
2022-03-28 15:19:55 +02:00
|
|
|
if (teamName == undefined || teamName == "") {
|
|
|
|
res.json({"status": "error", "data": "teamName must be a non-empty string"});
|
2022-03-27 22:21:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
teamId = parseInt(teamId);
|
|
|
|
tmdb.editTeam(teamId, teamName)
|
|
|
|
.then(match => res.send({"status": "OK", "data": match}))
|
|
|
|
.catch(err => res.send({"status": "error", "data": err}));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// #endregion
|
2022-03-24 18:47:35 +01:00
|
|
|
|
|
|
|
//Takes JSON body
|
|
|
|
api.post("/tournament/create", (req, res) => {
|
|
|
|
//Check that req body is valid
|
|
|
|
if (req.body.name == undefined || req.body.name == "") {
|
|
|
|
res.json({"status": "error", "data": "No data supplied"});
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//Check that req is json
|
|
|
|
// if (req.get("Content-Type") != "application/json") {
|
|
|
|
console.log(req.get("Content-Type"));
|
|
|
|
let name = req.body.name;
|
|
|
|
let description = req.body.description;
|
|
|
|
let teamLimit = req.body.teamLimit;
|
2022-03-28 15:19:55 +02:00
|
|
|
let startDate = req.body.startDate; //TODO: timezones, 2 hr skips
|
2022-03-24 18:47:35 +01:00
|
|
|
let endDate = req.body.endDate;
|
|
|
|
if (name == undefined || name == "" || description == undefined || description == "") {
|
|
|
|
res.json({"status": "error", "data": "name and description must be provided"});
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (teamLimit == undefined ) {
|
|
|
|
res.json({"status": "error", "data": "teamLimit must be provided"});
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
teamLimit = parseInt(teamLimit);
|
|
|
|
} catch (err) {
|
|
|
|
res.json({"status": "error", "data": "teamLimit must be a number"});
|
|
|
|
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.createTournament(name, description, startDate, endDate, teamLimit)
|
2022-03-28 15:19:55 +02:00
|
|
|
.then(msg => res.json({"status": "OK", "data": msg}))
|
|
|
|
.catch(err => res.json({"status": "error", "data": err}));
|
|
|
|
|
2022-03-20 21:06:35 +01:00
|
|
|
});
|
2022-03-25 15:01:36 +01:00
|
|
|
|
|
|
|
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)
|
2022-03-28 15:19:55 +02:00
|
|
|
.then(msg => res.json({"status": "OK", "data": msg}))
|
|
|
|
.catch(err => res.json({"status": "error", "data": err}));
|
|
|
|
|
2022-03-25 15:01:36 +01:00
|
|
|
});
|
2022-03-28 15:19:55 +02:00
|
|
|
|
|
|
|
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}));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2022-03-20 15:34:13 +01:00
|
|
|
// #endregion
|