2021-11-21 21:37:35 +00:00
|
|
|
var express = require('express');
|
|
|
|
var router = express.Router();
|
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-25 05:29:29 +00:00
|
|
|
function adminLoggedIn(req, res, next) {
|
|
|
|
if (req.user && req.user[2]) {
|
2021-11-26 19:08:45 +00:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
req.flash('error', 'An admin account is required to access this page.');
|
|
|
|
res.redirect('/auth/login');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function userLoggedIn(req, res, next) {
|
|
|
|
if (req.user) {
|
2021-11-25 05:29:29 +00:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.redirect('/auth/login');
|
|
|
|
}
|
2021-11-26 19:08:45 +00:00
|
|
|
}
|
2021-11-21 21:37:35 +00:00
|
|
|
|
2021-11-21 22:30:54 +00:00
|
|
|
router.get('/sports', function(req, res, next) {
|
|
|
|
sports.retrieveAll()
|
|
|
|
.then(data => res.json(data));
|
2021-11-22 22:30:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/sport', function(req, res, next) {
|
|
|
|
sports.getFromID(req.query.sport)
|
|
|
|
.then(data => res.json(data));
|
2021-11-21 21:37:35 +00:00
|
|
|
});
|
|
|
|
|
2021-11-21 22:40:48 +00:00
|
|
|
router.get('/seasons', function(req, res, next) {
|
|
|
|
seasons.retrieveAll()
|
|
|
|
.then(data => res.json(data));
|
|
|
|
})
|
|
|
|
|
2021-11-21 23:24:43 +00:00
|
|
|
router.get('/genders', function(req, res, next) {
|
|
|
|
genders.retrieveBySport(req.query.sport)
|
|
|
|
.then(data => res.json(data));
|
|
|
|
})
|
|
|
|
|
2021-11-21 23:48:03 +00:00
|
|
|
router.get('/divisions', function(req, res, next) {
|
2021-11-22 22:20:59 +00:00
|
|
|
let gender;
|
|
|
|
if(req.query.gender) gender = (req.query.gender == 'female' ? genders.FEMALE : genders.MALE);
|
2021-11-21 23:48:03 +00:00
|
|
|
|
2021-11-22 22:20:59 +00:00
|
|
|
divisions.retrieve(req.query.sport, gender)
|
2021-11-21 23:48:03 +00:00
|
|
|
.then(data => res.json(data));
|
|
|
|
})
|
|
|
|
|
2021-11-22 23:27:50 +00:00
|
|
|
router.get('/division', function(req, res, next) {
|
|
|
|
divisions.getFromID(req.query.division)
|
|
|
|
.then(data => res.json(data));
|
|
|
|
})
|
|
|
|
|
2021-11-22 00:15:02 +00:00
|
|
|
router.get('/teams', function(req, res, next) {
|
2021-11-22 22:45:19 +00:00
|
|
|
teams.retrieve(req.query.sport)
|
2021-11-22 00:15:02 +00:00
|
|
|
.then(data => res.json(data));
|
|
|
|
})
|
|
|
|
|
2021-11-22 04:59:07 +00:00
|
|
|
router.get('/team', function(req, res, next) {
|
|
|
|
teams.getFromID(req.query.team)
|
|
|
|
.then(data => res.json(data));
|
|
|
|
})
|
|
|
|
|
|
|
|
router.get('/games', function(req, res, next) {
|
2021-11-26 19:53:54 +00:00
|
|
|
const userID = req.user ? req.user[0] : null;
|
2021-11-26 02:21:21 +00:00
|
|
|
if(req.query.user) games.retrieveByUser(userID).then(data => res.json(data));
|
|
|
|
else games.retrieve(req.query.team, req.query.division, req.query.season).then(data => res.json(data));
|
2021-11-22 04:59:07 +00:00
|
|
|
})
|
|
|
|
|
2021-11-23 07:49:11 +00:00
|
|
|
router.get('/game', function(req, res, next) {
|
|
|
|
games.getFromID(req.query.game)
|
|
|
|
.then(data => res.json(data));
|
|
|
|
})
|
|
|
|
|
2021-11-25 05:29:29 +00:00
|
|
|
router.get('/accounts', adminLoggedIn, function(req, res, next) {
|
|
|
|
accounts.retrieveAll()
|
|
|
|
.then(data => res.json(data));
|
|
|
|
})
|
|
|
|
|
2021-11-26 19:08:45 +00:00
|
|
|
router.get('/account', userLoggedIn, function(req, res, next) {
|
|
|
|
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 {
|
|
|
|
accounts.getFromID(req.query.account)
|
|
|
|
.then(data => res.json(data));
|
|
|
|
}
|
2021-11-25 19:04:18 +00:00
|
|
|
})
|
|
|
|
|
2021-11-21 21:37:35 +00:00
|
|
|
module.exports = router;
|