diff --git a/routes/manage.js b/routes/manage.js index 506a5f1..44abc09 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -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 loggedInUserID = req.user[0]; + const loggedInUserIsAdmin = req.user[2]; + + const game = id ? await games.getFromID(id) : null; - const id = req.body['game']; - const remove = req.body['remove']; - - 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("/")); - }); + 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) { diff --git a/views/manage/addgame.pug b/views/manage/addgame.pug index 61e8f61..3a44834 100644 --- a/views/manage/addgame.pug +++ b/views/manage/addgame.pug @@ -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')