2021-11-21 21:37:35 +00:00
|
|
|
var express = require('express');
|
|
|
|
var router = express.Router();
|
2021-11-26 22:50:43 +00:00
|
|
|
|
2021-11-21 22:30:54 +00:00
|
|
|
var sports = require('../database/scores/sports');
|
2021-11-21 22:40:48 +00:00
|
|
|
var seasons = require('../database/scores/seasons');
|
2021-11-21 23:24:43 +00:00
|
|
|
var genders = require('../database/scores/genders');
|
2021-11-21 23:48:03 +00:00
|
|
|
var divisions = require('../database/scores/divisions');
|
2021-11-22 00:15:02 +00:00
|
|
|
var teams = require('../database/scores/teams');
|
2021-11-22 04:59:07 +00:00
|
|
|
var games = require('../database/scores/games');
|
2021-11-25 05:29:29 +00:00
|
|
|
var accounts = require('../database/accounts/accounts');
|
2021-11-21 22:40:48 +00:00
|
|
|
|
2021-11-26 22:50:43 +00:00
|
|
|
var checkLoginStatus = require('./checkLoginStatus');
|
2021-11-21 21:37:35 +00:00
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/sports', async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const data = await sports.retrieveAll();
|
|
|
|
res.json(data);
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-22 22:30:02 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/sport', async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const sportID = req.query.sport;
|
|
|
|
const data = await sports.getFromID(sportID);
|
|
|
|
res.json(data);
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-21 21:37:35 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/seasons', async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const data = await seasons.retrieveAll();
|
|
|
|
res.json(data);
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-21 22:40:48 +00:00
|
|
|
})
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/genders', async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const sportID = req.query.sport;
|
|
|
|
const data = await genders.retrieveBySport(sportID);
|
|
|
|
res.json(data);
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-21 23:24:43 +00:00
|
|
|
})
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/divisions', async function(req, res, next) {
|
|
|
|
try{
|
|
|
|
let gender;
|
|
|
|
if(req.query.gender) gender = (req.query.gender == 'female' ? genders.FEMALE : genders.MALE);
|
|
|
|
|
|
|
|
const sportID = req.query.sport;
|
|
|
|
const data = await divisions.retrieve(sportID, gender);
|
|
|
|
res.json(data);
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-21 23:48:03 +00:00
|
|
|
})
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/division', async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const divisionID = req.query.division;
|
|
|
|
const data = await divisions.getFromID(divisionID);
|
|
|
|
res.json(data);
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-22 23:27:50 +00:00
|
|
|
})
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/teams', async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const sportID = req.query.sport;
|
|
|
|
const data = await teams.retrieve(sportID);
|
|
|
|
res.json(data);
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-22 00:15:02 +00:00
|
|
|
})
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/team', async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const teamID = req.query.team;
|
|
|
|
const data = await teams.getFromID(teamID);
|
|
|
|
res.json(data);
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-22 04:59:07 +00:00
|
|
|
})
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/games', async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const userID = req.user ? req.user[0] : null;
|
|
|
|
if(req.query.user) {
|
|
|
|
const data = await games.retrieveByUser(userID);
|
|
|
|
res.json(data);
|
|
|
|
} else {
|
|
|
|
const seasonID = req.query.season;
|
|
|
|
const divisionID = req.query.division;
|
|
|
|
const teamID = req.query.team;
|
|
|
|
const data = await games.retrieve(teamID, divisionID, seasonID);
|
|
|
|
res.json(data);
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-22 04:59:07 +00:00
|
|
|
})
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/game', async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const gameID = req.query.game;
|
2021-11-27 04:57:02 +00:00
|
|
|
const ofUser = req.query.ofuser;
|
|
|
|
const currentUserID = req.user ? req.user[0] : null;
|
2021-11-27 04:28:20 +00:00
|
|
|
|
|
|
|
let data;
|
|
|
|
if(gameID) data = await games.getFromID(gameID);
|
2021-11-27 04:57:02 +00:00
|
|
|
else if(ofUser) data = await games.getLatest(currentUserID);
|
|
|
|
else data = await games.getLatest();
|
2021-11-26 23:16:11 +00:00
|
|
|
res.json(data);
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-23 07:49:11 +00:00
|
|
|
})
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/accounts', checkLoginStatus.admin, async function(req, res, next) {
|
|
|
|
try {
|
|
|
|
const data = await accounts.retrieveAll();
|
|
|
|
res.json(data);
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
|
|
|
}
|
2021-11-25 05:29:29 +00:00
|
|
|
})
|
|
|
|
|
2021-11-26 23:16:11 +00:00
|
|
|
router.get('/account', checkLoginStatus.user, async function(req, res, next) {
|
|
|
|
try{
|
|
|
|
const userIsAdmin = req.user[2];
|
|
|
|
const loggedInAccountID = req.user[0];
|
|
|
|
const requestedAccountID = req.query.account;
|
|
|
|
|
|
|
|
if(!userIsAdmin && loggedInAccountID != requestedAccountID) {
|
|
|
|
res.status(403).send("ACCESS DENIED");
|
|
|
|
} else {
|
|
|
|
const data = await accounts.getFromID(req.query.account);
|
|
|
|
res.json(data);
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
res.status(500).send("An error has occurred");
|
2021-11-26 19:08:45 +00:00
|
|
|
}
|
2021-11-25 19:04:18 +00:00
|
|
|
})
|
|
|
|
|
2021-11-21 21:37:35 +00:00
|
|
|
module.exports = router;
|