From 9db8ae51515f9c82fff957bb713217cbc192952e Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 23:16:29 -0700 Subject: [PATCH 01/41] remove require database from index.js --- routes/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/routes/index.js b/routes/index.js index 4cb7f20..3c536d2 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,6 +1,5 @@ var express = require('express'); var router = express.Router(); -var database = require('../database/database'); /* GET home page. */ router.get('/', function(req, res, next) { From dca907964843f2137e1f94e8e067af70eb48cca4 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 23:17:43 -0700 Subject: [PATCH 02/41] Create files for /manage page --- routes/manage.js | 9 +++++++++ views/manage.pug | 0 2 files changed, 9 insertions(+) create mode 100644 routes/manage.js create mode 100644 views/manage.pug diff --git a/routes/manage.js b/routes/manage.js new file mode 100644 index 0000000..796064b --- /dev/null +++ b/routes/manage.js @@ -0,0 +1,9 @@ +var express = require('express'); +var router = express.Router(); + +/* GET manage page. */ +router.get('/', function(req, res, next) { + res.render('manage', { title: 'Score Management' }); +}); + +module.exports = router; diff --git a/views/manage.pug b/views/manage.pug new file mode 100644 index 0000000..e69de29 From 41d9140863c5ea08abf7b88af456ecd6cab47914 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 23:23:06 -0700 Subject: [PATCH 03/41] Remove console.log used for debugging --- database/scores/games.js | 1 - 1 file changed, 1 deletion(-) diff --git a/database/scores/games.js b/database/scores/games.js index 9af0b89..265dd56 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -52,7 +52,6 @@ async function retrieveByTeamDivisionAndSeason(teamID, divisionID, seasonID) { gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), teamID, opponentID, teamScore, opponentScore)); }); - console.log(gamesList); return gamesList; } From f7dfd24a68d96bef2d3ad71ce9b75dbacf9b1474 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 13:44:19 -0700 Subject: [PATCH 04/41] Remove unnecessary div from index.pug --- views/index.pug | 1 - 1 file changed, 1 deletion(-) diff --git a/views/index.pug b/views/index.pug index 1d3ffb2..2e52919 100644 --- a/views/index.pug +++ b/views/index.pug @@ -22,7 +22,6 @@ block content label Team span(class='form-section-input') select#team-dropdown(name="team" class="form-main-dropdown") - span(class='form-section') div h2#games-table-header span#no-scores-message From a6e8e924e4459858eb2de01a9e5c7e0b6c7861b1 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 13:56:57 -0700 Subject: [PATCH 05/41] Create basic layout for manage page --- views/manage.pug | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/views/manage.pug b/views/manage.pug index e69de29..79ac0b0 100644 --- a/views/manage.pug +++ b/views/manage.pug @@ -0,0 +1,31 @@ +extends layout + +block stylesheets + link(rel='stylesheet', href='/stylesheets/index.css') + link(rel='stylesheet', href='/stylesheets/form.css') + +block content + div#mobile-view + h1 Management Panel + div + span(class='form-section') + label Category + span(class='form-section-input') + select#category-dropdown(name="category" class="form-main-dropdown") + option(value="seasons") Seasons + option(value="sports") Sports + option(value="divisions") Divisions + option(value="teams") Teams + option(value="games") Games + div + h2#table-header + table + colgroup + col#score-column(span="1") + col#opponent-column(span="1") + col#date-column(span="1") + tbody#games-table + + +block scripts + script(src='/scripts/index.js' type="module") \ No newline at end of file From 7baa986a30093360ff704ddacf51fcdc1582a759 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 13:57:13 -0700 Subject: [PATCH 06/41] Add manage route to app.js --- app.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app.js b/app.js index 29f7b4f..6bc76de 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,7 @@ var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); var submitRouter = require('./routes/submit'); var dataRouter = require('./routes/data'); +var manageRouter = require('./routes/manage'); var app = express(); @@ -25,6 +26,8 @@ app.use('/', indexRouter); app.use('/users', usersRouter); app.use('/submit', submitRouter); app.use('/data', dataRouter); +app.use('/manage', manageRouter); + // catch 404 and forward to error handler app.use(function(req, res, next) { From 77359ba3a7f99d1a58369a69e0619660dbe0960b Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 14:07:06 -0700 Subject: [PATCH 07/41] Add formatting tweak to index.js --- public/scripts/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/index.js b/public/scripts/index.js index 4a4721c..41d9610 100644 --- a/public/scripts/index.js +++ b/public/scripts/index.js @@ -20,7 +20,7 @@ async function listSeasons() { seasonsList.forEach(season => { const option = document.createElement('option'); - option.text = season.year - 1 + "-" + season.year; + option.text = (season.year - 1) + "-" + season.year; option.value = season.id; seasonDropdown.appendChild(option); }); From 1ce1bdb27c4edc2921eeee241a138d12c3761158 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 14:07:51 -0700 Subject: [PATCH 08/41] Add formatting tweak to submit.js --- public/scripts/submit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/submit.js b/public/scripts/submit.js index bd4d08a..8c3cf81 100644 --- a/public/scripts/submit.js +++ b/public/scripts/submit.js @@ -19,7 +19,7 @@ async function listSeasons() { seasonsList.forEach(season => { const option = document.createElement('option'); - option.text = season.year - 1 + "-" + season.year; + option.text = (season.year - 1) + "-" + season.year; option.value = season.id; seasonDropdown.appendChild(option); }); From 4aafccc5505fdd4d475854cf29a1cd8caf33b0e8 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 14:50:32 -0700 Subject: [PATCH 09/41] Add functions to list seasons and sports in manage page --- public/scripts/manage.js | 116 +++++++++++++++++++++++++++++++++++++++ views/manage.pug | 13 ++--- 2 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 public/scripts/manage.js diff --git a/public/scripts/manage.js b/public/scripts/manage.js new file mode 100644 index 0000000..19b700a --- /dev/null +++ b/public/scripts/manage.js @@ -0,0 +1,116 @@ +import * as Data from "./data.js"; + +const categoryDropdown = document.getElementById('category-dropdown'); +const itemsListTable = document.getElementById('items-list'); + +class Category { + constructor(name, getItems, listHeaders, listItem, addItem, submitItem, editItem) { + this.name = name; + this.getItems = getItems; + this.listHeaders = listHeaders; + this.listItem = listItem; + this.addItem = addItem; + this.submitItem = submitItem; + this.editItem = editItem; + } +} + +const CATEGORIES = []; + +CATEGORIES.push(new Category( + "seasons", + async function getSeasons() { + return await Data.getSeasons(); + }, + async function listSeasonHeaders() { + const headerRow = document.createElement('tr'); + + const yearsHeader = document.createElement('th'); + yearsHeader.textContent = "Years"; + + headerRow.appendChild(yearsHeader); + + itemsListTable.appendChild(headerRow); + }, + function listSeason(season, row) { + const yearCell = document.createElement('td'); + yearCell.textContent = (season.year - 1) + "-" + season.year; + row.appendChild(yearCell); + }, + async function addSeason() { + // + }, + async function submitSeason() { + // + }, + async function editSeason() { + + } +)); + +CATEGORIES.push(new Category( + "sports", + async function getSports() { + return await Data.getSports(); + }, + async function listSportHeaders() { + const headerRow = document.createElement('tr'); + + const yearsHeader = document.createElement('th'); + yearsHeader.textContent = "Name"; + headerRow.appendChild(yearsHeader); + + itemsListTable.appendChild(headerRow); + }, + function listSport(sport, row) { + const nameCell = document.createElement('td'); + nameCell.textContent = sport.name; + row.appendChild(nameCell); + }, + async function addSeason() { + // + }, + async function submitSeason() { + // + }, + async function editSeason() { + + } +)); + + + +async function listItems(category) { + itemsListTable.innerHTML = ""; + + const itemsList = await category.getItems(); + + await category.listHeaders(); + + itemsList.forEach(item => { + const row = document.createElement('tr'); + + category.listItem(item, row); + + const editCell = document.createElement('td'); + const editButton = document.createElement('button'); + editButton.textContent = "edit"; + editCell.appendChild(editButton); + row.appendChild(editCell); + + const removeCell = document.createElement('td'); + const removeButton = document.createElement('button'); + removeButton.textContent = "remove"; + removeCell.appendChild(removeButton); + row.appendChild(removeCell); + + itemsListTable.appendChild(row); + }); +} +listItems(CATEGORIES[0]); + + + +categoryDropdown.onchange = () => { + listItems(CATEGORIES[categoryDropdown.selectedIndex]); +}; \ No newline at end of file diff --git a/views/manage.pug b/views/manage.pug index 79ac0b0..7977824 100644 --- a/views/manage.pug +++ b/views/manage.pug @@ -1,7 +1,7 @@ extends layout block stylesheets - link(rel='stylesheet', href='/stylesheets/index.css') + link(rel='stylesheet', href='/stylesheets/manage.css') link(rel='stylesheet', href='/stylesheets/form.css') block content @@ -19,13 +19,8 @@ block content option(value="games") Games div h2#table-header - table - colgroup - col#score-column(span="1") - col#opponent-column(span="1") - col#date-column(span="1") - tbody#games-table - + table#items-list + button Add new... block scripts - script(src='/scripts/index.js' type="module") \ No newline at end of file + script(src='/scripts/manage.js' type="module") \ No newline at end of file From 75594a81a6497272941e897485797a9b9728ff74 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 14:58:43 -0700 Subject: [PATCH 10/41] Add option to fetch all divisions --- public/scripts/data.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/public/scripts/data.js b/public/scripts/data.js index 61f3923..502bbb8 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -16,8 +16,12 @@ export async function getGenders(sportID) { return gendersList; } -export async function getDivisions(sportID, gender) { - const response = await fetch(`/data/divisions?sport=${+sportID}&gender=${gender}`); +export async function getDivisions(sportID = undefined, gender = undefined) { + let URL = '/data/divisions?'; + if(sportID) URL += `sport=${+sportID}&`; + if(gender) URL += `gender=${gender}&`; + + const response = await fetch(URL); const divisionsList = await response.json(); return divisionsList; } From 435748ae7663ea3af7219d76396ccfce24a22c4f Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 15:20:59 -0700 Subject: [PATCH 11/41] Add functionality to return all divisions in database --- database/scores/divisions.js | 30 ++++++++++++++++++++++-------- routes/data.js | 5 +++-- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/database/scores/divisions.js b/database/scores/divisions.js index 38fa8be..80fe35e 100644 --- a/database/scores/divisions.js +++ b/database/scores/divisions.js @@ -6,9 +6,11 @@ const genders = require('./genders'); class Division { - constructor(id, name) { + constructor(id, name, gender, sportID) { this.id = id; this.name = name; + this.gender = gender; + this.sportID = sportID; } } @@ -29,7 +31,7 @@ async function add(name, gender, sportID) { return new Division(id, name); } -async function rename(id, division) { +async function rename(id, name) { const query = `UPDATE scores.divisions SET division_name = $2 WHERE division_id = $1;`; @@ -45,17 +47,29 @@ async function remove(id) { return new Division(id, name); } -async function retrieveBySportAndGender(sportID, gender) { - const query = `SELECT * +async function retrieve(sportID = undefined, gender = undefined) { + let table; + + if(sportID && gender) { + const query = `SELECT division_id, division_name, gender, sport_id FROM scores.divisions WHERE sport_id = $1 AND gender = $2 ORDER BY division_name;`; - const genderID = getGenderID(gender); - const table = await database.executeQuery(query, [sportID, genderID]); + const genderID = getGenderID(gender); + table = await database.executeQuery(query, [sportID, genderID]); + } + else { + const query = `SELECT division_id, division_name, gender, sport_id + FROM scores.divisions + ORDER BY division_name, + gender;`; + table = await database.executeQuery(query); + } const divisionsList = []; table.forEach((row) => { - divisionsList.push(new Division(row[0], row[1])); + let gender = (row[2] == "F") ? genders.FEMALE : genders.MALE; + divisionsList.push(new Division(row[0], row[1], gender, row[3])); }); return divisionsList; } @@ -67,4 +81,4 @@ async function retrieveBySportAndGender(sportID, gender) { exports.add = add; exports.rename = rename; exports.remove = remove; -exports.retrieveBySportAndGender = retrieveBySportAndGender; \ No newline at end of file +exports.retrieve = retrieve; \ No newline at end of file diff --git a/routes/data.js b/routes/data.js index 4344d2d..588ed2f 100644 --- a/routes/data.js +++ b/routes/data.js @@ -24,9 +24,10 @@ router.get('/genders', function(req, res, next) { }) router.get('/divisions', function(req, res, next) { - const gender = req.query.gender == 'female' ? genders.FEMALE : genders.MALE; + let gender; + if(req.query.gender) gender = (req.query.gender == 'female' ? genders.FEMALE : genders.MALE); - divisions.retrieveBySportAndGender(req.query.sport, gender) + divisions.retrieve(req.query.sport, gender) .then(data => res.json(data)); }) From b6f8b6bdaa78a57bdbba80ea08e0d0be3029a457 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 15:30:02 -0700 Subject: [PATCH 12/41] Add functions to retrieve sport by ID --- database/scores/sports.js | 11 ++++++++++- public/scripts/data.js | 10 ++++++++-- routes/data.js | 5 +++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/database/scores/sports.js b/database/scores/sports.js index 14a6e45..134009d 100644 --- a/database/scores/sports.js +++ b/database/scores/sports.js @@ -48,6 +48,14 @@ async function retrieveAll() { return sportsList; } +async function getFromID(id) { + const query = `SELECT sport_name + FROM scores.sports + WHERE sport_id = $1;`; + const name = (await database.executeQuery(query, [id]))[0][0]; + return new Sport(id, name); +} + @@ -55,4 +63,5 @@ async function retrieveAll() { exports.add = add; exports.rename = rename; exports.remove = remove; -exports.retrieveAll = retrieveAll; \ No newline at end of file +exports.retrieveAll = retrieveAll; +exports.getFromID = getFromID; \ No newline at end of file diff --git a/public/scripts/data.js b/public/scripts/data.js index 502bbb8..567c1ac 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -1,11 +1,17 @@ export async function getSports() { - const response = await fetch('/data/sports'); + const response = await fetch(`/data/sports`); const sportsList = await response.json(); return sportsList; } +export async function getSportName(sportID) { + const response = await fetch(`/data/sport?sport=${sportID}`); + const sport = await response.json(); + return sport.name; +} + export async function getSeasons() { - const response = await fetch('/data/seasons'); + const response = await fetch(`/data/seasons`); const seasonsList = await response.json(); return seasonsList; } diff --git a/routes/data.js b/routes/data.js index 588ed2f..fc61052 100644 --- a/routes/data.js +++ b/routes/data.js @@ -13,6 +13,11 @@ router.get('/sports', function(req, res, next) { .then(data => res.json(data)); }); +router.get('/sport', function(req, res, next) { + sports.getFromID(req.query.sport) + .then(data => res.json(data)); +}); + router.get('/seasons', function(req, res, next) { seasons.retrieveAll() .then(data => res.json(data)); From 18f840183fad1627fe59c2d1b621505c3b27c93a Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 15:32:44 -0700 Subject: [PATCH 13/41] Add functionality to list divisions in management page --- public/scripts/manage.js | 53 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 19b700a..8958f1c 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -56,9 +56,9 @@ CATEGORIES.push(new Category( async function listSportHeaders() { const headerRow = document.createElement('tr'); - const yearsHeader = document.createElement('th'); - yearsHeader.textContent = "Name"; - headerRow.appendChild(yearsHeader); + const nameHeader = document.createElement('th'); + nameHeader.textContent = "Name"; + headerRow.appendChild(nameHeader); itemsListTable.appendChild(headerRow); }, @@ -67,6 +67,53 @@ CATEGORIES.push(new Category( nameCell.textContent = sport.name; row.appendChild(nameCell); }, + async function addSport() { + // + }, + async function submitSport() { + // + }, + async function editSport() { + + } +)); + +CATEGORIES.push(new Category( + "divisions", + async function getDivisions() { + return await Data.getDivisions(); + }, + async function listDivisionHeaders() { + const headerRow = document.createElement('tr'); + + const nameHeader = document.createElement('th'); + nameHeader.textContent = "Name"; + headerRow.appendChild(nameHeader); + + const genderHeader = document.createElement('th'); + headerRow.appendChild(genderHeader); + + const sportHeader = document.createElement('th'); + sportHeader.textContent = "Sport"; + headerRow.appendChild(sportHeader); + + itemsListTable.appendChild(headerRow); + }, + function listDivision(division, row) { + const nameCell = document.createElement('td'); + nameCell.textContent = division.name; + row.appendChild(nameCell); + + const genderCell = document.createElement('td'); + const gender = division.gender.name == "female" ? "F" : "M"; + genderCell.textContent = gender; + row.appendChild(genderCell); + + const sportCell = document.createElement('td'); + Data.getSportName(division.sportID) + .then(data => sportCell.textContent = data); + row.appendChild(sportCell); + }, async function addSeason() { // }, From 28b11b7266689063c9b112fa284a4c661119b3b6 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 15:38:20 -0700 Subject: [PATCH 14/41] Group divisions by sport_id --- database/scores/divisions.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/database/scores/divisions.js b/database/scores/divisions.js index 80fe35e..a3cebd2 100644 --- a/database/scores/divisions.js +++ b/database/scores/divisions.js @@ -61,7 +61,8 @@ async function retrieve(sportID = undefined, gender = undefined) { else { const query = `SELECT division_id, division_name, gender, sport_id FROM scores.divisions - ORDER BY division_name, + ORDER BY sport_id, + division_name, gender;`; table = await database.executeQuery(query); } From 90ef696b717ec05e92ffd55b840b377ec44e2488 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 15:45:19 -0700 Subject: [PATCH 15/41] Add retrieve all teams in database functionality --- database/scores/teams.js | 25 +++++++++++++++++++------ public/scripts/data.js | 7 +++++-- routes/data.js | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/database/scores/teams.js b/database/scores/teams.js index 893cc34..3717841 100644 --- a/database/scores/teams.js +++ b/database/scores/teams.js @@ -5,9 +5,10 @@ const database = require('./../database'); class Team { - constructor(id, name) { + constructor(id, name, sportID) { this.id = id; this.name = name; + this.sportID = sportID; } } @@ -37,16 +38,28 @@ async function remove(id) { return new Team(id, name); } -async function retrieveBySport(sportID) { - const query = `SELECT * +async function retrieve(sportID = undefined) { + let table; + + if(sportID) { + const query = `SELECT team_id, team_name, sport_id FROM scores.teams WHERE sport_id = $1 ORDER BY team_name;`; - const table = await database.executeQuery(query, [sportID]); + table = await database.executeQuery(query, [sportID]); + } + else { + const query = `SELECT team_id, team_name, sport_id + FROM scores.teams + ORDER BY + sport_id, + team_name;`; + table = await database.executeQuery(query); + } const teamsList = []; table.forEach((row) => { - teamsList.push(new Team(row[0], row[1])); + teamsList.push(new Team(row[0], row[1], row[2])); }); return teamsList; } @@ -66,5 +79,5 @@ async function getFromID(id) { exports.add = add; exports.rename = rename; exports.remove = remove; -exports.retrieveBySport = retrieveBySport; +exports.retrieve = retrieve; exports.getFromID = getFromID; \ No newline at end of file diff --git a/public/scripts/data.js b/public/scripts/data.js index 567c1ac..b83fdd0 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -32,8 +32,11 @@ export async function getDivisions(sportID = undefined, gender = undefined) { return divisionsList; } -export async function getTeams(sportID) { - const response = await fetch(`/data/teams?sport=${+sportID}`); +export async function getTeams(sportID = undefined) { + let URL = '/data/teams?'; + if(sportID) URL += `sport=${+sportID}&`; + + const response = await fetch(URL); const teamsList = await response.json(); return teamsList; } diff --git a/routes/data.js b/routes/data.js index fc61052..f9c6431 100644 --- a/routes/data.js +++ b/routes/data.js @@ -37,7 +37,7 @@ router.get('/divisions', function(req, res, next) { }) router.get('/teams', function(req, res, next) { - teams.retrieveBySport(req.query.sport) + teams.retrieve(req.query.sport) .then(data => res.json(data)); }) From 6fdbaedc791db514e5f61ab9318690c952fd0976 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 15:48:30 -0700 Subject: [PATCH 16/41] Add functionality to list teams on manage page --- public/scripts/manage.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 8958f1c..f6bcda6 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -125,6 +125,45 @@ CATEGORIES.push(new Category( } )); +CATEGORIES.push(new Category( + "teams", + async function getTeams() { + return await Data.getTeams(); + }, + async function listTeamHeaders() { + const headerRow = document.createElement('tr'); + + const nameHeader = document.createElement('th'); + nameHeader.textContent = "Name"; + headerRow.appendChild(nameHeader); + + const sportHeader = document.createElement('th'); + sportHeader.textContent = "Sport"; + headerRow.appendChild(sportHeader); + + itemsListTable.appendChild(headerRow); + }, + function listTeam(team, row) { + const nameCell = document.createElement('td'); + nameCell.textContent = team.name; + row.appendChild(nameCell); + + const sportCell = document.createElement('td'); + Data.getSportName(team.sportID) + .then(data => sportCell.textContent = data); + row.appendChild(sportCell); + }, + async function addSeason() { + // + }, + async function submitSeason() { + // + }, + async function editSeason() { + + } +)); + async function listItems(category) { From 17f14573e94647befee47ca1190902f46103b484 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 16:00:12 -0700 Subject: [PATCH 17/41] Add functionality to get all games from database --- database/scores/games.js | 25 +++++++++++++++++++------ public/scripts/data.js | 10 ++++++++-- routes/data.js | 2 +- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/database/scores/games.js b/database/scores/games.js index 265dd56..9f27bab 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -5,13 +5,15 @@ const database = require('./../database'); class Game { - constructor(id, date, team1ID, team2ID, team1Score, team2Score) { + constructor(id, date, team1ID, team2ID, team1Score, team2Score, divisionID, seasonID) { this.id = id; this.date = date; this.team1ID = team1ID; this.team2ID = team2ID; this.team1Score = team1Score; this.team2Score = team2Score; + this.divisionID = divisionID; + this.seasonID = seasonID; } } @@ -36,12 +38,23 @@ async function remove(id) { return new Game(id, row[3], row[4], row[5], row[6], row[7]); } -async function retrieveByTeamDivisionAndSeason(teamID, divisionID, seasonID) { - const query = `SELECT * +async function retrieve(teamID, divisionID, seasonID) { + let table; + + if(teamID && divisionID && seasonID) { + const query = `SELECT game_id, division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score FROM scores.games WHERE (team1_id = $1 OR team2_id = $1) AND division_id = $2 AND season_id = $3 ORDER BY game_date DESC;`; - const table = (await database.executeQuery(query, [teamID,divisionID,seasonID])); + table = await database.executeQuery(query, [teamID,divisionID,seasonID]); + } + else { + const query = `SELECT game_id, division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score + FROM scores.games + ORDER BY game_date DESC;`; + table = await database.executeQuery(query); + } + const gamesList = []; table.forEach((row) => { @@ -50,7 +63,7 @@ async function retrieveByTeamDivisionAndSeason(teamID, divisionID, seasonID) { teamScore = opponentIsTeam2 ? row[6] : row[7]; opponentScore = opponentIsTeam2 ? row[7] : row[6]; - gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), teamID, opponentID, teamScore, opponentScore)); + gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), teamID, opponentID, teamScore, opponentScore, row[1], row[2])); }); return gamesList; } @@ -61,4 +74,4 @@ async function retrieveByTeamDivisionAndSeason(teamID, divisionID, seasonID) { exports.add = add; exports.remove = remove; -exports.retrieveByTeamDivisionAndSeason = retrieveByTeamDivisionAndSeason; \ No newline at end of file +exports.retrieve = retrieve; \ No newline at end of file diff --git a/public/scripts/data.js b/public/scripts/data.js index b83fdd0..9866321 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -47,8 +47,14 @@ export async function getTeamName(teamID) { return team.name; } -export async function getGames(teamID, divisionID, seasonID) { - const response = await fetch(`/data/games?team=${+teamID}&division=${+divisionID}&season=${+seasonID}`); +export async function getGames(teamID = undefined, divisionID = undefined, seasonID = undefined) { + let URL = '/data/games?'; + if(teamID) URL += `team=${+teamID}&`; + if(divisionID) URL += `division=${+divisionID}&`; + if(seasonID) URL += `season=${+seasonID}`; + + + const response = await fetch(URL); const gamesList = await response.json(); return gamesList; } \ No newline at end of file diff --git a/routes/data.js b/routes/data.js index f9c6431..76dd2ac 100644 --- a/routes/data.js +++ b/routes/data.js @@ -47,7 +47,7 @@ router.get('/team', function(req, res, next) { }) router.get('/games', function(req, res, next) { - games.retrieveByTeamDivisionAndSeason(req.query.team, req.query.division, req.query.season) + games.retrieve(req.query.team, req.query.division, req.query.season) .then(data => res.json(data)); }) From fe659184f41838ec53313d7d9b9ef84eee3f426b Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 16:13:21 -0700 Subject: [PATCH 18/41] Fix bug in games.js --- database/scores/games.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/database/scores/games.js b/database/scores/games.js index 9f27bab..4055523 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -58,12 +58,17 @@ async function retrieve(teamID, divisionID, seasonID) { const gamesList = []; table.forEach((row) => { - opponentIsTeam2 = teamID != row[5]; - opponentID = opponentIsTeam2 ? row[5] : row[4]; - teamScore = opponentIsTeam2 ? row[6] : row[7]; - opponentScore = opponentIsTeam2 ? row[7] : row[6]; - - gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), teamID, opponentID, teamScore, opponentScore, row[1], row[2])); + if(teamID) { + const opponentIsTeam2 = teamID != row[5]; + const opponentID = opponentIsTeam2 ? row[5] : row[4]; + const teamScore = opponentIsTeam2 ? row[6] : row[7]; + const opponentScore = opponentIsTeam2 ? row[7] : row[6]; + + gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), teamID, opponentID, teamScore, opponentScore, row[1], row[2])); + } + else { + gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), row[4], row[5], row[6], row[7], row[1], row[2])); + } }); return gamesList; } From 1f55ba3ff9921942a17cf2d5f120e206ce6666ab Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 16:27:50 -0700 Subject: [PATCH 19/41] Add functions to get division by ID --- database/scores/divisions.js | 20 +++++++++-- public/scripts/data.js | 6 ++++ public/scripts/manage.js | 68 ++++++++++++++++++++++++++++++++++++ routes/data.js | 5 +++ 4 files changed, 96 insertions(+), 3 deletions(-) diff --git a/database/scores/divisions.js b/database/scores/divisions.js index a3cebd2..7da86dd 100644 --- a/database/scores/divisions.js +++ b/database/scores/divisions.js @@ -20,6 +20,10 @@ function getGenderID(gender) { return (gender == genders.MALE) ? "M" : "F"; } +function getGenderFromID(genderID) { + return (genderID == "F") ? genders.FEMALE : genders.MALE; +} + async function add(name, gender, sportID) { @@ -69,12 +73,21 @@ async function retrieve(sportID = undefined, gender = undefined) { const divisionsList = []; table.forEach((row) => { - let gender = (row[2] == "F") ? genders.FEMALE : genders.MALE; - divisionsList.push(new Division(row[0], row[1], gender, row[3])); + divisionsList.push(new Division(row[0], row[1], getGenderFromID(row[2]), row[3])); }); return divisionsList; } +async function getFromID(id) { + const query = `SELECT division_id, division_name, gender, sport_id + FROM scores.divisions + WHERE division_id = $1;`; + const row = (await database.executeQuery(query, [id]))[0]; + + + return new Division(id, row[1], getGenderFromID(row[2]), row[3]); +} + @@ -82,4 +95,5 @@ async function retrieve(sportID = undefined, gender = undefined) { exports.add = add; exports.rename = rename; exports.remove = remove; -exports.retrieve = retrieve; \ No newline at end of file +exports.retrieve = retrieve; +exports.getFromID = getFromID; \ No newline at end of file diff --git a/public/scripts/data.js b/public/scripts/data.js index 9866321..8ef7479 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -32,6 +32,12 @@ export async function getDivisions(sportID = undefined, gender = undefined) { return divisionsList; } +export async function getDivision(divisionID) { + const response = await fetch(`/data/division?division=${divisionID}`); + const division = await response.json(); + return division; +} + export async function getTeams(sportID = undefined) { let URL = '/data/teams?'; if(sportID) URL += `sport=${+sportID}&`; diff --git a/public/scripts/manage.js b/public/scripts/manage.js index f6bcda6..83920f0 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -164,6 +164,74 @@ CATEGORIES.push(new Category( } )); +CATEGORIES.push(new Category( + "games", + async function getGames() { + return await Data.getGames(); + }, + async function listGameHeaders() { + const headerRow = document.createElement('tr'); + + const teamsHeader = document.createElement('th'); + teamsHeader.textContent = "Teams"; + headerRow.appendChild(teamsHeader); + + const scoreHeader = document.createElement('th'); + headerRow.appendChild(scoreHeader); + + const sportNameHeader = document.createElement('th'); + sportNameHeader.textContent = "Sport"; + headerRow.appendChild(sportNameHeader); + + const divisionNameHeader = document.createElement('th'); + headerRow.appendChild(divisionNameHeader); + + const divisionGenderHeader = document.createElement('th'); + headerRow.appendChild(divisionGenderHeader); + + const dateHeader = document.createElement('th'); + dateHeader.textContent = "Date"; + headerRow.appendChild(dateHeader); + + itemsListTable.appendChild(headerRow); + }, + function listGame(game, row) { + const teamsCell = document.createElement('td'); + const team1NameSpan = document.createElement('span'); + Data.getTeamName(game.team1ID) + .then(data => team1NameSpan.textContent = data); + teamsCell.appendChild(team1NameSpan); + const team2NameSpan = document.createElement('span'); + Data.getTeamName(game.team2ID) + .then(data => team2NameSpan.textContent = data); + teamsCell.appendChild(team2NameSpan); + row.appendChild(teamsCell); + + const scoresCell = document.createElement('td'); + const team1ScoreSpan = document.createElement('span'); + team1ScoreSpan.textContent = game.team1Score; + scoresCell.appendChild(team1ScoreSpan); + const team2ScoreSpan = document.createElement('span'); + team2ScoreSpan.textContent = game.team2Score; + scoresCell.appendChild(team2ScoreSpan); + row.appendChild(scoresCell); + + const sportCell = document.createElement('td'); + Data.getSportName(team.sportID) + .then(data => sportCell.textContent = data); + row.appendChild(sportCell); + }, + async function addSeason() { + // + }, + async function submitSeason() { + // + }, + async function editSeason() { + + } +)); + async function listItems(category) { diff --git a/routes/data.js b/routes/data.js index 76dd2ac..917b36b 100644 --- a/routes/data.js +++ b/routes/data.js @@ -36,6 +36,11 @@ router.get('/divisions', function(req, res, next) { .then(data => res.json(data)); }) +router.get('/division', function(req, res, next) { + divisions.getFromID(req.query.division) + .then(data => res.json(data)); +}) + router.get('/teams', function(req, res, next) { teams.retrieve(req.query.sport) .then(data => res.json(data)); From 54bef983121f6e467a7eaf0198da0338a873a5b7 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 16:58:36 -0700 Subject: [PATCH 20/41] Add games list to manage page --- public/scripts/manage.js | 64 ++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 83920f0..b609171 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -3,6 +3,11 @@ import * as Data from "./data.js"; const categoryDropdown = document.getElementById('category-dropdown'); const itemsListTable = document.getElementById('items-list'); + +function getGenderLetter(genderName) { + return genderName == "female" ? "F" : "M"; +} + class Category { constructor(name, getItems, listHeaders, listItem, addItem, submitItem, editItem) { this.name = name; @@ -105,7 +110,7 @@ CATEGORIES.push(new Category( row.appendChild(nameCell); const genderCell = document.createElement('td'); - const gender = division.gender.name == "female" ? "F" : "M"; + const gender = getGenderLetter(division.gender.name); genderCell.textContent = gender; row.appendChild(genderCell); @@ -183,12 +188,6 @@ CATEGORIES.push(new Category( sportNameHeader.textContent = "Sport"; headerRow.appendChild(sportNameHeader); - const divisionNameHeader = document.createElement('th'); - headerRow.appendChild(divisionNameHeader); - - const divisionGenderHeader = document.createElement('th'); - headerRow.appendChild(divisionGenderHeader); - const dateHeader = document.createElement('th'); dateHeader.textContent = "Date"; headerRow.appendChild(dateHeader); @@ -217,9 +216,32 @@ CATEGORIES.push(new Category( row.appendChild(scoresCell); const sportCell = document.createElement('td'); - Data.getSportName(team.sportID) - .then(data => sportCell.textContent = data); + const sportSpan = document.createElement('span'); + const divisionSpan = document.createElement('span'); + divisionSpan.classList.add('division-span'); + const divisionNameSpan = document.createElement('span'); + const divisionGenderSpan = document.createElement('span'); + divisionSpan.appendChild(divisionNameSpan); + divisionSpan.appendChild(divisionGenderSpan); + Data.getDivision(game.divisionID) + .then(data => { + Data.getSportName(data.sportID) + .then(data => sportSpan.textContent = data); + divisionNameSpan.textContent = data.name; + divisionGenderSpan.textContent = getGenderLetter(data.gender.name); + }); + sportCell.appendChild(sportSpan); + sportCell.appendChild(divisionSpan); row.appendChild(sportCell); + + const dateCell = document.createElement('td'); + const yearSpan = document.createElement('span'); + yearSpan.textContent = game.date.slice(0,4); + dateCell.appendChild(yearSpan); + const dateSpan = document.createElement('span'); + dateSpan.textContent = game.date.slice(5); + dateCell.appendChild(dateSpan); + row.appendChild(dateCell); }, async function addSeason() { // @@ -246,22 +268,26 @@ async function listItems(category) { category.listItem(item, row); - const editCell = document.createElement('td'); - const editButton = document.createElement('button'); - editButton.textContent = "edit"; - editCell.appendChild(editButton); - row.appendChild(editCell); + const manageCell = document.createElement('td'); - const removeCell = document.createElement('td'); + const editSpan = document.createElement('span'); + const editButton = document.createElement('button'); + editButton.textContent = "E"; + editSpan.appendChild(editButton); + manageCell.appendChild(editSpan); + + const removeSpan = document.createElement('remove'); const removeButton = document.createElement('button'); - removeButton.textContent = "remove"; - removeCell.appendChild(removeButton); - row.appendChild(removeCell); + removeButton.textContent = "D"; + removeSpan.appendChild(removeButton); + manageCell.appendChild(removeSpan); + + row.appendChild(manageCell); itemsListTable.appendChild(row); }); } -listItems(CATEGORIES[0]); +listItems(CATEGORIES[categoryDropdown.selectedIndex]); From 4d5669dab79a3b8b1be0320f762a0e93d978c0a0 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 17:13:29 -0700 Subject: [PATCH 21/41] Tweak table layout --- public/scripts/manage.js | 37 ++++++++++++++++++++++++++++++++++- public/stylesheets/manage.css | 15 ++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 public/stylesheets/manage.css diff --git a/public/scripts/manage.js b/public/scripts/manage.js index b609171..80bc534 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -35,12 +35,19 @@ CATEGORIES.push(new Category( headerRow.appendChild(yearsHeader); + const spacerHeader = document.createElement('th'); + spacerHeader.classList.add('spacer-column'); + headerRow.appendChild(spacerHeader); + itemsListTable.appendChild(headerRow); }, function listSeason(season, row) { const yearCell = document.createElement('td'); yearCell.textContent = (season.year - 1) + "-" + season.year; row.appendChild(yearCell); + + const spacerCell = document.createElement('td'); + row.appendChild(spacerCell); }, async function addSeason() { // @@ -65,12 +72,19 @@ CATEGORIES.push(new Category( nameHeader.textContent = "Name"; headerRow.appendChild(nameHeader); + const spacerHeader = document.createElement('th'); + spacerHeader.classList.add('spacer-column'); + headerRow.appendChild(spacerHeader); + itemsListTable.appendChild(headerRow); }, function listSport(sport, row) { const nameCell = document.createElement('td'); nameCell.textContent = sport.name; row.appendChild(nameCell); + + const spacerCell = document.createElement('td'); + row.appendChild(spacerCell); }, async function addSport() { // @@ -97,6 +111,10 @@ CATEGORIES.push(new Category( const genderHeader = document.createElement('th'); headerRow.appendChild(genderHeader); + + const spacerHeader = document.createElement('th'); + spacerHeader.classList.add('spacer-column'); + headerRow.appendChild(spacerHeader); const sportHeader = document.createElement('th'); sportHeader.textContent = "Sport"; @@ -114,6 +132,9 @@ CATEGORIES.push(new Category( genderCell.textContent = gender; row.appendChild(genderCell); + const spacerCell = document.createElement('td'); + row.appendChild(spacerCell); + const sportCell = document.createElement('td'); Data.getSportName(division.sportID) .then(data => sportCell.textContent = data); @@ -141,6 +162,10 @@ CATEGORIES.push(new Category( const nameHeader = document.createElement('th'); nameHeader.textContent = "Name"; headerRow.appendChild(nameHeader); + + const spacerHeader = document.createElement('th'); + spacerHeader.classList.add('spacer-column'); + headerRow.appendChild(spacerHeader); const sportHeader = document.createElement('th'); sportHeader.textContent = "Sport"; @@ -153,6 +178,9 @@ CATEGORIES.push(new Category( nameCell.textContent = team.name; row.appendChild(nameCell); + const spacerCell = document.createElement('td'); + row.appendChild(spacerCell); + const sportCell = document.createElement('td'); Data.getSportName(team.sportID) .then(data => sportCell.textContent = data); @@ -184,6 +212,10 @@ CATEGORIES.push(new Category( const scoreHeader = document.createElement('th'); headerRow.appendChild(scoreHeader); + const spacerHeader = document.createElement('th'); + spacerHeader.classList.add('spacer-column'); + headerRow.appendChild(spacerHeader); + const sportNameHeader = document.createElement('th'); sportNameHeader.textContent = "Sport"; headerRow.appendChild(sportNameHeader); @@ -214,11 +246,14 @@ CATEGORIES.push(new Category( team2ScoreSpan.textContent = game.team2Score; scoresCell.appendChild(team2ScoreSpan); row.appendChild(scoresCell); + + const spacerCell = document.createElement('td'); + row.appendChild(spacerCell); const sportCell = document.createElement('td'); const sportSpan = document.createElement('span'); const divisionSpan = document.createElement('span'); - divisionSpan.classList.add('division-span'); + divisionSpan.classList.add('flat-content'); const divisionNameSpan = document.createElement('span'); const divisionGenderSpan = document.createElement('span'); divisionSpan.appendChild(divisionNameSpan); diff --git a/public/stylesheets/manage.css b/public/stylesheets/manage.css new file mode 100644 index 0000000..95ecca1 --- /dev/null +++ b/public/stylesheets/manage.css @@ -0,0 +1,15 @@ +.flat-content { + flex-direction: row; +} + +th { + text-align: left; +} + +td { + white-space: nowrap; +} + +.spacer-column { + width: 100%; +} \ No newline at end of file From 722bb4fce6933e9fc3a789a71c58f213bfc28980 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 17:38:45 -0700 Subject: [PATCH 22/41] Edit css --- public/stylesheets/style.css | 3 +++ 1 file changed, 3 insertions(+) diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index b6a5c49..ec6953e 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -15,3 +15,6 @@ a { flex-direction: column; } +.flat-content { + flex-direction: row; +} \ No newline at end of file From 8587212af921870075817c9ee08e3e252b6842af Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 17:45:58 -0700 Subject: [PATCH 23/41] Fix bug where games.js uses nonexistent moment.js module for formatting --- database/scores/games.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/database/scores/games.js b/database/scores/games.js index 4055523..922d600 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -23,10 +23,8 @@ async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, tea const query = `INSERT INTO scores.games(division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score) VALUES($1, $2, $3, $4, $5, $6, $7) RETURNING game_id;`; - - const dateISO = date.format('YYYY-MM-DD'); - const id = (await database.executeQuery(query, [divisionID, seasonID, dateISO, team1ID, team2ID, team1Score, team2Score]))[0][0]; + const id = (await database.executeQuery(query, [divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score]))[0][0]; return new Game(id, date, team1ID, team2ID, team1Score, team2Score); } From e257e9732087192151602008027a62c4ead3b297 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 17:48:21 -0700 Subject: [PATCH 24/41] Edit CSS --- public/stylesheets/manage.css | 4 ---- 1 file changed, 4 deletions(-) diff --git a/public/stylesheets/manage.css b/public/stylesheets/manage.css index 95ecca1..6a5359f 100644 --- a/public/stylesheets/manage.css +++ b/public/stylesheets/manage.css @@ -1,7 +1,3 @@ -.flat-content { - flex-direction: row; -} - th { text-align: left; } From 9f2572336fc897e758bc6cdff00902936e72f837 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 17:48:51 -0700 Subject: [PATCH 25/41] Move game submit page from /submit to /manage/addgame --- public/scripts/manage.js | 17 +-------------- routes/manage.js | 23 +++++++++++++++++++- routes/submit.js | 27 ------------------------ views/{submit.pug => manage/addgame.pug} | 2 +- views/manage/layout.pug | 10 +++++++++ 5 files changed, 34 insertions(+), 45 deletions(-) delete mode 100644 routes/submit.js rename views/{submit.pug => manage/addgame.pug} (93%) create mode 100644 views/manage/layout.pug diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 80bc534..04656eb 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -52,9 +52,6 @@ CATEGORIES.push(new Category( async function addSeason() { // }, - async function submitSeason() { - // - }, async function editSeason() { } @@ -89,9 +86,6 @@ CATEGORIES.push(new Category( async function addSport() { // }, - async function submitSport() { - // - }, async function editSport() { } @@ -143,9 +137,6 @@ CATEGORIES.push(new Category( async function addSeason() { // }, - async function submitSeason() { - // - }, async function editSeason() { } @@ -189,9 +180,6 @@ CATEGORIES.push(new Category( async function addSeason() { // }, - async function submitSeason() { - // - }, async function editSeason() { } @@ -278,10 +266,7 @@ CATEGORIES.push(new Category( dateCell.appendChild(dateSpan); row.appendChild(dateCell); }, - async function addSeason() { - // - }, - async function submitSeason() { + async function addGame() { // }, async function editSeason() { diff --git a/routes/manage.js b/routes/manage.js index 796064b..454c3c9 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -1,9 +1,30 @@ var express = require('express'); var router = express.Router(); +var genders = require('../database/scores/genders'); +var games = require('../database/scores/games'); + -/* GET manage page. */ router.get('/', function(req, res, next) { res.render('manage', { title: 'Score Management' }); }); +router.get('/addgame', function(req, res, next) { + res.render('manage/addgame', { title: 'Submit Score' }); +}); + +router.post('/submitgame', 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']; + + games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) + .then(res.send("SUCCESS")); +}); + module.exports = router; diff --git a/routes/submit.js b/routes/submit.js deleted file mode 100644 index fcd660b..0000000 --- a/routes/submit.js +++ /dev/null @@ -1,27 +0,0 @@ -var express = require('express'); -var router = express.Router(); -var genders = require('../database/scores/genders'); -var games = require('../database/scores/games'); - -/* GET submit page. */ -router.get('/', function(req, res, next) { - res.render('submit', { title: 'Submit Score' }); -}); - -/* POST submit page. */ -router.post('/', 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 = moment(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']; - - games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) - .then(res.send("SUCCESS")); -}); - -module.exports = router; diff --git a/views/submit.pug b/views/manage/addgame.pug similarity index 93% rename from views/submit.pug rename to views/manage/addgame.pug index 85f352f..04026f4 100644 --- a/views/submit.pug +++ b/views/manage/addgame.pug @@ -7,7 +7,7 @@ block stylesheets block content div#mobile-view h1 Submit Score - form(action='/submit', method='POST') + form(action='./submitgame', method='POST') span(class='form-section') label Year span(class='form-section-input') diff --git a/views/manage/layout.pug b/views/manage/layout.pug new file mode 100644 index 0000000..e4f85eb --- /dev/null +++ b/views/manage/layout.pug @@ -0,0 +1,10 @@ +doctype html +html + head + title= title + meta(name='viewport', content='width=device-width, initial-scale=1') + link(rel='stylesheet', href='/stylesheets/style.css') + block stylesheets + body + block content + block scripts From 7c5c059e09ca215048caaf71d53788032773eb01 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 17:55:15 -0700 Subject: [PATCH 26/41] Remove submit router from app.js --- app.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/app.js b/app.js index 6bc76de..c773280 100644 --- a/app.js +++ b/app.js @@ -6,7 +6,6 @@ var logger = require('morgan'); var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); -var submitRouter = require('./routes/submit'); var dataRouter = require('./routes/data'); var manageRouter = require('./routes/manage'); @@ -24,7 +23,6 @@ app.use(express.static(path.join(__dirname, 'public'))); app.use('/', indexRouter); app.use('/users', usersRouter); -app.use('/submit', submitRouter); app.use('/data', dataRouter); app.use('/manage', manageRouter); From 4133758bbafbb2c9085fd6d2eb7b2c3770796566 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 18:04:36 -0700 Subject: [PATCH 27/41] Create page to add season --- public/scripts/manage/season.js | 0 routes/manage.js | 12 ++++++++++++ views/manage/addseason.pug | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 public/scripts/manage/season.js create mode 100644 views/manage/addseason.pug diff --git a/public/scripts/manage/season.js b/public/scripts/manage/season.js new file mode 100644 index 0000000..e69de29 diff --git a/routes/manage.js b/routes/manage.js index 454c3c9..72d0784 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -2,6 +2,7 @@ var express = require('express'); var router = express.Router(); var genders = require('../database/scores/genders'); var games = require('../database/scores/games'); +var seasons = require('../database/scores/seasons'); router.get('/', function(req, res, next) { @@ -27,4 +28,15 @@ router.post('/submitgame', function(req, res, next) { .then(res.send("SUCCESS")); }); +router.get('/addseason', function(req, res, next) { + res.render('manage/addseason', { title: 'Add season', currentYear : (new Date()).getFullYear() }); +}); + +router.post('/submitseason', function(req, res, next) { + const year = req.body['year']; + + seasons.add(year) + .then(res.send("SUCCESS")); +}); + module.exports = router; diff --git a/views/manage/addseason.pug b/views/manage/addseason.pug new file mode 100644 index 0000000..e82937c --- /dev/null +++ b/views/manage/addseason.pug @@ -0,0 +1,19 @@ +extends layout + +block stylesheets + link(rel='stylesheet', href='/stylesheets/submit.css') + link(rel='stylesheet', href='/stylesheets/form.css') + +block content + div#mobile-view + h1 Add Season + form(action='./submitseason', method='POST') + span(class='form-section') + label Ending year + span(class='form-section-input') + input(type="number", name="year", value=currentYear) + span(class='form-section') + button#submit-button(type="submit") Submit + +block scripts + script(src='/scripts/season.js' type="module") \ No newline at end of file From 31575212ad0ef0e733ac8007b5a407282e3b62b6 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 18:42:02 -0700 Subject: [PATCH 28/41] Allow "Add new..." button to redirect to webpages per category --- public/scripts/manage.js | 8 +++++--- views/manage.pug | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 04656eb..2f24fc4 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -2,6 +2,7 @@ import * as Data from "./data.js"; const categoryDropdown = document.getElementById('category-dropdown'); const itemsListTable = document.getElementById('items-list'); +const addNewButton = document.getElementById('add-new-button'); function getGenderLetter(genderName) { @@ -50,7 +51,7 @@ CATEGORIES.push(new Category( row.appendChild(spacerCell); }, async function addSeason() { - // + window.location.href = "manage/addseason"; }, async function editSeason() { @@ -267,7 +268,7 @@ CATEGORIES.push(new Category( row.appendChild(dateCell); }, async function addGame() { - // + window.location.href = "manage/addgame"; }, async function editSeason() { @@ -313,4 +314,5 @@ listItems(CATEGORIES[categoryDropdown.selectedIndex]); categoryDropdown.onchange = () => { listItems(CATEGORIES[categoryDropdown.selectedIndex]); -}; \ No newline at end of file +}; +addNewButton.addEventListener('click', () => CATEGORIES[categoryDropdown.selectedIndex].addItem()); \ No newline at end of file diff --git a/views/manage.pug b/views/manage.pug index 7977824..c43dcf9 100644 --- a/views/manage.pug +++ b/views/manage.pug @@ -20,7 +20,7 @@ block content div h2#table-header table#items-list - button Add new... + button#add-new-button Add new... block scripts script(src='/scripts/manage.js' type="module") \ No newline at end of file From f95015095d425d28147d16c4ff52bdbea0144ab2 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 18:47:40 -0700 Subject: [PATCH 29/41] Add page for adding sports --- public/scripts/manage.js | 2 +- routes/manage.js | 12 ++++++++++++ views/manage/addsport.pug | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 views/manage/addsport.pug diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 2f24fc4..2017341 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -85,7 +85,7 @@ CATEGORIES.push(new Category( row.appendChild(spacerCell); }, async function addSport() { - // + window.location.href = "manage/addsport"; }, async function editSport() { diff --git a/routes/manage.js b/routes/manage.js index 72d0784..4582727 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -3,6 +3,7 @@ var router = express.Router(); var genders = require('../database/scores/genders'); var games = require('../database/scores/games'); var seasons = require('../database/scores/seasons'); +var sports = require('../database/scores/sports'); router.get('/', function(req, res, next) { @@ -39,4 +40,15 @@ router.post('/submitseason', function(req, res, next) { .then(res.send("SUCCESS")); }); +router.get('/addsport', function(req, res, next) { + res.render('manage/addsport', { title: 'Add sport' }); +}); + +router.post('/submitsport', function(req, res, next) { + const name = req.body['name']; + + sports.add(name) + .then(res.send("SUCCESS")); +}); + module.exports = router; diff --git a/views/manage/addsport.pug b/views/manage/addsport.pug new file mode 100644 index 0000000..7f7e5ad --- /dev/null +++ b/views/manage/addsport.pug @@ -0,0 +1,19 @@ +extends layout + +block stylesheets + link(rel='stylesheet', href='/stylesheets/submit.css') + link(rel='stylesheet', href='/stylesheets/form.css') + +block content + div#mobile-view + h1 Add Sport + form(action='./submitsport', method='POST') + span(class='form-section') + label Sport name + span(class='form-section-input') + input(type="text", name="name") + span(class='form-section') + button#submit-button(type="submit") Submit + +block scripts + script(src='/scripts/sport.js' type="module") \ No newline at end of file From 1a3b013a0f93f9c12e8d4a170173636c5704e005 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 19:11:16 -0700 Subject: [PATCH 30/41] Add page to add divisions --- public/scripts/manage.js | 6 +++--- public/scripts/manage/division.js | 18 ++++++++++++++++++ routes/manage.js | 23 +++++++++++++++++++++++ views/manage/adddivision.pug | 30 ++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 public/scripts/manage/division.js create mode 100644 views/manage/adddivision.pug diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 2017341..770a517 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -135,10 +135,10 @@ CATEGORIES.push(new Category( .then(data => sportCell.textContent = data); row.appendChild(sportCell); }, - async function addSeason() { - // + async function addDivision() { + window.location.href = "manage/adddivision"; }, - async function editSeason() { + async function editDivision() { } )); diff --git a/public/scripts/manage/division.js b/public/scripts/manage/division.js new file mode 100644 index 0000000..6b37492 --- /dev/null +++ b/public/scripts/manage/division.js @@ -0,0 +1,18 @@ +import * as Data from "../data.js"; + + +const sportDropdown = document.getElementById('sport-dropdown'); + +async function listSports() { + sportDropdown.innerHTML = ""; + + const sportsList = await Data.getSports(); + + sportsList.forEach(sport => { + const option = document.createElement('option'); + option.text = sport.name; + option.value = sport.id; + sportDropdown.appendChild(option); + }); +} +listSports(); \ No newline at end of file diff --git a/routes/manage.js b/routes/manage.js index 4582727..72f17e8 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -4,6 +4,8 @@ var genders = require('../database/scores/genders'); var games = require('../database/scores/games'); var seasons = require('../database/scores/seasons'); var sports = require('../database/scores/sports'); +var divisions = require('../database/scores/divisions'); +var genders = require('../database/scores/genders'); router.get('/', function(req, res, next) { @@ -51,4 +53,25 @@ router.post('/submitsport', function(req, res, next) { .then(res.send("SUCCESS")); }); +router.get('/adddivision', function(req, res, next) { + res.render('manage/adddivision', { title: 'Add division' }); +}); + +router.post('/submitdivision', function(req, res, next) { + const name = req.body['name']; + const sport = req.body['sport']; + const genderName = req.body['gender']; + + if(genderName == "both") { + divisions.add(name, genders.FEMALE, sport) + .then(divisions.add(name, genders.MALE, sport) + .then(res.send("SUCCESS"))); + } + else { + const gender = (genderName == "female") ? genders.FEMALE : genders.MALE; + divisions.add(name, gender, sport) + .then(res.send("SUCCESS")); + } +}); + module.exports = router; diff --git a/views/manage/adddivision.pug b/views/manage/adddivision.pug new file mode 100644 index 0000000..4b8d7d1 --- /dev/null +++ b/views/manage/adddivision.pug @@ -0,0 +1,30 @@ +extends layout + +block stylesheets + link(rel='stylesheet', href='/stylesheets/submit.css') + link(rel='stylesheet', href='/stylesheets/form.css') + +block content + div#mobile-view + h1 Add Division + form(action='./submitdivision', method='POST') + span(class='form-section') + label Sport + span(class='form-section-input') + select#sport-dropdown(name="sport" class="form-main-dropdown") + span(class='form-section') + label Gender + span(class='form-section-input') + select#gender-dropdown(name="gender" class="form-main-dropdown") + option(value="both") Both + option(value="female") Female + option(value="male") Male + span(class='form-section') + label Division name + span(class='form-section-input') + input(type="text", name="name") + span(class='form-section') + button#submit-button(type="submit") Submit + +block scripts + script(src='/scripts/manage/division.js' type="module") \ No newline at end of file From aacff9cca1121b4afd4156fa7f5fa9d966b76620 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 19:17:23 -0700 Subject: [PATCH 31/41] Add page to add teams --- public/scripts/manage.js | 6 +++--- public/scripts/manage/team.js | 18 ++++++++++++++++++ routes/manage.js | 13 +++++++++++++ views/manage/addteam.pug | 23 +++++++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 public/scripts/manage/team.js create mode 100644 views/manage/addteam.pug diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 770a517..651047e 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -178,10 +178,10 @@ CATEGORIES.push(new Category( .then(data => sportCell.textContent = data); row.appendChild(sportCell); }, - async function addSeason() { - // + async function addTeam() { + window.location.href = "manage/addteam"; }, - async function editSeason() { + async function editTeam() { } )); diff --git a/public/scripts/manage/team.js b/public/scripts/manage/team.js new file mode 100644 index 0000000..6b37492 --- /dev/null +++ b/public/scripts/manage/team.js @@ -0,0 +1,18 @@ +import * as Data from "../data.js"; + + +const sportDropdown = document.getElementById('sport-dropdown'); + +async function listSports() { + sportDropdown.innerHTML = ""; + + const sportsList = await Data.getSports(); + + sportsList.forEach(sport => { + const option = document.createElement('option'); + option.text = sport.name; + option.value = sport.id; + sportDropdown.appendChild(option); + }); +} +listSports(); \ No newline at end of file diff --git a/routes/manage.js b/routes/manage.js index 72f17e8..04b2b9a 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -6,6 +6,7 @@ var seasons = require('../database/scores/seasons'); var sports = require('../database/scores/sports'); var divisions = require('../database/scores/divisions'); var genders = require('../database/scores/genders'); +var teams = require('../database/scores/teams'); router.get('/', function(req, res, next) { @@ -74,4 +75,16 @@ router.post('/submitdivision', function(req, res, next) { } }); +router.get('/addteam', function(req, res, next) { + res.render('manage/addteam', { title: 'Add team' }); +}); + +router.post('/submitteam', function(req, res, next) { + const name = req.body['name']; + const sport = req.body['sport']; + + teams.add(name, sport) + .then(res.send("SUCCESS")); +}); + module.exports = router; diff --git a/views/manage/addteam.pug b/views/manage/addteam.pug new file mode 100644 index 0000000..c3d422b --- /dev/null +++ b/views/manage/addteam.pug @@ -0,0 +1,23 @@ +extends layout + +block stylesheets + link(rel='stylesheet', href='/stylesheets/submit.css') + link(rel='stylesheet', href='/stylesheets/form.css') + +block content + div#mobile-view + h1 Add Team + form(action='./submitteam', method='POST') + span(class='form-section') + label Sport + span(class='form-section-input') + select#sport-dropdown(name="sport" class="form-main-dropdown") + span(class='form-section') + label Team name + span(class='form-section-input') + input(type="text", name="name") + span(class='form-section') + button#submit-button(type="submit") Submit + +block scripts + script(src='/scripts/manage/team.js' type="module") \ No newline at end of file From 5c7875382818d5725b9a7d2c928336e48f60ae78 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 19:23:20 -0700 Subject: [PATCH 32/41] Fix bug regarding page redirection --- public/scripts/manage.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 651047e..5482569 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -51,7 +51,7 @@ CATEGORIES.push(new Category( row.appendChild(spacerCell); }, async function addSeason() { - window.location.href = "manage/addseason"; + window.location.href = "/manage/addseason"; }, async function editSeason() { @@ -85,7 +85,7 @@ CATEGORIES.push(new Category( row.appendChild(spacerCell); }, async function addSport() { - window.location.href = "manage/addsport"; + window.location.href = "/manage/addsport"; }, async function editSport() { @@ -136,7 +136,7 @@ CATEGORIES.push(new Category( row.appendChild(sportCell); }, async function addDivision() { - window.location.href = "manage/adddivision"; + window.location.href = "/manage/adddivision"; }, async function editDivision() { @@ -179,7 +179,7 @@ CATEGORIES.push(new Category( row.appendChild(sportCell); }, async function addTeam() { - window.location.href = "manage/addteam"; + window.location.href = "/manage/addteam"; }, async function editTeam() { @@ -268,7 +268,7 @@ CATEGORIES.push(new Category( row.appendChild(dateCell); }, async function addGame() { - window.location.href = "manage/addgame"; + window.location.href = "/manage/addgame"; }, async function editSeason() { From 6ef9096cb792e5bd4a0aaebf1391574ead25d267 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 19:25:44 -0700 Subject: [PATCH 33/41] Remove delete button on manage page --- public/scripts/manage.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 5482569..1f4ff7e 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -297,12 +297,6 @@ async function listItems(category) { editSpan.appendChild(editButton); manageCell.appendChild(editSpan); - const removeSpan = document.createElement('remove'); - const removeButton = document.createElement('button'); - removeButton.textContent = "D"; - removeSpan.appendChild(removeButton); - manageCell.appendChild(removeSpan); - row.appendChild(manageCell); itemsListTable.appendChild(row); From 4fec3a45c800a07b999624ce7e7338d456ccfd61 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 19:40:33 -0700 Subject: [PATCH 34/41] Change header in addseason.pug to match title --- views/manage/addseason.pug | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/manage/addseason.pug b/views/manage/addseason.pug index e82937c..96d4080 100644 --- a/views/manage/addseason.pug +++ b/views/manage/addseason.pug @@ -6,7 +6,7 @@ block stylesheets block content div#mobile-view - h1 Add Season + h1 #{title} form(action='./submitseason', method='POST') span(class='form-section') label Ending year From 35af76c90f7e4d4acf1979c1c8ce259881ea68e3 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 22:03:02 -0700 Subject: [PATCH 35/41] Add ability to edit sports in manage page --- public/scripts/manage.js | 12 ++++++---- public/scripts/manage/sport.js | 43 ++++++++++++++++++++++++++++++++++ public/stylesheets/form.css | 4 ++++ routes/manage.js | 13 +++++----- views/manage/addsport.pug | 13 ++++++---- 5 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 public/scripts/manage/sport.js diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 1f4ff7e..8b0221b 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -10,13 +10,12 @@ function getGenderLetter(genderName) { } class Category { - constructor(name, getItems, listHeaders, listItem, addItem, submitItem, editItem) { + constructor(name, getItems, listHeaders, listItem, addItem, editItem) { this.name = name; this.getItems = getItems; this.listHeaders = listHeaders; this.listItem = listItem; this.addItem = addItem; - this.submitItem = submitItem; this.editItem = editItem; } } @@ -85,10 +84,10 @@ CATEGORIES.push(new Category( row.appendChild(spacerCell); }, async function addSport() { - window.location.href = "/manage/addsport"; + window.location.href = `/manage/sport`; }, - async function editSport() { - + async function editSport(id) { + window.location.href = `/manage/sport?sport=${id}` } )); @@ -294,6 +293,9 @@ async function listItems(category) { const editSpan = document.createElement('span'); const editButton = document.createElement('button'); editButton.textContent = "E"; + editButton.addEventListener('click', () => { + CATEGORIES[categoryDropdown.selectedIndex].editItem(item.id); + }); editSpan.appendChild(editButton); manageCell.appendChild(editSpan); diff --git a/public/scripts/manage/sport.js b/public/scripts/manage/sport.js new file mode 100644 index 0000000..2b12593 --- /dev/null +++ b/public/scripts/manage/sport.js @@ -0,0 +1,43 @@ +import * as Data from "../data.js"; + + +const mainHeader = document.getElementById('main-header'); +const nameTextbox = document.getElementById('name-textbox'); +const submitButton = document.getElementById('submit-button'); +const deleteButton = document.getElementById('delete-button'); +const submissionForm = document.getElementById('submission-form'); + + +async function initializeForm() { + let params = new URLSearchParams(location.search); + let sportID = params.get('sport') + if(sportID) { + mainHeader.textContent = "Edit Sport"; + + const sportName = await Data.getSportName(sportID); + + nameTextbox.value = sportName; + deleteButton.style.visibility = "visible"; + deleteButton.disabled = false; + + const sportIDInput = document.createElement('input'); + sportIDInput.setAttribute('name', 'sport'); + sportIDInput.setAttribute('value', sportID); + sportIDInput.setAttribute('type', 'hidden'); + submissionForm.appendChild(sportIDInput); + } + nameTextbox.disabled = false; + + nameTextbox.addEventListener('keyup', checkDataValidity); +} +initializeForm(); + +async function checkDataValidity() { + let dataIsValid = true; + + if(!nameTextbox.value) dataIsValid = false; + + + if(dataIsValid) submitButton.disabled = false; + else submitButton.disabled = true; +} \ No newline at end of file diff --git a/public/stylesheets/form.css b/public/stylesheets/form.css index 85b34bc..21abe10 100644 --- a/public/stylesheets/form.css +++ b/public/stylesheets/form.css @@ -31,4 +31,8 @@ form { #submit-button { margin-top: 1.5em; + } + + #delete-button { + visibility: hidden; } \ No newline at end of file diff --git a/routes/manage.js b/routes/manage.js index 04b2b9a..a42f734 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -33,7 +33,7 @@ router.post('/submitgame', function(req, res, next) { }); router.get('/addseason', function(req, res, next) { - res.render('manage/addseason', { title: 'Add season', currentYear : (new Date()).getFullYear() }); + res.render('manage/addseason', { title: 'Add Season', currentYear : (new Date()).getFullYear() }); }); router.post('/submitseason', function(req, res, next) { @@ -43,15 +43,16 @@ router.post('/submitseason', function(req, res, next) { .then(res.send("SUCCESS")); }); -router.get('/addsport', function(req, res, next) { - res.render('manage/addsport', { title: 'Add sport' }); +router.get('/sport', function(req, res, next) { + res.render('manage/addsport', { title: 'Add Sport' }); }); -router.post('/submitsport', function(req, res, next) { +router.post('/sport', function(req, res, next) { const name = req.body['name']; + const id = req.body['sport']; - sports.add(name) - .then(res.send("SUCCESS")); + if(id) sports.rename(id, name).then(res.redirect('/manage')); + else sports.add(name).then(res.redirect('/manage')); }); router.get('/adddivision', function(req, res, next) { diff --git a/views/manage/addsport.pug b/views/manage/addsport.pug index 7f7e5ad..5251b7e 100644 --- a/views/manage/addsport.pug +++ b/views/manage/addsport.pug @@ -6,14 +6,17 @@ block stylesheets block content div#mobile-view - h1 Add Sport - form(action='./submitsport', method='POST') + h1#main-header Add Sport + form#submission-form(action='./sport', method='POST') span(class='form-section') label Sport name span(class='form-section-input') - input(type="text", name="name") + input#name-textbox(type="text" name="name" disabled) span(class='form-section') - button#submit-button(type="submit") Submit + button#submit-button(type="submit" disabled) Submit + span(class='form-section') + button#delete-button(disabled) Delete + block scripts - script(src='/scripts/sport.js' type="module") \ No newline at end of file + script(src='/scripts/manage/sport.js' type="module") \ No newline at end of file From cf91548daca5c4b4b174adca65204ab9d57e18a4 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 22:10:47 -0700 Subject: [PATCH 36/41] Add ability to remove sports --- public/scripts/manage/sport.js | 17 ++++++++++++++++- routes/manage.js | 4 +++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/public/scripts/manage/sport.js b/public/scripts/manage/sport.js index 2b12593..c30fbce 100644 --- a/public/scripts/manage/sport.js +++ b/public/scripts/manage/sport.js @@ -40,4 +40,19 @@ async function checkDataValidity() { if(dataIsValid) submitButton.disabled = false; else submitButton.disabled = true; -} \ No newline at end of file +} + +async function removeSport() { + const removeInput = document.createElement('input'); + removeInput.setAttribute('name', 'remove'); + removeInput.setAttribute('value', 1); + removeInput.setAttribute('type', 'hidden'); + submissionForm.appendChild(removeInput); + submissionForm.submit(); +} + +deleteButton.addEventListener('click', () => { + const verified = confirm("This sport will be removed."); + + if(verified) removeSport(); +}); \ No newline at end of file diff --git a/routes/manage.js b/routes/manage.js index a42f734..145dfa0 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -50,8 +50,10 @@ router.get('/sport', function(req, res, next) { router.post('/sport', function(req, res, next) { const name = req.body['name']; const id = req.body['sport']; + const remove = req.body['remove']; - if(id) sports.rename(id, name).then(res.redirect('/manage')); + 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')); }); From a2b9c5f50ec8e5e258a38b99e71a15dbaf130688 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 23:23:57 -0700 Subject: [PATCH 37/41] Add ability to edit divisions --- public/scripts/form.js | 40 ++++++++++++++++++++ public/scripts/manage.js | 6 +-- public/scripts/manage/division.js | 61 +++++++++++++++++++++++++------ routes/manage.js | 32 ++++++++++------ views/manage/adddivision.pug | 14 ++++--- 5 files changed, 122 insertions(+), 31 deletions(-) create mode 100644 public/scripts/form.js diff --git a/public/scripts/form.js b/public/scripts/form.js new file mode 100644 index 0000000..9d4ee93 --- /dev/null +++ b/public/scripts/form.js @@ -0,0 +1,40 @@ +import * as Data from "./data.js"; + +export async function populateSports(sportDropdown, selectedSportID = undefined) { + sportDropdown.innerHTML = ""; + + const sportsList = await Data.getSports(); + + let currentIndex = 0; + let selectedSportIndex; + sportsList.forEach(sport => { + const option = document.createElement('option'); + option.text = sport.name; + option.value = sport.id; + sportDropdown.appendChild(option); + + if(sport.id == selectedSportID) selectedSportIndex = currentIndex; + currentIndex++; + }); + + if(selectedSportIndex) sportDropdown.selectedIndex = selectedSportIndex; +} + +export async function addHiddenValue(name, value, form) { + const valueInput = document.createElement('input'); + valueInput.setAttribute('name', name); + valueInput.setAttribute('value', value); + valueInput.setAttribute('type', 'hidden'); + form.appendChild(valueInput); +} + +export async function addRemoveFunction(removeButton, form, objectTitle) { + removeButton.addEventListener('click', async () => { + const verified = confirm(`This ${objectTitle} will be removed.`); + + if(verified) { + await addHiddenValue('remove', 1, form); + form.submit(); + } + }); +} \ No newline at end of file diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 8b0221b..a30c179 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -135,10 +135,10 @@ CATEGORIES.push(new Category( row.appendChild(sportCell); }, async function addDivision() { - window.location.href = "/manage/adddivision"; + window.location.href = "/manage/division"; }, - async function editDivision() { - + async function editDivision(id) { + window.location.href = `/manage/division?division=${id}` } )); diff --git a/public/scripts/manage/division.js b/public/scripts/manage/division.js index 6b37492..fb623c5 100644 --- a/public/scripts/manage/division.js +++ b/public/scripts/manage/division.js @@ -1,18 +1,57 @@ import * as Data from "../data.js"; +import * as Form from "../form.js"; +const submissionForm = document.getElementById('submission-form'); const sportDropdown = document.getElementById('sport-dropdown'); +const genderDropdown = document.getElementById('gender-dropdown'); +const nameTextbox = document.getElementById('name-textbox'); +const submitButton = document.getElementById('submit-button'); +const deleteButton = document.getElementById('delete-button'); -async function listSports() { - sportDropdown.innerHTML = ""; - const sportsList = await Data.getSports(); - - sportsList.forEach(sport => { - const option = document.createElement('option'); - option.text = sport.name; - option.value = sport.id; - sportDropdown.appendChild(option); - }); +async function initializeForm() { + let params = new URLSearchParams(location.search); + let divisionID = params.get('division'); + if(divisionID) { + const division = await Data.getDivision(divisionID); + + nameTextbox.value = division.name; + + deleteButton.style.visibility = "visible"; + deleteButton.disabled = false; + + const gender = division.gender.name; + + if(gender == 'female') genderDropdown.selectedIndex = 1; + else genderDropdown.selectedIndex = 2; + + Form.populateSports(sportDropdown, division.sportID); + + Form.addHiddenValue('division', divisionID, submissionForm); + } + else { + Form.populateSports(sportDropdown); + + genderDropdown.disabled = false; + + sportDropdown.disabled = false; + } + + nameTextbox.disabled = false; + nameTextbox.addEventListener('keyup', checkDataValidity); } -listSports(); \ No newline at end of file +initializeForm(); + +async function checkDataValidity() { + let dataIsValid = true; + + if(!nameTextbox.value) dataIsValid = false; + + + if(dataIsValid) submitButton.disabled = false; + else submitButton.disabled = true; +} + +Form.addRemoveFunction(deleteButton, submissionForm, "division"); + diff --git a/routes/manage.js b/routes/manage.js index 145dfa0..4b6683f 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -57,24 +57,34 @@ router.post('/sport', function(req, res, next) { else sports.add(name).then(res.redirect('/manage')); }); -router.get('/adddivision', function(req, res, next) { - res.render('manage/adddivision', { title: 'Add division' }); +router.get('/division', function(req, res, next) { + let title = req.query.division ? 'Edit Division' : 'Add Division' + + res.render('manage/adddivision', { title }); }); -router.post('/submitdivision', function(req, res, next) { +router.post('/division', function(req, res, next) { const name = req.body['name']; const sport = req.body['sport']; const genderName = req.body['gender']; - if(genderName == "both") { - divisions.add(name, genders.FEMALE, sport) - .then(divisions.add(name, genders.MALE, sport) - .then(res.send("SUCCESS"))); - } + 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 { - const gender = (genderName == "female") ? genders.FEMALE : genders.MALE; - divisions.add(name, gender, sport) - .then(res.send("SUCCESS")); + 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")); + } } }); diff --git a/views/manage/adddivision.pug b/views/manage/adddivision.pug index 4b8d7d1..5db737c 100644 --- a/views/manage/adddivision.pug +++ b/views/manage/adddivision.pug @@ -6,25 +6,27 @@ block stylesheets block content div#mobile-view - h1 Add Division - form(action='./submitdivision', method='POST') + h1 #{title} + form#submission-form(action='./division', method='POST') span(class='form-section') label Sport span(class='form-section-input') - select#sport-dropdown(name="sport" class="form-main-dropdown") + select#sport-dropdown(name="sport" class="form-main-dropdown" disabled) span(class='form-section') label Gender span(class='form-section-input') - select#gender-dropdown(name="gender" class="form-main-dropdown") + select#gender-dropdown(name="gender" class="form-main-dropdown" disabled) option(value="both") Both option(value="female") Female option(value="male") Male span(class='form-section') label Division name span(class='form-section-input') - input(type="text", name="name") + input#name-textbox(type="text", name="name" disabled) span(class='form-section') - button#submit-button(type="submit") Submit + button#submit-button(type="submit" disabled) Submit + span(class='form-section') + button#delete-button(disabled) Delete block scripts script(src='/scripts/manage/division.js' type="module") \ No newline at end of file From 5d88f0ac4dd393ba9a2181ceda8127454d2af140 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 23:45:24 -0700 Subject: [PATCH 38/41] Add ability to edit teams --- database/scores/teams.js | 7 +++-- public/scripts/data.js | 4 +-- public/scripts/index.js | 4 +-- public/scripts/manage.js | 14 +++++----- public/scripts/manage/team.js | 52 +++++++++++++++++++++++++++-------- routes/manage.js | 16 +++++++---- views/manage/addteam.pug | 12 ++++---- 7 files changed, 74 insertions(+), 35 deletions(-) diff --git a/database/scores/teams.js b/database/scores/teams.js index 3717841..592796f 100644 --- a/database/scores/teams.js +++ b/database/scores/teams.js @@ -65,11 +65,12 @@ async function retrieve(sportID = undefined) { } async function getFromID(id) { - const query = `SELECT team_name + const query = `SELECT team_name, sport_id FROM scores.teams WHERE team_id = $1;`; - const name = (await database.executeQuery(query, [id]))[0][0]; - return new Team(id, name); + const row = (await database.executeQuery(query, [id]))[0]; + console.log(row); + return new Team(id, row[0], row[1]); } diff --git a/public/scripts/data.js b/public/scripts/data.js index 8ef7479..f2eead9 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -47,10 +47,10 @@ export async function getTeams(sportID = undefined) { return teamsList; } -export async function getTeamName(teamID) { +export async function getTeam(teamID) { const response = await fetch(`/data/team?team=${+teamID}`); const team = await response.json(); - return team.name; + return team; } export async function getGames(teamID = undefined, divisionID = undefined, seasonID = undefined) { diff --git a/public/scripts/index.js b/public/scripts/index.js index 41d9610..d752ff1 100644 --- a/public/scripts/index.js +++ b/public/scripts/index.js @@ -128,8 +128,8 @@ async function listGames() { row.appendChild(scoreCell); const opponentCell = document.createElement('td'); - Data.getTeamName(game.team2ID) - .then(data => opponentCell.textContent = data); + Data.getTeam(game.team2ID) + .then(data => opponentCell.textContent = data.name); row.appendChild(opponentCell); const dateCell = document.createElement('td'); diff --git a/public/scripts/manage.js b/public/scripts/manage.js index a30c179..ca2f4b2 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -178,10 +178,10 @@ CATEGORIES.push(new Category( row.appendChild(sportCell); }, async function addTeam() { - window.location.href = "/manage/addteam"; + window.location.href = "/manage/team"; }, - async function editTeam() { - + async function editTeam(id) { + window.location.href = `/manage/team?team=${id}`; } )); @@ -217,12 +217,12 @@ CATEGORIES.push(new Category( function listGame(game, row) { const teamsCell = document.createElement('td'); const team1NameSpan = document.createElement('span'); - Data.getTeamName(game.team1ID) - .then(data => team1NameSpan.textContent = data); + Data.getTeam(game.team1ID) + .then(data => team1NameSpan.textContent = data.name); teamsCell.appendChild(team1NameSpan); const team2NameSpan = document.createElement('span'); - Data.getTeamName(game.team2ID) - .then(data => team2NameSpan.textContent = data); + Data.getTeam(game.team2ID) + .then(data => team2NameSpan.textContent = data.name); teamsCell.appendChild(team2NameSpan); row.appendChild(teamsCell); diff --git a/public/scripts/manage/team.js b/public/scripts/manage/team.js index 6b37492..d5d8291 100644 --- a/public/scripts/manage/team.js +++ b/public/scripts/manage/team.js @@ -1,18 +1,48 @@ import * as Data from "../data.js"; +import * as Form from "../form.js"; +const submissionForm = document.getElementById('submission-form'); const sportDropdown = document.getElementById('sport-dropdown'); +const nameTextbox = document.getElementById('name-textbox'); +const submitButton = document.getElementById('submit-button'); +const deleteButton = document.getElementById('delete-button'); -async function listSports() { - sportDropdown.innerHTML = ""; +async function initializeForm() { + let params = new URLSearchParams(location.search); + let teamID = params.get('team'); + if(teamID) { + const team = await Data.getTeam(teamID); - const sportsList = await Data.getSports(); - - sportsList.forEach(sport => { - const option = document.createElement('option'); - option.text = sport.name; - option.value = sport.id; - sportDropdown.appendChild(option); - }); + nameTextbox.value = team.name; + + deleteButton.style.visibility = "visible"; + deleteButton.disabled = false; + + Form.populateSports(sportDropdown, team.sportID); + + console.log(team.sportID); + Form.addHiddenValue('team', teamID, submissionForm); + } + else { + sportDropdown.disabled = false; + + Form.populateSports(sportDropdown); + } + + nameTextbox.disabled = false; + nameTextbox.addEventListener('keyup', checkDataValidity); } -listSports(); \ No newline at end of file +initializeForm(); + +async function checkDataValidity() { + let dataIsValid = true; + + if(!nameTextbox.value) dataIsValid = false; + + + if(dataIsValid) submitButton.disabled = false; + else submitButton.disabled = true; +} + +Form.addRemoveFunction(deleteButton, submissionForm, "team"); \ No newline at end of file diff --git a/routes/manage.js b/routes/manage.js index 4b6683f..4114790 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -88,16 +88,22 @@ router.post('/division', function(req, res, next) { } }); -router.get('/addteam', function(req, res, next) { - res.render('manage/addteam', { title: 'Add team' }); +router.get('/team', function(req, res, next) { + let title = req.query.team ? 'Edit Team' : 'Add Team' + + res.render('manage/addteam', { title }); }); -router.post('/submitteam', function(req, res, next) { +router.post('/team', function(req, res, next) { const name = req.body['name']; const sport = req.body['sport']; - teams.add(name, sport) - .then(res.send("SUCCESS")); + 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")); }); module.exports = router; diff --git a/views/manage/addteam.pug b/views/manage/addteam.pug index c3d422b..3cb6ccb 100644 --- a/views/manage/addteam.pug +++ b/views/manage/addteam.pug @@ -6,18 +6,20 @@ block stylesheets block content div#mobile-view - h1 Add Team - form(action='./submitteam', method='POST') + h1 #{title} + form#submission-form(action='./team', method='POST') span(class='form-section') label Sport span(class='form-section-input') - select#sport-dropdown(name="sport" class="form-main-dropdown") + select#sport-dropdown(name="sport" class="form-main-dropdown" disabled) span(class='form-section') label Team name span(class='form-section-input') - input(type="text", name="name") + input#name-textbox(type="text", name="name" disabled) span(class='form-section') - button#submit-button(type="submit") Submit + button#submit-button(type="submit" disabled) Submit + span(class='form-section') + button#delete-button(disabled) Delete block scripts script(src='/scripts/manage/team.js' type="module") \ No newline at end of file From b5495b1f57c51fab126e4244af1420466d242236 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Tue, 23 Nov 2021 00:49:11 -0700 Subject: [PATCH 39/41] Add ability to edit games --- database/scores/games.js | 26 ++++++++++- public/scripts/data.js | 6 +++ public/scripts/form.js | 86 +++++++++++++++++++++++++++++++++++ public/scripts/manage.js | 6 +-- public/scripts/manage/game.js | 71 +++++++++++++++++++++++++++++ routes/data.js | 5 ++ routes/manage.js | 19 ++++++-- views/manage/addgame.pug | 28 ++++++------ views/manage/addseason.pug | 2 +- 9 files changed, 226 insertions(+), 23 deletions(-) create mode 100644 public/scripts/manage/game.js diff --git a/database/scores/games.js b/database/scores/games.js index 922d600..270941b 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -71,10 +71,34 @@ async function retrieve(teamID, divisionID, seasonID) { return gamesList; } +async function edit(gameID, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) { + const query = `UPDATE scores.games + SET division_id = $2, + season_id = $3, + game_date = $4, + team1_id = $5, + team2_id = $6, + team1_score = $7, + team2_score = $8 + WHERE game_id = $1;`; + await database.executeQuery(query, [gameID, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score]); + return new Game(gameID, date, team1ID, team2ID, team1Score, team2Score, divisionID, seasonID); +} + +async function getFromID(gameID) { + const query = `SELECT game_id, division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score + FROM scores.games + WHERE game_id = $1;`; + const row = (await database.executeQuery(query, [gameID]))[0]; + return new Game(row[0], row[3].toISOString().slice(0,10), row[4], row[5], row[6], row[7], row[1], row[2]); +} + exports.add = add; exports.remove = remove; -exports.retrieve = retrieve; \ No newline at end of file +exports.retrieve = retrieve; +exports.edit = edit; +exports.getFromID = getFromID; \ No newline at end of file diff --git a/public/scripts/data.js b/public/scripts/data.js index f2eead9..873d4d6 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -63,4 +63,10 @@ export async function getGames(teamID = undefined, divisionID = undefined, seaso const response = await fetch(URL); const gamesList = await response.json(); return gamesList; +} + +export async function getGame(gameID) { + const response = await fetch(`/data/game?game=${gameID}`); + const game = await response.json(); + return game; } \ No newline at end of file diff --git a/public/scripts/form.js b/public/scripts/form.js index 9d4ee93..5132ea9 100644 --- a/public/scripts/form.js +++ b/public/scripts/form.js @@ -20,6 +20,92 @@ export async function populateSports(sportDropdown, selectedSportID = undefined) if(selectedSportIndex) sportDropdown.selectedIndex = selectedSportIndex; } +export async function populateSeasons(seasonDropdown, selectedSeasonID = undefined) { + seasonDropdown.innerHTML = ""; + + const seasonsList = await Data.getSeasons(); + + let currentIndex = 0; + let selectedSeasonIndex; + seasonsList.forEach(season => { + const option = document.createElement('option'); + option.text = (season.year - 1) + "-" + season.year; + option.value = season.id; + seasonDropdown.appendChild(option); + + if(season.id == selectedSeasonID) selectedSeasonIndex = currentIndex; + currentIndex++; + }); + + if(selectedSeasonIndex) seasonDropdown.selectedIndex = selectedSeasonIndex; +} + +export async function populateGenders(genderDropdown, selectedSportID, selectedGender = undefined) { + genderDropdown.innerHTML = ""; + + const gendersList = await Data.getGenders(selectedSportID); + + if(selectedSportID) { + let currentIndex = 0; + let selectedGenderIndex; + gendersList.forEach(gender => { + const option = document.createElement('option'); + option.text = (gender.name == "female") ? "Female" : (gender.name == "male") ? "Male" : ""; + option.value = gender.name; + genderDropdown.appendChild(option); + + if(gender.name == selectedGender) selectedGenderIndex = currentIndex; + currentIndex++; + }); + + if(selectedGenderIndex) genderDropdown.selectedIndex = selectedGenderIndex; + } +} + +export async function populateDivisions (divisionDropdown, selectedSportID, selectedGender, selectedDivisionID = undefined) { + divisionDropdown.innerHTML = ""; + + if(selectedSportID && selectedGender) { + const divisionsList = await Data.getDivisions(selectedSportID, selectedGender); + + let currentIndex = 0; + let selectedDivisionIndex; + divisionsList.forEach(division => { + const option = document.createElement('option'); + option.text = division.name; + option.value = division.id; + divisionDropdown.appendChild(option); + + if(division.id == selectedDivisionID) selectedDivisionIndex = currentIndex; + currentIndex++; + }); + + if(selectedDivisionIndex) divisionDropdown.selectedIndex = selectedDivisionIndex; + } +} + +export async function populateTeams(teamDropdown, selectedSportID, selectedTeamID) { + teamDropdown.innerHTML = ""; + + if(selectedSportID) { + const teamsList = await Data.getTeams(selectedSportID); + + let currentIndex = 0; + let selectedTeamIndex; + teamsList.forEach(team => { + const option = document.createElement('option'); + option.text = team.name; + option.value = team.id; + teamDropdown.appendChild(option); + + if(team.id == selectedTeamID) selectedTeamIndex = currentIndex; + currentIndex++; + }); + + if(selectedTeamIndex) teamDropdown.selectedIndex = selectedTeamIndex; + } +} + export async function addHiddenValue(name, value, form) { const valueInput = document.createElement('input'); valueInput.setAttribute('name', name); diff --git a/public/scripts/manage.js b/public/scripts/manage.js index ca2f4b2..27ad375 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -267,10 +267,10 @@ CATEGORIES.push(new Category( row.appendChild(dateCell); }, async function addGame() { - window.location.href = "/manage/addgame"; + window.location.href = "/manage/game"; }, - async function editSeason() { - + async function editGame(id) { + window.location.href = `/manage/game?game=${id}`; } )); diff --git a/public/scripts/manage/game.js b/public/scripts/manage/game.js new file mode 100644 index 0000000..0cc134b --- /dev/null +++ b/public/scripts/manage/game.js @@ -0,0 +1,71 @@ +import * as Data from "./../data.js"; +import * as Form from "./../form.js"; + + +const submissionForm = document.getElementById('submission-form'); +const sportDropdown = document.getElementById('sport-dropdown'); +const seasonDropdown = document.getElementById('year-dropdown'); +const genderDropdown = document.getElementById('gender-dropdown'); +const divisionDropdown = document.getElementById('division-dropdown'); +const dateInput = document.getElementById('date-input'); +const team1Dropdown = document.getElementById('team1-dropdown'); +const team2Dropdown = document.getElementById('team2-dropdown'); +const team1ScoreTextbox = document.getElementById('team1-score-textbox'); +const team2ScoreTextbox = document.getElementById('team2-score-textbox'); +const submitButton = document.getElementById('submit-button'); +const deleteButton = document.getElementById('delete-button'); + + +async function initializeForm() { + let params = new URLSearchParams(location.search); + let gameID = params.get('game'); + if(gameID) { + deleteButton.style.visibility = "visible"; + deleteButton.disabled = false; + + const game = await Data.getGame(gameID); + + Form.populateSeasons(seasonDropdown, game.seasonID); + Data.getDivision(game.divisionID) + .then(data => { + Form.populateSports(sportDropdown, data.sportID) + .then(() => { + Form.populateGenders(genderDropdown, sportDropdown.value, data.gender.name) + .then(() => { + Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value, game.divisionID); + }); + Form.populateTeams(team1Dropdown, sportDropdown.value, game.team1ID); + Form.populateTeams(team2Dropdown, sportDropdown.value, game.team2ID); + }); + }); + dateInput.value = game.date; + team1ScoreTextbox.value = game.team1Score; + team2ScoreTextbox.value = game.team2Score; + } + else { + Form.populateSeasons(seasonDropdown); + Form.populateSports(sportDropdown) + .then(() => { + Form.populateGenders(genderDropdown, sportDropdown.value) + .then(() => { + Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); + }); + Form.populateTeams(team1Dropdown, sportDropdown.value); + Form.populateTeams(team2Dropdown, sportDropdown.value); + }); + dateInput.value = (new Date()).toISOString().slice(0,10); + } + seasonDropdown.disabled = false; + sportDropdown.disabled = false; + genderDropdown.disabled = false; + divisionDropdown.disabled = false; + dateInput.disabled = false; + team1Dropdown.disabled = false; + team2Dropdown.disabled = false; + team1ScoreTextbox.disabled = false; + team2ScoreTextbox.disabled = false; + submitButton.disabled = false; +} +initializeForm(); + +Form.addRemoveFunction(deleteButton, submissionForm, "game"); diff --git a/routes/data.js b/routes/data.js index 917b36b..6ad845a 100644 --- a/routes/data.js +++ b/routes/data.js @@ -56,4 +56,9 @@ router.get('/games', function(req, res, next) { .then(data => res.json(data)); }) +router.get('/game', function(req, res, next) { + games.getFromID(req.query.game) + .then(data => res.json(data)); +}) + module.exports = router; \ No newline at end of file diff --git a/routes/manage.js b/routes/manage.js index 4114790..e7c77ed 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -13,11 +13,13 @@ router.get('/', function(req, res, next) { res.render('manage', { title: 'Score Management' }); }); -router.get('/addgame', function(req, res, next) { - res.render('manage/addgame', { title: 'Submit Score' }); +router.get('/game', function(req, res, next) { + let title = req.query.game ? 'Edit Game' : 'Submit Score' + + res.render('manage/addgame', { title }); }); -router.post('/submitgame', function(req, res, next) { +router.post('/game', function(req, res, next) { const seasonID = req.body['year']; const sportID = req.body['sport']; const gender = (req.body['gender'] == "female") ? genders.FEMALE : genders.MALE; @@ -28,8 +30,15 @@ router.post('/submitgame', function(req, res, next) { const team2ID = req.body['team2']; const team2Score = req.body['team2-score']; - games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) - .then(res.send("SUCCESS")); + 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) + .then(res.redirect('/manage')); + else games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) + .then(res.redirect("/manage")); }); router.get('/addseason', function(req, res, next) { diff --git a/views/manage/addgame.pug b/views/manage/addgame.pug index 04026f4..039bfd1 100644 --- a/views/manage/addgame.pug +++ b/views/manage/addgame.pug @@ -6,34 +6,36 @@ block stylesheets block content div#mobile-view - h1 Submit Score - form(action='./submitgame', method='POST') + h1 #{title} + form#submission-form(action='./submitgame', method='POST') span(class='form-section') label Year span(class='form-section-input') - select#year-dropdown(name="year" class="form-main-dropdown") + select#year-dropdown(name="year" class="form-main-dropdown" disabled) span(class='form-section') label Sport span(class='form-section-input') - select#sport-dropdown(name="sport" class="form-main-dropdown") - select#gender-dropdown(name="gender") - select#division-dropdown(name="division") + select#sport-dropdown(name="sport" class="form-main-dropdown" disabled) + select#gender-dropdown(name="gender" disabled) + select#division-dropdown(name="division" disabled) span(class='form-section') label Date of match span(class='form-section-input') - input(type="date", name="date", value=date) + input#date-input(type="date", name="date" value=date disabled) span(class='form-section') label Your team span(class='form-section-input') - select#team1-dropdown(name="team1" class="form-main-dropdown") - input(class="form-score-input", type="number", name="team1-score", value="0") + select#team1-dropdown(name="team1" class="form-main-dropdown" disabled) + input#team1-score-textbox(class="form-score-input", type="number", name="team1-score", value="0" disabled) span(class='form-section') label Opponent span(class='form-section-input') - select#team2-dropdown(name="team2" class="form-main-dropdown") - input(class="form-score-input", type="number", name="team2-score", value="0") + select#team2-dropdown(name="team2" class="form-main-dropdown" disabled) + input#team2-score-textbox(class="form-score-input", type="number", name="team2-score", value="0" disabled) span(class='form-section') - button#submit-button(type="submit") Submit + button#submit-button(type="submit" disabled) Submit + span(class='form-section') + button#delete-button(disabled) Delete block scripts - script(src='/scripts/submit.js' type="module") \ No newline at end of file + script(src='/scripts/manage/game.js' type="module") \ No newline at end of file diff --git a/views/manage/addseason.pug b/views/manage/addseason.pug index 96d4080..6539a76 100644 --- a/views/manage/addseason.pug +++ b/views/manage/addseason.pug @@ -16,4 +16,4 @@ block content button#submit-button(type="submit") Submit block scripts - script(src='/scripts/season.js' type="module") \ No newline at end of file + script(src='/scripts/manage/season.js' type="module") \ No newline at end of file From bd0ae3bdfd0246a504477c6022094553e869bbd7 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Tue, 23 Nov 2021 14:48:19 -0700 Subject: [PATCH 40/41] Add ability to delete seasons --- public/scripts/manage.js | 24 ++++++++++++++++++++++-- routes/manage.js | 11 +++++++---- views/manage/addseason.pug | 2 +- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/public/scripts/manage.js b/public/scripts/manage.js index 27ad375..8c89bfd 100644 --- a/public/scripts/manage.js +++ b/public/scripts/manage.js @@ -50,10 +50,30 @@ CATEGORIES.push(new Category( row.appendChild(spacerCell); }, async function addSeason() { - window.location.href = "/manage/addseason"; + window.location.href = "/manage/season"; }, - async function editSeason() { + async function editSeason(id) { + const verified = confirm(`This season will be removed.`); + + if(verified) { + const form = document.createElement('form'); + form.action = "/manage/season"; + form.method = "POST"; + form.style.visibility = "hidden"; + itemsListTable.appendChild(form); + const remove = document.createElement('input'); + remove.setAttribute('name', 'remove'); + remove.setAttribute('value', 1); + form.appendChild(remove); + + const seasonID = document.createElement('input'); + seasonID.setAttribute('name', 'season'); + seasonID.setAttribute('value', id); + form.appendChild(seasonID); + + form.submit(); + } } )); diff --git a/routes/manage.js b/routes/manage.js index e7c77ed..9757e69 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -41,15 +41,18 @@ router.post('/game', function(req, res, next) { .then(res.redirect("/manage")); }); -router.get('/addseason', function(req, res, next) { +router.get('/season', function(req, res, next) { res.render('manage/addseason', { title: 'Add Season', currentYear : (new Date()).getFullYear() }); }); -router.post('/submitseason', function(req, res, next) { +router.post('/season', function(req, res, next) { const year = req.body['year']; - seasons.add(year) - .then(res.send("SUCCESS")); + 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")); }); router.get('/sport', function(req, res, next) { diff --git a/views/manage/addseason.pug b/views/manage/addseason.pug index 6539a76..bdb13fd 100644 --- a/views/manage/addseason.pug +++ b/views/manage/addseason.pug @@ -7,7 +7,7 @@ block stylesheets block content div#mobile-view h1 #{title} - form(action='./submitseason', method='POST') + form(action='./season', method='POST') span(class='form-section') label Ending year span(class='form-section-input') From fb63ef6b416c148e3b1d37a5ed009fd44c99dfc8 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Tue, 23 Nov 2021 14:48:30 -0700 Subject: [PATCH 41/41] Add buttons to access add score and management pages --- public/scripts/index.js | 13 ++++++++++++- public/stylesheets/index.css | 11 +++++++++++ views/index.pug | 6 +++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/public/scripts/index.js b/public/scripts/index.js index d752ff1..843ed4b 100644 --- a/public/scripts/index.js +++ b/public/scripts/index.js @@ -8,6 +8,8 @@ const teamDropdown = document.getElementById('team-dropdown'); const gamesTable = document.getElementById('games-table'); const gamesTableHeader = document.getElementById('games-table-header'); const noScoresMessage = document.getElementById('no-scores-message'); +const addScoreButton = document.getElementById('add-score-button'); +const manageButton = document.getElementById('manage-button'); @@ -173,4 +175,13 @@ sportDropdown.onchange = (() => { }); genderDropdown.onchange = listDivisions; teamDropdown.onchange = listGames; -seasonDropdown.onchange = listGames; \ No newline at end of file +seasonDropdown.onchange = listGames; + + +addScoreButton.addEventListener('click', () => { + window.location.href = '/manage/game'; +}); + +manageButton.addEventListener('click', () => { + window.location.href = '/manage' +}); \ No newline at end of file diff --git a/public/stylesheets/index.css b/public/stylesheets/index.css index 2f8f252..7a60f19 100644 --- a/public/stylesheets/index.css +++ b/public/stylesheets/index.css @@ -18,4 +18,15 @@ th { tr { height: 3em; +} + +#header-div { + display: flex; + flex-direction: row; +} + +#actions-div { + display: flex; + flex-direction: column; + margin-left: auto; } \ No newline at end of file diff --git a/views/index.pug b/views/index.pug index 2e52919..674d874 100644 --- a/views/index.pug +++ b/views/index.pug @@ -6,7 +6,11 @@ block stylesheets block content div#mobile-view - h1 Score Tracker + div#header-div + h1 Score Tracker + div#actions-div + button#add-score-button + + button#manage-button Manage div span(class='form-section') label Year