Improve error management for games
parent
292c4a7039
commit
f9ac3f49cd
|
@ -21,39 +21,50 @@ router.get('/' ,checkLoginStatus.user, function(req, res, next) {
|
||||||
router.get('/game', checkLoginStatus.user, function(req, res, next) {
|
router.get('/game', checkLoginStatus.user, function(req, res, next) {
|
||||||
let title = req.query.game ? 'Edit Game' : 'Submit Score'
|
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) {
|
router.post('/game', checkLoginStatus.user, async function(req, res, next) {
|
||||||
const seasonID = req.body['year'];
|
try {
|
||||||
const sportID = req.body['sport'];
|
const seasonID = req.body['year'];
|
||||||
const gender = (req.body['gender'] == "female") ? genders.FEMALE : genders.MALE;
|
const sportID = req.body['sport'];
|
||||||
const divisionID = req.body['division'];
|
const gender = (req.body['gender'] == "female") ? genders.FEMALE : genders.MALE;
|
||||||
const date = req.body['date'];
|
const divisionID = req.body['division'];
|
||||||
const team1ID = req.body['team1'];
|
const date = req.body['date'];
|
||||||
const team1Score = req.body['team1-score'];
|
const team1ID = req.body['team1'];
|
||||||
const team2ID = req.body['team2'];
|
const team1Score = req.body['team1-score'];
|
||||||
const team2Score = req.body['team2-score'];
|
const team2ID = req.body['team2'];
|
||||||
const userID = req.user[0];
|
const team2Score = req.body['team2-score'];
|
||||||
|
const userID = req.user[0];
|
||||||
|
|
||||||
|
const id = req.body['game'];
|
||||||
|
const remove = req.body['remove'];
|
||||||
|
|
||||||
|
const loggedInUserID = req.user[0];
|
||||||
|
const loggedInUserIsAdmin = req.user[2];
|
||||||
|
|
||||||
|
const game = id ? await games.getFromID(id) : null;
|
||||||
|
|
||||||
const id = req.body['game'];
|
if(!loggedInUserIsAdmin && game && loggedInUserID != game.submitterID) {
|
||||||
const remove = req.body['remove'];
|
res.status(403).send("ACCESS DENIED");
|
||||||
|
}
|
||||||
const loggedInUserID = req.user[0];
|
else if(remove) {
|
||||||
const loggedInUserIsAdmin = req.user[2];
|
await games.remove(id);
|
||||||
|
res.redirect("/manage");
|
||||||
games.getFromID(id)
|
}
|
||||||
.then(game => {
|
else if(id) {
|
||||||
if(!loggedInUserIsAdmin && loggedInUserID != game.submitterID) {
|
await games.edit(id, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score);
|
||||||
res.status(403).send("ACCESS DENIED");
|
res.redirect('/manage');
|
||||||
}
|
}
|
||||||
else if(remove) games.remove(id)
|
else {
|
||||||
.then(res.redirect("/manage"));
|
await games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID);
|
||||||
else if(id) games.edit(id, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score)
|
res.redirect('/');
|
||||||
.then(res.redirect('/manage'));
|
}
|
||||||
else games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID)
|
} catch(err) {
|
||||||
.then(res.redirect("/"));
|
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) {
|
router.get('/season', checkLoginStatus.admin, function(req, res, next) {
|
||||||
|
|
|
@ -45,6 +45,7 @@ block content
|
||||||
label Score
|
label Score
|
||||||
span(class='form-section-input')
|
span(class='form-section-input')
|
||||||
input#team2-score-textbox(type="number", name="team2-score", value="0" disabled)
|
input#team2-score-textbox(type="number", name="team2-score", value="0" disabled)
|
||||||
|
.error #{message}
|
||||||
span(class='form-section')
|
span(class='form-section')
|
||||||
button#submit-button(type="submit" disabled) Submit
|
button#submit-button(type="submit" disabled) Submit
|
||||||
span(class='form-section')
|
span(class='form-section')
|
||||||
|
|
Reference in New Issue