asura-tmdb/src/server/index.js

152 lines
4.8 KiB
JavaScript
Raw Normal View History

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();
const port = 3000;
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-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-21 09:28:26 +01:00
api.get("/", (req, res) => {
2022-03-16 14:33:07 +01:00
res.sendFile(path.join(__dirname, "public", "landing.html"));
});
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}));
});
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}));
// .then(tournament => res.json({"status": "OK", "data": tournament}));
// .then(console.log("lol"))
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;
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-21 09:28:26 +01:00
api.get("/match/:matchId/getMatch", (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-24 18:47:35 +01:00
// JSON body: {"winner": teamId}
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}));
});
//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;
let startDate = req.body.startDate;
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)
.catch(err => res.json({"status": "error", "data": err}))
.then(msg => res.json({"status": "OK", "data": msg}));
2022-03-20 21:06:35 +01:00
});
2022-03-20 15:34:13 +01:00
// #endregion