This repository has been archived on 2024-04-05. You can view files and clone it, but cannot push or open issues/pull-requests.
score-tracker/routes/manage.js

159 lines
5.0 KiB
JavaScript
Raw Normal View History

2021-11-22 06:17:43 +00:00
var express = require('express');
var router = express.Router();
var genders = require('../database/scores/genders');
var games = require('../database/scores/games');
2021-11-23 01:04:36 +00:00
var seasons = require('../database/scores/seasons');
2021-11-23 01:47:40 +00:00
var sports = require('../database/scores/sports');
2021-11-23 02:11:16 +00:00
var divisions = require('../database/scores/divisions');
var genders = require('../database/scores/genders');
2021-11-23 02:17:23 +00:00
var teams = require('../database/scores/teams');
2021-11-25 19:40:19 +00:00
var accounts = require('../database/accounts/accounts');
2021-11-25 04:40:33 +00:00
function userLoggedIn(req, res, next) {
if (req.user) {
next();
}
else {
res.redirect('/auth/login');
}
}
function adminLoggedIn(req, res, next) {
if (req.user && req.user[2]) {
next();
}
else {
req.flash('error', 'An admin account is required to access this page.');
res.redirect('/auth/login');
2021-11-25 04:40:33 +00:00
}
}
2021-11-22 06:17:43 +00:00
2021-11-25 04:40:33 +00:00
router.get('/' ,userLoggedIn, function(req, res, next) {
2021-11-22 06:17:43 +00:00
res.render('manage', { title: 'Score Management' });
});
2021-11-25 04:40:33 +00:00
router.get('/game', userLoggedIn, function(req, res, next) {
2021-11-23 07:49:11 +00:00
let title = req.query.game ? 'Edit Game' : 'Submit Score'
res.render('manage/addgame', { title });
});
2021-11-25 04:40:33 +00:00
router.post('/game', userLoggedIn, 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'];
2021-11-23 07:49:11 +00:00
const id = req.body['game'];
const remove = req.body['delete'];
if(remove) games.remove(id)
.then(res.redirect("/manage"));
else if(id) games.edit(id, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score)
2021-11-23 07:49:11 +00:00
.then(res.redirect('/manage'));
else games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score)
.then(res.redirect("/manage"));
});
2021-11-25 04:40:33 +00:00
router.get('/season', adminLoggedIn, function(req, res, next) {
res.render('manage/addseason', { title: 'Add Season', currentYear : (new Date()).getFullYear() });
2021-11-23 01:04:36 +00:00
});
2021-11-25 04:40:33 +00:00
router.post('/season', adminLoggedIn, function(req, res, next) {
2021-11-23 01:04:36 +00:00
const year = req.body['year'];
2021-11-23 21:48:19 +00:00
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"));
2021-11-23 01:04:36 +00:00
});
2021-11-25 04:40:33 +00:00
router.get('/sport', adminLoggedIn, function(req, res, next) {
res.render('manage/addsport', { title: 'Add Sport' });
2021-11-23 01:47:40 +00:00
});
2021-11-25 04:40:33 +00:00
router.post('/sport', adminLoggedIn, function(req, res, next) {
2021-11-23 01:47:40 +00:00
const name = req.body['name'];
const id = req.body['sport'];
2021-11-23 05:10:47 +00:00
const remove = req.body['remove'];
2021-11-23 01:47:40 +00:00
2021-11-23 05:10:47 +00:00
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'));
2021-11-23 01:47:40 +00:00
});
2021-11-25 04:40:33 +00:00
router.get('/division', adminLoggedIn, function(req, res, next) {
2021-11-23 06:23:57 +00:00
let title = req.query.division ? 'Edit Division' : 'Add Division'
res.render('manage/adddivision', { title });
2021-11-23 02:11:16 +00:00
});
2021-11-25 04:40:33 +00:00
router.post('/division', adminLoggedIn, function(req, res, next) {
2021-11-23 02:11:16 +00:00
const name = req.body['name'];
const sport = req.body['sport'];
const genderName = req.body['gender'];
2021-11-23 06:23:57 +00:00
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'));
2021-11-23 02:11:16 +00:00
else {
2021-11-23 06:23:57 +00:00
if(genderName == "both") {
divisions.add(name, genders.FEMALE, sport)
.then(divisions.add(name, genders.MALE, sport)
.then(res.redirect("/manage")));
}
else {
const gender = (genderName == "female") ? genders.FEMALE : genders.MALE;
divisions.add(name, gender, sport)
.then(res.redirect("/manage"));
}
2021-11-23 02:11:16 +00:00
}
});
2021-11-25 04:40:33 +00:00
router.get('/team', adminLoggedIn, function(req, res, next) {
2021-11-23 06:45:24 +00:00
let title = req.query.team ? 'Edit Team' : 'Add Team'
res.render('manage/addteam', { title });
2021-11-23 02:17:23 +00:00
});
2021-11-25 04:40:33 +00:00
router.post('/team', adminLoggedIn, function(req, res, next) {
2021-11-23 02:17:23 +00:00
const name = req.body['name'];
const sport = req.body['sport'];
2021-11-23 06:45:24 +00:00
const id = req.body['team'];
const remove = req.body['remove'];
if(remove) teams.remove(id).then(res.redirect('/manage'));
else if(id) teams.rename(id, name).then(res.redirect('/manage'));
else teams.add(name, sport).then(res.redirect("/manage"));
2021-11-23 02:17:23 +00:00
});
router.get('/account', adminLoggedIn, (req, res, next) => {
let title = req.query.account ? 'Manage User' : 'Create User'
res.render('accounts/createuser', { title });
});
2021-11-25 19:40:19 +00:00
router.post('/account', adminLoggedIn, (req, res, next) => {
const email = req.body.email;
const password = req.body.password;
const isAdmin = !!req.body.admin;
const accountID = req.body.account;
if(accountID) accounts.edit(accountID, email, password, isAdmin).then(res.redirect('/manage'));
else accounts.create(req.body.email, req.body.password, !!req.body.admin).then(res.redirect('/manage'));
});
2021-11-22 06:17:43 +00:00
module.exports = router;