Improve error management for games

main
sudoer777 2021-11-26 16:29:03 -07:00
parent 292c4a7039
commit f9ac3f49cd
2 changed files with 42 additions and 30 deletions

View File

@ -21,39 +21,50 @@ router.get('/' ,checkLoginStatus.user, function(req, res, next) {
router.get('/game', checkLoginStatus.user, function(req, res, next) {
let title = req.query.game ? 'Edit Game' : 'Submit Score'
res.render('manage/addgame', { title, userLoggedIn: !!req.user });
res.render('manage/addgame', { title, userLoggedIn: !!req.user, message: req.flash('error') });
});
router.post('/game', checkLoginStatus.user, function(req, res, next) {
const seasonID = req.body['year'];
const sportID = req.body['sport'];
const gender = (req.body['gender'] == "female") ? genders.FEMALE : genders.MALE;
const divisionID = req.body['division'];
const date = req.body['date'];
const team1ID = req.body['team1'];
const team1Score = req.body['team1-score'];
const team2ID = req.body['team2'];
const team2Score = req.body['team2-score'];
const userID = req.user[0];
router.post('/game', checkLoginStatus.user, async function(req, res, next) {
try {
const seasonID = req.body['year'];
const sportID = req.body['sport'];
const gender = (req.body['gender'] == "female") ? genders.FEMALE : genders.MALE;
const divisionID = req.body['division'];
const date = req.body['date'];
const team1ID = req.body['team1'];
const team1Score = req.body['team1-score'];
const team2ID = req.body['team2'];
const team2Score = req.body['team2-score'];
const userID = req.user[0];
const id = req.body['game'];
const remove = req.body['remove'];
const id = req.body['game'];
const remove = req.body['remove'];
const loggedInUserID = req.user[0];
const loggedInUserIsAdmin = req.user[2];
const loggedInUserID = req.user[0];
const loggedInUserIsAdmin = req.user[2];
games.getFromID(id)
.then(game => {
if(!loggedInUserIsAdmin && loggedInUserID != game.submitterID) {
res.status(403).send("ACCESS DENIED");
}
else if(remove) games.remove(id)
.then(res.redirect("/manage"));
else if(id) games.edit(id, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score)
.then(res.redirect('/manage'));
else games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID)
.then(res.redirect("/"));
});
const game = id ? await games.getFromID(id) : null;
if(!loggedInUserIsAdmin && game && loggedInUserID != game.submitterID) {
res.status(403).send("ACCESS DENIED");
}
else if(remove) {
await games.remove(id);
res.redirect("/manage");
}
else if(id) {
await games.edit(id, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score);
res.redirect('/manage');
}
else {
await games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID);
res.redirect('/');
}
} catch(err) {
console.error("ERROR: " + err.message);
req.flash("error", "An error has occurred.");
res.redirect('/manage/game');
}
});
router.get('/season', checkLoginStatus.admin, function(req, res, next) {

View File

@ -45,6 +45,7 @@ block content
label Score
span(class='form-section-input')
input#team2-score-textbox(type="number", name="team2-score", value="0" disabled)
.error #{message}
span(class='form-section')
button#submit-button(type="submit" disabled) Submit
span(class='form-section')