diff --git a/routes/manage.js b/routes/manage.js index 44abc09..d343952 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -68,17 +68,25 @@ router.post('/game', checkLoginStatus.user, async function(req, res, next) { }); router.get('/season', checkLoginStatus.admin, function(req, res, next) { - res.render('manage/addseason', { title: 'Add Season', currentYear : (new Date()).getFullYear(), userLoggedIn: !!req.user }); + res.render('manage/addseason', { title: 'Add Season', currentYear : (new Date()).getFullYear(), userLoggedIn: !!req.user, message: req.flash('error') }); }); -router.post('/season', checkLoginStatus.admin, function(req, res, next) { - const year = req.body['year']; +router.post('/season', checkLoginStatus.admin, async function(req, res, next) { + try { + const year = req.body['year']; - const seasonID = req.body['season']; - const remove = req.body['remove']; + const seasonID = req.body['season']; + const remove = req.body['remove']; - if(remove) seasons.remove(seasonID).then(res.redirect('/manage')); - else seasons.add(year).then(res.redirect("/manage")); + if(remove) await seasons.remove(seasonID); + else await seasons.add(year); + + res.redirect('/manage'); + } catch(err) { + console.error("ERROR: " + err.message); + req.flash("error", "An error has occurred."); + res.redirect('/manage/season'); + } }); router.get('/sport', checkLoginStatus.admin, function(req, res, next) { diff --git a/views/manage/addseason.pug b/views/manage/addseason.pug index 27783b5..ba43698 100644 --- a/views/manage/addseason.pug +++ b/views/manage/addseason.pug @@ -10,6 +10,7 @@ block content label Ending year span(class='form-section-input') input#season-textbox(type="number", name="year", value=currentYear) + .error #{message} span(class='form-section') button#submit-button(type="submit" disabled) Submit