Improve error handling for divisions
parent
3f6251f5bb
commit
ad361adab1
|
@ -114,31 +114,34 @@ router.post('/sport', checkLoginStatus.admin, async function(req, res, next) {
|
||||||
router.get('/division', checkLoginStatus.admin, function(req, res, next) {
|
router.get('/division', checkLoginStatus.admin, function(req, res, next) {
|
||||||
let title = req.query.division ? 'Edit Division' : 'Add Division'
|
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) {
|
||||||
const name = req.body['name'];
|
try {
|
||||||
const sport = req.body['sport'];
|
const name = req.body['name'];
|
||||||
const genderName = req.body['gender'];
|
const sport = req.body['sport'];
|
||||||
|
const genderName = req.body['gender'];
|
||||||
const id = req.body['division'];
|
|
||||||
const remove = req.body['remove'];
|
const id = req.body['division'];
|
||||||
|
const remove = req.body['remove'];
|
||||||
|
|
||||||
if(remove) divisions.remove(id).then(res.redirect('/manage'));
|
if(remove) await divisions.remove(id);
|
||||||
else if(id) divisions.rename(id, name).then(res.redirect('/manage'));
|
else if(id) await divisions.rename(id, name);
|
||||||
else {
|
|
||||||
if(genderName == "both") {
|
|
||||||
divisions.add(name, genders.FEMALE, sport)
|
|
||||||
.then(divisions.add(name, genders.MALE, sport)
|
|
||||||
.then(res.redirect("/manage")));
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
const gender = (genderName == "female") ? genders.FEMALE : genders.MALE;
|
if(genderName == 'both') {
|
||||||
divisions.add(name, gender, sport)
|
await divisions.add(name, genders.FEMALE, sport);
|
||||||
.then(res.redirect("/manage"));
|
await divisions.add(name, genders.MALE, sport);
|
||||||
|
} else {
|
||||||
|
const gender = (genderName == "female") ? genders.FEMALE : genders.MALE;
|
||||||
|
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');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ block content
|
||||||
label Division name
|
label Division name
|
||||||
span(class='form-section-input')
|
span(class='form-section-input')
|
||||||
input#name-textbox(type="text", name="name" disabled)
|
input#name-textbox(type="text", name="name" 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