Improve error handling for divisions
parent
3f6251f5bb
commit
ad361adab1
|
@ -114,10 +114,11 @@ router.post('/sport', checkLoginStatus.admin, async function(req, res, next) {
|
|||
router.get('/division', checkLoginStatus.admin, function(req, res, next) {
|
||||
let title = req.query.division ? 'Edit Division' : 'Add Division'
|
||||
|
||||
res.render('manage/adddivision', { title, userLoggedIn: !!req.user });
|
||||
res.render('manage/adddivision', { title, userLoggedIn: !!req.user, message: req.flash('error') });
|
||||
});
|
||||
|
||||
router.post('/division', checkLoginStatus.admin, function(req, res, next) {
|
||||
router.post('/division', checkLoginStatus.admin, async function(req, res, next) {
|
||||
try {
|
||||
const name = req.body['name'];
|
||||
const sport = req.body['sport'];
|
||||
const genderName = req.body['gender'];
|
||||
|
@ -125,21 +126,23 @@ router.post('/division', checkLoginStatus.admin, function(req, res, next) {
|
|||
const id = req.body['division'];
|
||||
const remove = req.body['remove'];
|
||||
|
||||
|
||||
if(remove) divisions.remove(id).then(res.redirect('/manage'));
|
||||
else if(id) divisions.rename(id, name).then(res.redirect('/manage'));
|
||||
else {
|
||||
if(genderName == "both") {
|
||||
divisions.add(name, genders.FEMALE, sport)
|
||||
.then(divisions.add(name, genders.MALE, sport)
|
||||
.then(res.redirect("/manage")));
|
||||
}
|
||||
if(remove) await divisions.remove(id);
|
||||
else if(id) await divisions.rename(id, name);
|
||||
else {
|
||||
if(genderName == 'both') {
|
||||
await divisions.add(name, genders.FEMALE, sport);
|
||||
await divisions.add(name, genders.MALE, sport);
|
||||
} else {
|
||||
const gender = (genderName == "female") ? genders.FEMALE : genders.MALE;
|
||||
divisions.add(name, gender, sport)
|
||||
.then(res.redirect("/manage"));
|
||||
await divisions.add(name, gender, sport);
|
||||
}
|
||||
}
|
||||
res.redirect('/manage');
|
||||
} catch(err) {
|
||||
console.error("ERROR: " + err.message);
|
||||
req.flash("error", "An error has occurred.");
|
||||
res.redirect('/manage/division');
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/team', checkLoginStatus.admin, function(req, res, next) {
|
||||
|
|
|
@ -21,6 +21,7 @@ block content
|
|||
label Division 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')
|
||||
|
|
Reference in New Issue