diff --git a/routes/manage.js b/routes/manage.js index d343952..4558cfd 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -90,17 +90,25 @@ router.post('/season', checkLoginStatus.admin, async function(req, res, next) { }); router.get('/sport', checkLoginStatus.admin, function(req, res, next) { - res.render('manage/addsport', { title: 'Add Sport', userLoggedIn: !!req.user }); + res.render('manage/addsport', { title: 'Add Sport', userLoggedIn: !!req.user, message: req.flash('error') }); }); -router.post('/sport', checkLoginStatus.admin, function(req, res, next) { - const name = req.body['name']; - const id = req.body['sport']; - const remove = req.body['remove']; +router.post('/sport', checkLoginStatus.admin, async function(req, res, next) { + try { + const name = req.body['name']; + const id = req.body['sport']; + const remove = req.body['remove']; - if(remove) sports.remove(id).then(res.redirect('/manage')); - else if(id) sports.rename(id, name).then(res.redirect('/manage')); - else sports.add(name).then(res.redirect('/manage')); + if(remove) await sports.remove(id); + else if(id) await sports.rename(id, name); + else await sports.add(name); + + res.redirect('/manage'); + } catch(err) { + console.error("ERROR: " + err.message); + req.flash("error", "An error has occurred."); + res.redirect('/manage/sport'); + } }); router.get('/division', checkLoginStatus.admin, function(req, res, next) { diff --git a/views/manage/addsport.pug b/views/manage/addsport.pug index 067b9ac..b4caa50 100644 --- a/views/manage/addsport.pug +++ b/views/manage/addsport.pug @@ -10,6 +10,7 @@ block content label Sport name span(class='form-section-input') input#name-textbox(type="text" name="name" disabled) + .error #{message} span(class='form-section') button#submit-button(type="submit" disabled) Submit span(class='form-section')