This repository has been archived on 2024-04-05. You can view files and clone it, but cannot push or open issues/pull-requests.
score-tracker/routes/fetch.js

200 lines
6.2 KiB
JavaScript

var express = require('express');
var router = express.Router();
var sports = require('../database/scores/sports');
var seasons = require('../database/scores/seasons');
var genders = require('../database/scores/genders');
var divisions = require('../database/scores/divisions');
var teams = require('../database/scores/teams');
var games = require('../database/scores/games');
var accounts = require('../database/accounts/accounts');
var checkLoginStatus = require('./checkLoginStatus');
router.get('/index/dropdown', async function(req, res, next) {
let latestGame;
let seasonsData;
let sportsData;
let gendersData;
let divisionsData;
let teamsData;
try {
latestGame = await games.getLatest();
let division = await divisions.getFromID(latestGame.divisionID);
latestGame.sportID = division.sportID;
latestGame.gender = division.gender;
} catch {
latestGame = null;
}
try {
if(latestGame) {
seasonsData = await seasons.retrieveAll();
sportsData = await sports.retrieveAll();
gendersData = await genders.retrieveBySport(latestGame.sportID);
divisionsData = await divisions.retrieve(latestGame.sportID, latestGame.gender);
teamsData = await teams.retrieve(latestGame.sportID);
} else {
seasonsData = await seasons.retrieveAll();
sportsData = await sports.retrieveAll();
gendersData = await genders.retrieveBySport(sportsData[0].id);
divisionsData = await divisions.retrieve(sportsData[0].id);
teamsData = await teams.retrieve(sportsData[0].id);
}
res.json({
seasons : seasonsData,
sports : sportsData,
genders : gendersData,
divisions : divisionsData,
teams: teamsData,
latestGame
});
} catch(err) {
console.error("ERROR: " + err.message);
res.status(500).send("An error has occurred");
}
});
router.get('/index/scores', async function (req, res, next) {
try {
const seasonID = req.query.season;
const divisionID = req.query.division;
const teamID = req.query.team;
const data = await games.retrieve(teamID, divisionID, seasonID);
for (const game of data) {
game.opponent = await teams.getFromID(game.team2ID);
}
res.json(data);
} catch(err) {
console.error("ERROR: " + err.message);
res.status(500).send("An error has occurred");
}
});
router.get('/submit', async function(req, res, next) {
let latestGame;
let seasonsData;
let sportsData;
let gendersData;
let divisionsData;
let teamsData;
const userID = req.user ? req.user[0] : null;
try {
latestGame = await games.getLatest(userID);
let division = await divisions.getFromID(latestGame.divisionID);
latestGame.sportID = division.sportID;
latestGame.gender = division.gender;
} catch {
latestGame = null;
}
try {
if(latestGame) {
seasonsData = await seasons.retrieveAll();
sportsData = await sports.retrieveAll();
gendersData = await genders.retrieveBySport(latestGame.sportID);
divisionsData = await divisions.retrieve(latestGame.sportID, latestGame.gender);
teamsData = await teams.retrieve(latestGame.sportID);
} else {
seasonsData = await seasons.retrieveAll();
sportsData = await sports.retrieveAll();
gendersData = await genders.retrieveBySport(sportsData[0].id);
divisionsData = await divisions.retrieve(sportsData[0].id);
teamsData = await teams.retrieve(sportsData[0].id);
}
res.json({
seasons : seasonsData,
sports : sportsData,
genders : gendersData,
divisions : divisionsData,
teams: teamsData,
latestGame
});
} catch(err) {
console.error("ERROR: " + err.message);
res.status(500).send("An error has occurred");
}
});
router.get('/manage/divisions', async function (req, res, next) {
const data = await divisions.retrieve();
for(const division of data) {
division.sport = await sports.getFromID(division.sportID);
}
res.json(data);
});
router.get('/manage/division', async function (req, res, next) {
try {
const divisionID = req.query.division;
const data = await divisions.getFromID(divisionID);
data.sport = await sports.getFromID(data.sportID);
res.json(data);
} catch(err) {
console.error("ERROR: " + err.message);
res.status(500).send("An error has occurred");
}
});
router.get('/manage/teams', async function (req, res, next) {
const data = await teams.retrieve();
for(const team of data) {
team.sport = await sports.getFromID(team.sportID);
}
res.json(data);
});
router.get('/manage/team', async function (req, res, next) {
try {
const teamID = req.query.team;
const data = await teams.getFromID(teamID);
data.sport = await sports.getFromID(data.sportID);
res.json(data);
} catch(err) {
console.error("ERROR: " + err.message);
res.status(500).send("An error has occurred");
}
});
router.get('/manage/games', checkLoginStatus.user, async function (req, res, next) {
try{
const userIsAdmin = req.user[2];
const loggedInAccountID = req.user[0];
if(!userIsAdmin) {
res.status(403).send("ACCESS DENIED");
} else {
const data = await games.retrieve();
for(const game of data) {
game.team1 = await teams.getFromID(game.team1ID);
game.team2 = await teams.getFromID(game.team2ID);
game.division = await divisions.getFromID(game.divisionID);
game.sport = await sports.getFromID(game.division.sportID);
game.submitter = game.submitterName || (await accounts.getFromID(game.submitterID));
}
res.json(data);
}
} catch(err) {
console.error("ERROR: " + err.message);
res.status(500).send("An error has occurred");
}
});
module.exports = router;