Add better error handling for data.js route
parent
4f3581b1e5
commit
6705f06e18
147
routes/data.js
147
routes/data.js
|
@ -11,66 +11,137 @@ var accounts = require('../database/accounts/accounts');
|
||||||
|
|
||||||
var checkLoginStatus = require('./checkLoginStatus');
|
var checkLoginStatus = require('./checkLoginStatus');
|
||||||
|
|
||||||
router.get('/sports', function(req, res, next) {
|
router.get('/sports', async function(req, res, next) {
|
||||||
sports.retrieveAll()
|
try {
|
||||||
.then(data => res.json(data));
|
const data = await sports.retrieveAll();
|
||||||
|
res.json(data);
|
||||||
|
} catch(err) {
|
||||||
|
console.error("ERROR: " + err.message);
|
||||||
|
res.status(500).send("An error has occurred");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/sport', function(req, res, next) {
|
router.get('/sport', async function(req, res, next) {
|
||||||
sports.getFromID(req.query.sport)
|
try {
|
||||||
.then(data => res.json(data));
|
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");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get('/seasons', function(req, res, next) {
|
router.get('/seasons', async function(req, res, next) {
|
||||||
seasons.retrieveAll()
|
try {
|
||||||
.then(data => res.json(data));
|
const data = await seasons.retrieveAll();
|
||||||
|
res.json(data);
|
||||||
|
} catch(err) {
|
||||||
|
console.error("ERROR: " + err.message);
|
||||||
|
res.status(500).send("An error has occurred");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/genders', function(req, res, next) {
|
router.get('/genders', async function(req, res, next) {
|
||||||
genders.retrieveBySport(req.query.sport)
|
try {
|
||||||
.then(data => res.json(data));
|
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");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/divisions', function(req, res, next) {
|
router.get('/divisions', async function(req, res, next) {
|
||||||
|
try{
|
||||||
let gender;
|
let gender;
|
||||||
if(req.query.gender) gender = (req.query.gender == 'female' ? genders.FEMALE : genders.MALE);
|
if(req.query.gender) gender = (req.query.gender == 'female' ? genders.FEMALE : genders.MALE);
|
||||||
|
|
||||||
divisions.retrieve(req.query.sport, gender)
|
const sportID = req.query.sport;
|
||||||
.then(data => res.json(data));
|
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");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/division', function(req, res, next) {
|
router.get('/division', async function(req, res, next) {
|
||||||
divisions.getFromID(req.query.division)
|
try {
|
||||||
.then(data => res.json(data));
|
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");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/teams', function(req, res, next) {
|
router.get('/teams', async function(req, res, next) {
|
||||||
teams.retrieve(req.query.sport)
|
try {
|
||||||
.then(data => res.json(data));
|
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");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/team', function(req, res, next) {
|
router.get('/team', async function(req, res, next) {
|
||||||
teams.getFromID(req.query.team)
|
try {
|
||||||
.then(data => res.json(data));
|
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");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/games', function(req, res, next) {
|
router.get('/games', async function(req, res, next) {
|
||||||
|
try {
|
||||||
const userID = req.user ? req.user[0] : null;
|
const userID = req.user ? req.user[0] : null;
|
||||||
if(req.query.user) games.retrieveByUser(userID).then(data => res.json(data));
|
if(req.query.user) {
|
||||||
else games.retrieve(req.query.team, req.query.division, req.query.season).then(data => res.json(data));
|
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");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/game', function(req, res, next) {
|
router.get('/game', async function(req, res, next) {
|
||||||
games.getFromID(req.query.game)
|
try {
|
||||||
.then(data => res.json(data));
|
const gameID = req.query.game;
|
||||||
|
const data = await games.getFromID(gameID);
|
||||||
|
res.json(data);
|
||||||
|
} catch(err) {
|
||||||
|
console.error("ERROR: " + err.message);
|
||||||
|
res.status(500).send("An error has occurred");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/accounts', checkLoginStatus.admin, function(req, res, next) {
|
router.get('/accounts', checkLoginStatus.admin, async function(req, res, next) {
|
||||||
accounts.retrieveAll()
|
try {
|
||||||
.then(data => res.json(data));
|
const data = await accounts.retrieveAll();
|
||||||
|
res.json(data);
|
||||||
|
} catch(err) {
|
||||||
|
console.error("ERROR: " + err.message);
|
||||||
|
res.status(500).send("An error has occurred");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/account', checkLoginStatus.user, function(req, res, next) {
|
router.get('/account', checkLoginStatus.user, async function(req, res, next) {
|
||||||
|
try{
|
||||||
const userIsAdmin = req.user[2];
|
const userIsAdmin = req.user[2];
|
||||||
const loggedInAccountID = req.user[0];
|
const loggedInAccountID = req.user[0];
|
||||||
const requestedAccountID = req.query.account;
|
const requestedAccountID = req.query.account;
|
||||||
|
@ -78,8 +149,12 @@ router.get('/account', checkLoginStatus.user, function(req, res, next) {
|
||||||
if(!userIsAdmin && loggedInAccountID != requestedAccountID) {
|
if(!userIsAdmin && loggedInAccountID != requestedAccountID) {
|
||||||
res.status(403).send("ACCESS DENIED");
|
res.status(403).send("ACCESS DENIED");
|
||||||
} else {
|
} else {
|
||||||
accounts.getFromID(req.query.account)
|
const data = await accounts.getFromID(req.query.account);
|
||||||
.then(data => res.json(data));
|
res.json(data);
|
||||||
|
}
|
||||||
|
} catch(err) {
|
||||||
|
console.error("ERROR: " + err.message);
|
||||||
|
res.status(500).send("An error has occurred");
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Reference in New Issue