From 8b1bd3bc13d1790ffefdeade5d1c48aac57ab4d2 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 13 Nov 2021 20:47:42 -0700 Subject: [PATCH 01/53] install "pg" module --- package-lock.json | 273 +++++++++++++++++++++++++++++++++++++++++++++- package.json | 1 + 2 files changed, 268 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 006fc12..6499394 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "http-errors": "~1.6.2", "morgan": "~1.9.0", "nodemailer": "^6.6.5", + "pg": "^8.7.1", "pug": "2.0.0-beta11" }, "devDependencies": { @@ -195,6 +196,14 @@ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", "dev": true }, + "node_modules/buffer-writer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==", + "engines": { + "node": ">=4" + } + }, "node_modules/bytes": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", @@ -965,6 +974,11 @@ "wrappy": "1" } }, + "node_modules/packet-reader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" + }, "node_modules/parseurl": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz", @@ -992,6 +1006,115 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "node_modules/pg": { + "version": "8.7.1", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.7.1.tgz", + "integrity": "sha512-7bdYcv7V6U3KAtWjpQJJBww0UEsWuh4yQ/EjNf2HeO/NnvKjpvhEIe/A/TleP6wtmSKnUnghs5A9jUoK6iDdkA==", + "dependencies": { + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "^2.5.0", + "pg-pool": "^3.4.1", + "pg-protocol": "^1.5.0", + "pg-types": "^2.1.0", + "pgpass": "1.x" + }, + "engines": { + "node": ">= 8.0.0" + }, + "peerDependencies": { + "pg-native": ">=2.0.0" + }, + "peerDependenciesMeta": { + "pg-native": { + "optional": true + } + } + }, + "node_modules/pg-connection-string": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.5.0.tgz", + "integrity": "sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ==" + }, + "node_modules/pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/pg-pool": { + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.4.1.tgz", + "integrity": "sha512-TVHxR/gf3MeJRvchgNHxsYsTCHQ+4wm3VIHSS19z8NC0+gioEhq1okDY1sm/TYbfoP6JLFx01s0ShvZ3puP/iQ==", + "peerDependencies": { + "pg": ">=8.0" + } + }, + "node_modules/pg-protocol": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.5.0.tgz", + "integrity": "sha512-muRttij7H8TqRNu/DxrAJQITO4Ac7RmX3Klyr/9mJEOBeIpgnF8f9jAfRz5d3XwQZl5qBjF9gLsUtMPJE0vezQ==" + }, + "node_modules/pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "dependencies": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/pgpass": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.4.tgz", + "integrity": "sha512-YmuA56alyBq7M59vxVBfPJrGSozru8QAdoNlWuW3cz8l+UX3cWge0vTvjKhsSHSJpo3Bom8/Mm6hf0TR5GY0+w==", + "dependencies": { + "split2": "^3.1.1" + } + }, + "node_modules/postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==", + "engines": { + "node": ">=4" + } + }, + "node_modules/postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "dependencies": { + "xtend": "^4.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/process-nextick-args": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", @@ -1294,6 +1417,27 @@ "node": ">=0.8.0" } }, + "node_modules/split2": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/split2/-/split2-3.2.2.tgz", + "integrity": "sha512-9NThjpgZnifTkJpzTZ7Eue85S49QwpNhZTq6GRJwObb6jnLFNGB7Qm73V5HewTROPyxD0C29xqmaI68bQtV+hg==", + "dependencies": { + "readable-stream": "^3.0.0" + } + }, + "node_modules/split2/node_modules/readable-stream": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", + "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/statuses": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", @@ -1306,7 +1450,6 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", - "dev": true, "dependencies": { "safe-buffer": "~5.1.0" } @@ -1434,8 +1577,7 @@ "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", - "dev": true + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, "node_modules/utils-merge": { "version": "1.0.1", @@ -1492,6 +1634,14 @@ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "dev": true }, + "node_modules/xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "engines": { + "node": ">=0.4" + } + }, "node_modules/yargs": { "version": "3.10.0", "resolved": "https://registry.npmjs.org/yargs/-/yargs-3.10.0.tgz", @@ -1650,6 +1800,11 @@ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", "dev": true }, + "buffer-writer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" + }, "bytes": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", @@ -2258,6 +2413,11 @@ "wrappy": "1" } }, + "packet-reader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" + }, "parseurl": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz", @@ -2279,6 +2439,84 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "pg": { + "version": "8.7.1", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.7.1.tgz", + "integrity": "sha512-7bdYcv7V6U3KAtWjpQJJBww0UEsWuh4yQ/EjNf2HeO/NnvKjpvhEIe/A/TleP6wtmSKnUnghs5A9jUoK6iDdkA==", + "requires": { + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "^2.5.0", + "pg-pool": "^3.4.1", + "pg-protocol": "^1.5.0", + "pg-types": "^2.1.0", + "pgpass": "1.x" + } + }, + "pg-connection-string": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.5.0.tgz", + "integrity": "sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ==" + }, + "pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" + }, + "pg-pool": { + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.4.1.tgz", + "integrity": "sha512-TVHxR/gf3MeJRvchgNHxsYsTCHQ+4wm3VIHSS19z8NC0+gioEhq1okDY1sm/TYbfoP6JLFx01s0ShvZ3puP/iQ==", + "requires": {} + }, + "pg-protocol": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.5.0.tgz", + "integrity": "sha512-muRttij7H8TqRNu/DxrAJQITO4Ac7RmX3Klyr/9mJEOBeIpgnF8f9jAfRz5d3XwQZl5qBjF9gLsUtMPJE0vezQ==" + }, + "pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "requires": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + } + }, + "pgpass": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.4.tgz", + "integrity": "sha512-YmuA56alyBq7M59vxVBfPJrGSozru8QAdoNlWuW3cz8l+UX3cWge0vTvjKhsSHSJpo3Bom8/Mm6hf0TR5GY0+w==", + "requires": { + "split2": "^3.1.1" + } + }, + "postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==" + }, + "postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" + }, + "postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==" + }, + "postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "requires": { + "xtend": "^4.0.0" + } + }, "process-nextick-args": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", @@ -2550,6 +2788,26 @@ "amdefine": ">=0.0.4" } }, + "split2": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/split2/-/split2-3.2.2.tgz", + "integrity": "sha512-9NThjpgZnifTkJpzTZ7Eue85S49QwpNhZTq6GRJwObb6jnLFNGB7Qm73V5HewTROPyxD0C29xqmaI68bQtV+hg==", + "requires": { + "readable-stream": "^3.0.0" + }, + "dependencies": { + "readable-stream": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", + "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + } + } + }, "statuses": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", @@ -2559,7 +2817,6 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", - "dev": true, "requires": { "safe-buffer": "~5.1.0" } @@ -2662,8 +2919,7 @@ "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", - "dev": true + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, "utils-merge": { "version": "1.0.1", @@ -2705,6 +2961,11 @@ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "dev": true }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + }, "yargs": { "version": "3.10.0", "resolved": "https://registry.npmjs.org/yargs/-/yargs-3.10.0.tgz", diff --git a/package.json b/package.json index 0a035a8..8fbe5ba 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "http-errors": "~1.6.2", "morgan": "~1.9.0", "nodemailer": "^6.6.5", + "pg": "^8.7.1", "pug": "2.0.0-beta11" }, "devDependencies": { From 95d0979f53e82219f168573d9bfb9bd1fd5f453b Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 15 Nov 2021 10:39:03 -0700 Subject: [PATCH 02/53] Begin adding support for PostgreSQL database storage --- database/database.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 database/database.js diff --git a/database/database.js b/database/database.js new file mode 100644 index 0000000..e653b9a --- /dev/null +++ b/database/database.js @@ -0,0 +1,9 @@ +const app = require('../app'); +const { Client } = require('pg'); + +if (process.env.NODE_ENV !== 'production' || process.env.NODE_ENV !== 'testing') { + require('dotenv').config(); +} + +const client = new Client(); +client.connect(); \ No newline at end of file From 80632bf8b37b359f2a5e344b932796f91a8df0a8 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 15 Nov 2021 17:34:05 -0700 Subject: [PATCH 03/53] Add PG variables to .env.example --- .env.example | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.env.example b/.env.example index 9ef5217..11d1e69 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,11 @@ NODE_ENV=development +PGUSER=dbuser +PGHOST=database.server.com +PGPASSWORD=dbuserpassword +PGDATABASE=mydatabase +PGPORT=5432 + MAIL_FROM=fromaddress@example.com MAIL_HOST=smtp.smtphost.net MAIL_PORT=465 From 2557a83e3763f98b39db08267b8365f7d7c3318c Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Thu, 18 Nov 2021 14:12:46 -0700 Subject: [PATCH 04/53] Add packages "async" and "passport" --- package-lock.json | 60 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 ++ 2 files changed, 62 insertions(+) diff --git a/package-lock.json b/package-lock.json index 6499394..225db7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "demo", "version": "0.0.0", "dependencies": { + "async": "^3.2.2", "cookie-parser": "~1.4.3", "debug": "~2.6.9", "dotenv": "^10.0.0", @@ -15,6 +16,7 @@ "http-errors": "~1.6.2", "morgan": "~1.9.0", "nodemailer": "^6.6.5", + "passport": "^0.5.0", "pg": "^8.7.1", "pug": "2.0.0-beta11" }, @@ -109,6 +111,11 @@ "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", "integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=" }, + "node_modules/async": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/async/-/async-3.2.2.tgz", + "integrity": "sha512-H0E+qZaDEfx/FY4t7iLRv1W2fFI6+pyCeTw1uN20AQPiwqwM6ojPxHxdLv4z8hi2DtnW9BOckSspLucW7pIE5g==" + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -987,6 +994,30 @@ "node": ">= 0.8" } }, + "node_modules/passport": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/passport/-/passport-0.5.0.tgz", + "integrity": "sha512-ln+ue5YaNDS+fes6O5PCzXKSseY5u8MYhX9H5Co4s+HfYI5oqvnHKoOORLYDUPh+8tHvrxugF2GFcUA1Q1Gqfg==", + "dependencies": { + "passport-strategy": "1.x.x", + "pause": "0.0.1" + }, + "engines": { + "node": ">= 0.4.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/jaredhanson" + } + }, + "node_modules/passport-strategy": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/passport-strategy/-/passport-strategy-1.0.0.tgz", + "integrity": "sha1-tVOaqPwiWj0a0XlHbd8ja0QPUuQ=", + "engines": { + "node": ">= 0.4.0" + } + }, "node_modules/path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", @@ -1006,6 +1037,11 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "node_modules/pause": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/pause/-/pause-0.0.1.tgz", + "integrity": "sha1-HUCLP9t2kjuVQ9lvtMnf1TXZy10=" + }, "node_modules/pg": { "version": "8.7.1", "resolved": "https://registry.npmjs.org/pg/-/pg-8.7.1.tgz", @@ -1722,6 +1758,11 @@ "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", "integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=" }, + "async": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/async/-/async-3.2.2.tgz", + "integrity": "sha512-H0E+qZaDEfx/FY4t7iLRv1W2fFI6+pyCeTw1uN20AQPiwqwM6ojPxHxdLv4z8hi2DtnW9BOckSspLucW7pIE5g==" + }, "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -2423,6 +2464,20 @@ "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz", "integrity": "sha1-/CidTtiZMRlGDBViUyYs3I3mW/M=" }, + "passport": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/passport/-/passport-0.5.0.tgz", + "integrity": "sha512-ln+ue5YaNDS+fes6O5PCzXKSseY5u8MYhX9H5Co4s+HfYI5oqvnHKoOORLYDUPh+8tHvrxugF2GFcUA1Q1Gqfg==", + "requires": { + "passport-strategy": "1.x.x", + "pause": "0.0.1" + } + }, + "passport-strategy": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/passport-strategy/-/passport-strategy-1.0.0.tgz", + "integrity": "sha1-tVOaqPwiWj0a0XlHbd8ja0QPUuQ=" + }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", @@ -2439,6 +2494,11 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "pause": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/pause/-/pause-0.0.1.tgz", + "integrity": "sha1-HUCLP9t2kjuVQ9lvtMnf1TXZy10=" + }, "pg": { "version": "8.7.1", "resolved": "https://registry.npmjs.org/pg/-/pg-8.7.1.tgz", diff --git a/package.json b/package.json index 8fbe5ba..c94c979 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "test": "mocha" }, "dependencies": { + "async": "^3.2.2", "cookie-parser": "~1.4.3", "debug": "~2.6.9", "dotenv": "^10.0.0", @@ -14,6 +15,7 @@ "http-errors": "~1.6.2", "morgan": "~1.9.0", "nodemailer": "^6.6.5", + "passport": "^0.5.0", "pg": "^8.7.1", "pug": "2.0.0-beta11" }, From 9554d54496ace8ea808c540e9e4a4b89313dcbde Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Thu, 18 Nov 2021 14:13:03 -0700 Subject: [PATCH 05/53] Add database initialization --- database/database.js | 34 +++++++++++- database/init_database.sql | 105 +++++++++++++++++++++++++++++++++++++ routes/index.js | 1 + 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 database/init_database.sql diff --git a/database/database.js b/database/database.js index e653b9a..766168d 100644 --- a/database/database.js +++ b/database/database.js @@ -1,9 +1,41 @@ const app = require('../app'); const { Client } = require('pg'); +const fs = require('fs'); if (process.env.NODE_ENV !== 'production' || process.env.NODE_ENV !== 'testing') { require('dotenv').config(); } const client = new Client(); -client.connect(); \ No newline at end of file +client.connect(); + +async function executeQuery(query, values = []) { + const result = await client.query({ + rowMode: 'array', + text: query, + values: values + }); + return result.rows; +} + +async function InitializeDatabase() { + console.log("Initializing database...") + const sql = fs.readFileSync('database/init_database.sql').toString(); + await executeQuery(sql); + console.log("Database initialized.") +} + + + + +async function checkForDatabaseInitialization() { + const query = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`; + let result = await executeQuery(query); + + const scoresSchemaExists = result.length !== 0; + + if(!scoresSchemaExists) { + InitializeDatabase(); + } +} +checkForDatabaseInitialization(); \ No newline at end of file diff --git a/database/init_database.sql b/database/init_database.sql new file mode 100644 index 0000000..5df20f8 --- /dev/null +++ b/database/init_database.sql @@ -0,0 +1,105 @@ +/* SCORE TRACKER DATABASE LAYOUT + +scores: + + sports: + *sport_id* | name | currently_active + + divisions: + *division_id* | name | gender | *sport_id* | currently_active + + teams: + *team_id* | name | ~sport_id~ | currently_active + + seasons: + *season_id* | school_year + + games: + *game_id* | ~division_id~ | ~season_id~ | date | ~team1_id~ | ~team2_id~ | team1_score | team2_score | ~submitter_id~ | updated_timestamp + + + +accounts: + + users: + *user_id* | email | salt | password_hash | role | approved + + sessions: + *session_id* | ~user_id~ | expiration_date + +*/ + + +BEGIN; + + +CREATE SCHEMA IF NOT EXISTS scores; + + +CREATE TABLE IF NOT EXISTS scores.sports( + sport_id BIGINT GENERATED ALWAYS AS IDENTITY, + sport_name TEXT UNIQUE NOT NULL, + currently_active BOOLEAN DEFAULT TRUE, + PRIMARY KEY(sport_id) + ); + +CREATE TABLE IF NOT EXISTS scores.divisions( + division_id BIGINT GENERATED ALWAYS AS IDENTITY, + division_name TEXT NOT NULL, + gender VARCHAR(1) CHECK (gender IN ( 'F', 'M' ) ), + sport_id BIGINT, + currently_active BOOLEAN DEFAULT TRUE, + PRIMARY KEY(division_id), + CONSTRAINT fk_sport + FOREIGN KEY(sport_id) + REFERENCES scores.sports(sport_id) + ); + +CREATE TABLE IF NOT EXISTS scores.teams( + team_id BIGINT GENERATED ALWAYS AS IDENTITY, + team_name TEXT NOT NULL, + sport_id BIGINT, + currently_active BOOLEAN DEFAULT TRUE, + PRIMARY KEY(team_id), + CONSTRAINT fk_sport + FOREIGN KEY(sport_id) + REFERENCES scores.sports(sport_id) + ); + +CREATE TABLE IF NOT EXISTS scores.seasons( + season_id BIGINT GENERATED ALWAYS AS IDENTITY, + school_year INTEGER NOT NULL, + PRIMARY KEY(season_id) + ); + +CREATE TABLE IF NOT EXISTS scores.games( + game_id BIGINT GENERATED ALWAYS AS IDENTITY, + division_id BIGINT, + season_id BIGINT, + game_date DATE, + team1_id BIGINT, + team2_id BIGINT, + team1_score INTEGER, + team2_score INTEGER, +/* submitter_id BIGINT,*/ + updated_timestamp TIMESTAMP WITH TIME ZONE DEFAULT now(), + PRIMARY KEY(game_id), + CONSTRAINT fk_division + FOREIGN KEY(division_id) + REFERENCES scores.divisions(division_id), + CONSTRAINT fk_season + FOREIGN KEY(season_id) + REFERENCES scores.seasons(season_id), + CONSTRAINT fk_team1 + FOREIGN KEY(team1_id) + REFERENCES scores.teams(team_id), + CONSTRAINT fk_team2 + FOREIGN KEY(team2_id) + REFERENCES scores.teams(team_id) +/* CONSTRAINT fk_submitter + FOREIGN KEY(submitter_id) + REFERENCES accounts.users(user_id)*/ + ); + + +COMMIT; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 9399195..c806b1c 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,5 +1,6 @@ var express = require('express'); var router = express.Router(); +var database = require('../database/database'); /* GET home page. */ router.get('/', function(req, res, next) { From 7ceccd2928e7af8860f5dbefe8716caf88427ae0 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 19 Nov 2021 11:42:08 -0700 Subject: [PATCH 06/53] Fix database layout comment --- database/init_database.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/database/init_database.sql b/database/init_database.sql index 5df20f8..ad4f0e7 100644 --- a/database/init_database.sql +++ b/database/init_database.sql @@ -3,19 +3,19 @@ scores: sports: - *sport_id* | name | currently_active + *sport_id* | sport_name | currently_active divisions: - *division_id* | name | gender | *sport_id* | currently_active + *division_id* | division_name | gender | *sport_id* | currently_active teams: - *team_id* | name | ~sport_id~ | currently_active + *team_id* | team_name | ~sport_id~ | currently_active seasons: *season_id* | school_year games: - *game_id* | ~division_id~ | ~season_id~ | date | ~team1_id~ | ~team2_id~ | team1_score | team2_score | ~submitter_id~ | updated_timestamp + *game_id* | ~division_id~ | ~season_id~ | game_date | ~team1_id~ | ~team2_id~ | team1_score | team2_score | ~submitter_id~ | updated_timestamp From cc0b5ee087dc69aac6d6943f31fe8ac15d81eee6 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 16:48:48 -0700 Subject: [PATCH 07/53] Edit submit page to better match database values --- views/index.pug | 40 ---------------------------------------- views/submit.pug | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/views/index.pug b/views/index.pug index 7f6baf1..e69de29 100644 --- a/views/index.pug +++ b/views/index.pug @@ -1,40 +0,0 @@ -extends layout - -block stylesheets - link(rel='stylesheet', href='/stylesheets/submit.css') - -block content - h1 Submit Score - form(action='/submit', method='POST') - span(class='form-section') - label Sport - span(class='form-section-input') - select#sport-dropdown(name="sport") - option(value="Football" selected) Football - select#gender-dropdown(name="gender") - option(value="Male" selected) Male - option(value="Female") Female - select#division-dropdown(name="division") - option(value="Varsity") Varsity - option(value="JV-A") JV-A - option(value="JV-B") JV-B - span(class='form-section') - label Home Team - span(class='form-section-input') - input(type="text", name="home-team") - input(class="score-input", type="number", name="home-team-score", value="0") - span(class='form-section') - label Visiting Team - span(class='form-section-input') - input(type="text", name="visiting-team") - input(class="score-input", type="number", name="visiting-team-score", value="0") - span(class='form-section') - label Submitter - span(class='form-section-input') - input(type="text", name="submitter") - span(class='form-section') - label Send info to - span(class='form-section-input') - input(type="email", name="email", placeholder="email@example.com") - span(class='form-section') - button(type="submit") Submit diff --git a/views/submit.pug b/views/submit.pug index e69de29..385ad51 100644 --- a/views/submit.pug +++ b/views/submit.pug @@ -0,0 +1,41 @@ +extends layout + +block stylesheets + link(rel='stylesheet', href='/stylesheets/submit.css') + +block content + h1 Submit Score + form(action='/submit', method='POST') + span(class='form-section') + label Year + span(class='form-section-input') + select#year-dropdown(name="year" class="main-dropdown") + option(value="2022" selected) 2021-2022 + span(class='form-section') + label Sport + span(class='form-section-input') + select#sport-dropdown(name="sport" class="main-dropdown") + option(value="Basketball" selected) Basketball + select#gender-dropdown(name="gender") + option(value="Male" selected) Male + option(value="Female") Female + select#division-dropdown(name="division") + option(value="Varsity") Varsity + option(value="JV-A") JV-A + option(value="JV-B") JV-B + span(class='form-section') + label Date of match + span(class='form-section-input') + input(type="date", name="date", value=date) + span(class='form-section') + label Your team + span(class='form-section-input') + select#team1-dropdown(name="team1" class="main-dropdown") + input(class="score-input", type="number", name="team1-score", value="0") + span(class='form-section') + label Opponent + span(class='form-section-input') + select#team2-dropdown(name="team2" class="main-dropdown") + input(class="score-input", type="number", name="team2-score", value="0") + span(class='form-section') + button(type="submit") Submit From 93521dc431e155ee1c7f220fa607f9103324cbc5 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 16:50:23 -0700 Subject: [PATCH 08/53] Edit CSS for submit page --- public/stylesheets/submit.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/stylesheets/submit.css b/public/stylesheets/submit.css index 89dd41c..80a8146 100644 --- a/public/stylesheets/submit.css +++ b/public/stylesheets/submit.css @@ -16,7 +16,7 @@ span { } -#sport-dropdown { +.main-dropdown { width: 100%; } From 82137290098bc3da97c9d8db8c384c4c4e8955c3 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 16:51:08 -0700 Subject: [PATCH 09/53] Remove mail functions for submit page --- routes/submit.js | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/routes/submit.js b/routes/submit.js index 30901be..e79ff92 100644 --- a/routes/submit.js +++ b/routes/submit.js @@ -1,38 +1,31 @@ var express = require('express'); -var mail = require('../mail/mail'); var router = express.Router(); +var database = require('../database/database'); /* GET submit page. */ router.get('/', function(req, res, next) { - res.send('Nothing to send'); + const date_ob = new Date(); + const date = ("0" + date_ob.getDate()).slice(-2); + const month = ("0" + (date_ob.getMonth() + 1)).slice(-2); + const year = date_ob.getFullYear(); + + const currentDate = year + '-' + month + '-' + date; + res.render('submit', { title: 'Submit Score', date: currentDate }); }); /* POST submit page. */ router.post('/', function(req, res, next) { - let sport = req.body.sport; - let gender = req.body.gender; - let division = req.body.division; - let home = req.body['home-team']; - let homeScore = req.body['home-team-score']; - let visiting = req.body['visiting-team']; - let visitingScore = req.body['visiting-team-score']; - let submitter = req.body['submitter']; - let recipient = req.body['email']; + const year = req.body['year']; + const sport = req.body['sport']; + const gender = req.body['gender']; + const division = req.body['division']; + const date = req.body['date']; + const team1 = req.body['team1']; + const team1Score = req.body['team1-score']; + const team2 = req.body['team2']; + const team2Score = req.body['team2-score']; - let message = prepMailBody(sport, gender, division, home, homeScore, visiting, visitingScore, submitter); - mail.send(recipient, "Score Report", message); - - res.send('Score sent'); }); -var prepMailBody = function(sport, gender, division, home, homeScore, visiting, visitingScore, submitter) { - return( - "Score report from " + submitter + "

" + - "Sport:
" + sport + " - " + gender + " - " + division + "

" + - "Home team:
" + home + " - " + homeScore + "

" + - "Visiting team:
" + visiting + " - " + visitingScore + "

"); -}; - - module.exports = router; From 6f4a41fbd7e19b6ae7428b39406840e37fa41c5a Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 16:51:49 -0700 Subject: [PATCH 10/53] Edit database.js --- database/database.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/database/database.js b/database/database.js index 766168d..f166980 100644 --- a/database/database.js +++ b/database/database.js @@ -18,7 +18,7 @@ async function executeQuery(query, values = []) { return result.rows; } -async function InitializeDatabase() { +async function Initialize() { console.log("Initializing database...") const sql = fs.readFileSync('database/init_database.sql').toString(); await executeQuery(sql); @@ -35,7 +35,13 @@ async function checkForDatabaseInitialization() { const scoresSchemaExists = result.length !== 0; if(!scoresSchemaExists) { - InitializeDatabase(); + Initialize(); } } -checkForDatabaseInitialization(); \ No newline at end of file +checkForDatabaseInitialization(); + + + + + +exports.executeQuery = executeQuery; \ No newline at end of file From 64f4f9fb32dcee891a822e6df9b2d5744016b5d7 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 16:53:13 -0700 Subject: [PATCH 11/53] Add createSport function --- database/scores/sports.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 database/scores/sports.js diff --git a/database/scores/sports.js b/database/scores/sports.js new file mode 100644 index 0000000..abdea6b --- /dev/null +++ b/database/scores/sports.js @@ -0,0 +1,26 @@ +const database = require('./database'); + + + +class Sport { + constructor(id) { + this.id = id; + } +} + +async function createSport(name) { + query = `INSERT INTO scores.sports(sport_name) + VALUES($1);`; + await database.executeQuery(query, [name]); + + query = `SELECT sport_id FROM scores.sports + WHERE sport_name = $1` + const sportId = await database.executeQuery(query, [name]); + console.log(sportId); + + return new Sport(sportId[0][0]); +} + + + +exports.createSport = createSport; \ No newline at end of file From 2191218fe1809c7cfb049d5ac47bb7fd30729658 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 17:52:09 -0700 Subject: [PATCH 12/53] Add rename sport rename function --- database/scores/sports.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/database/scores/sports.js b/database/scores/sports.js index abdea6b..b1be445 100644 --- a/database/scores/sports.js +++ b/database/scores/sports.js @@ -1,26 +1,35 @@ -const database = require('./database'); +const database = require('./../database'); class Sport { - constructor(id) { + constructor(id, name) { this.id = id; + this.name = name; } } -async function createSport(name) { + + +async function create(name) { query = `INSERT INTO scores.sports(sport_name) - VALUES($1);`; - await database.executeQuery(query, [name]); - - query = `SELECT sport_id FROM scores.sports - WHERE sport_name = $1` + VALUES($1) + RETURNING sport_id;`; const sportId = await database.executeQuery(query, [name]); - console.log(sportId); + return new Sport(sportId[0][0], name); +} - return new Sport(sportId[0][0]); +async function rename(id, name) { + query = `UPDATE scores.sports + SET sport_name = $2 + WHERE sport_id = $1;` + await database.executeQuery(query, [id, name]); + return new Sport(id, name); } -exports.createSport = createSport; \ No newline at end of file + + +exports.create = create; +exports.rename = rename; \ No newline at end of file From c176e279877db26d5f4066656ad7eb06b4a67fed Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 17:58:29 -0700 Subject: [PATCH 13/53] Add function to remove sport --- database/scores/sports.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/database/scores/sports.js b/database/scores/sports.js index b1be445..bd719fc 100644 --- a/database/scores/sports.js +++ b/database/scores/sports.js @@ -15,21 +15,30 @@ async function create(name) { query = `INSERT INTO scores.sports(sport_name) VALUES($1) RETURNING sport_id;`; - const sportId = await database.executeQuery(query, [name]); - return new Sport(sportId[0][0], name); + const id = (await database.executeQuery(query, [name]))[0][0]; + return new Sport(id, name); } async function rename(id, name) { query = `UPDATE scores.sports SET sport_name = $2 - WHERE sport_id = $1;` + WHERE sport_id = $1;`; await database.executeQuery(query, [id, name]); return new Sport(id, name); } +async function remove(id) { + query = `DELETE FROM scores.sports + WHERE sport_id = $1 + RETURNING sport_name;`; + name = (await database.executeQuery(query, [id]))[0][0]; + return new Sport(id, name); +} + exports.create = create; -exports.rename = rename; \ No newline at end of file +exports.rename = rename; +exports.remove = remove; \ No newline at end of file From 2d14395b61c930862c072dd66561796027fe8383 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 18:07:37 -0700 Subject: [PATCH 14/53] Add function to retrieve all sports from database --- database/scores/sports.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/database/scores/sports.js b/database/scores/sports.js index bd719fc..38f35cf 100644 --- a/database/scores/sports.js +++ b/database/scores/sports.js @@ -35,10 +35,24 @@ async function remove(id) { return new Sport(id, name); } +async function retrieveAll() { + query = `SELECT * + FROM scores.sports + ORDER BY sport_name;`; + const table = await database.executeQuery(query); + + const sportsList = []; + table.forEach((row) => { + sportsList.push(new Sport(row[0], row[1])); + }); + return sportsList; +} + exports.create = create; exports.rename = rename; -exports.remove = remove; \ No newline at end of file +exports.remove = remove; +exports.retrieveAll = retrieveAll; \ No newline at end of file From 65238e374561e7f95dcde76c0bf2144996498c6a Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 18:53:22 -0700 Subject: [PATCH 15/53] Add module for defining genders --- database/scores/genders.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 database/scores/genders.js diff --git a/database/scores/genders.js b/database/scores/genders.js new file mode 100644 index 0000000..402d58f --- /dev/null +++ b/database/scores/genders.js @@ -0,0 +1,8 @@ +class Gender { + constructor(name) { + this.name = name; + } +} + +exports.male = new Gender("male"); +exports.female = new Gender("female"); \ No newline at end of file From f86717d99f0fddbbd2c9ecb6b8bb48c61e82489b Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 19:00:06 -0700 Subject: [PATCH 16/53] Move genders definition to dedicated constants folder --- {database/scores => constants}/genders.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {database/scores => constants}/genders.js (100%) diff --git a/database/scores/genders.js b/constants/genders.js similarity index 100% rename from database/scores/genders.js rename to constants/genders.js From 2db5bc4480e69d00e2ccfc470e0b819abe6f4993 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 19:14:43 -0700 Subject: [PATCH 17/53] Create functions for database management of divisions --- database/scores/divisions.js | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 database/scores/divisions.js diff --git a/database/scores/divisions.js b/database/scores/divisions.js new file mode 100644 index 0000000..0a0e8be --- /dev/null +++ b/database/scores/divisions.js @@ -0,0 +1,40 @@ +const database = require('./../database'); +const genders = require('../../constants/genders'); + + + + + +class Division { + constructor(id, name) { + this.id = id; + this.name = name; + } +} + + + +async function create(name, gender, sportID) { + query = `INSERT INTO scores.divisions(division_name,gender,sport_id) + VALUES($1,$2,$3) + RETURNING division_id;`; + const genderID = (gender == genders.male) ? "M" : "F"; + const id = (await database.executeQuery(query, [name, genderID, sportID]))[0][0]; + return new Division(id, name); +} + +async function rename(id, division) { + query = `UPDATE scores.divisions + SET division_name = $2 + WHERE division_id = $1;`; + await database.executeQuery(query, [id, name]); + return new Division(id, name); +} + +async function remove(id) { + query = `DELETE FROM scores.divisions + WHERE division_id = $1 + RETURNING division_name;`; + name = (await database.executeQuery(query, [id]))[0][0]; + return new Division(id, name); +} \ No newline at end of file From 6e108ef975a7415a023964baab65f6c63b998253 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:25:29 -0700 Subject: [PATCH 18/53] Create function to retrieve divisions from database --- database/scores/divisions.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/database/scores/divisions.js b/database/scores/divisions.js index 0a0e8be..8c5510d 100644 --- a/database/scores/divisions.js +++ b/database/scores/divisions.js @@ -14,11 +14,17 @@ class Division { +function getGenderID(gender) { + return (gender == genders.male) ? "M" : "F"; +} + + + async function create(name, gender, sportID) { query = `INSERT INTO scores.divisions(division_name,gender,sport_id) VALUES($1,$2,$3) RETURNING division_id;`; - const genderID = (gender == genders.male) ? "M" : "F"; + const genderID = getGenderID(gender); const id = (await database.executeQuery(query, [name, genderID, sportID]))[0][0]; return new Division(id, name); } @@ -37,4 +43,19 @@ async function remove(id) { RETURNING division_name;`; name = (await database.executeQuery(query, [id]))[0][0]; return new Division(id, name); +} + +async function retrieveBySportAndGender(sportID, gender) { + query = `SELECT * + 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 divisionsList = []; + table.forEach((row) => { + divisionsList.push(new Division(row[0], row[1])); + }); + return divisionsList; } \ No newline at end of file From 9c55999ad88deea85a50cc6e0c38015a92efc43f Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:35:56 -0700 Subject: [PATCH 19/53] Add function to create teams --- database/scores/teams.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 database/scores/teams.js diff --git a/database/scores/teams.js b/database/scores/teams.js new file mode 100644 index 0000000..8d9858b --- /dev/null +++ b/database/scores/teams.js @@ -0,0 +1,22 @@ +const database = require('./../database'); + + + + + +class Team { + constructor(id, name) { + this.id = id; + this.name = name; + } +} + + + +async function create(name, sportID) { + query = `INSERT INTO scores.teams(team_name, sport_id) + VALUES($1, $2) + RETURNING team_id;`; + const id = (await database.executeQuery(query, [name, sportID]))[0][0]; + return new Team(id, name); +} \ No newline at end of file From 7e07236779421710d06b8559e7a5ba4c5c1a7d1e Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:38:08 -0700 Subject: [PATCH 20/53] Add function to rename teams --- database/scores/teams.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/database/scores/teams.js b/database/scores/teams.js index 8d9858b..53450fd 100644 --- a/database/scores/teams.js +++ b/database/scores/teams.js @@ -19,4 +19,12 @@ async function create(name, sportID) { RETURNING team_id;`; const id = (await database.executeQuery(query, [name, sportID]))[0][0]; return new Team(id, name); +} + +async function rename(id, name) { + query = `UPDATE scores.teams + SET team_name = $2 + WHERE team_id = $1;`; + await database.executeQuery(query, [id, name]); + return new Team(id, name); } \ No newline at end of file From 16c90cf8839d697f11b6c2a0c62cfd9e81fbe1e7 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:39:30 -0700 Subject: [PATCH 21/53] Add function to remove teams --- database/scores/teams.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/database/scores/teams.js b/database/scores/teams.js index 53450fd..3e09496 100644 --- a/database/scores/teams.js +++ b/database/scores/teams.js @@ -27,4 +27,12 @@ async function rename(id, name) { WHERE team_id = $1;`; await database.executeQuery(query, [id, name]); return new Team(id, name); +} + +async function remove(id) { + query = `DELETE FROM scores.teams + WHERE team_id = $1 + RETURNING team_name;`; + name = (await database.executeQuery(query, [id]))[0][0]; + return new Team(id, name); } \ No newline at end of file From e37b10596e7f45668bf5a7235eb512947353414e Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:43:19 -0700 Subject: [PATCH 22/53] Add function to retrieve teams by sport --- database/scores/teams.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/database/scores/teams.js b/database/scores/teams.js index 3e09496..fe27d6b 100644 --- a/database/scores/teams.js +++ b/database/scores/teams.js @@ -35,4 +35,18 @@ async function remove(id) { RETURNING team_name;`; name = (await database.executeQuery(query, [id]))[0][0]; return new Team(id, name); +} + +async function retrieveBySport(sportID) { + query = `SELECT * + FROM scores.teams + WHERE sport_id = $1 + ORDER BY team_name;`; + const table = await database.executeQuery(query); + + const teamsList = []; + table.forEach((row) => { + teamsList.push(new Team(row[0], row[1])); + }); + return teamsList; } \ No newline at end of file From e056743022059243f6545985748eb1e79f51f4ca Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:45:28 -0700 Subject: [PATCH 23/53] Add "const" to accidental global variables --- database/scores/divisions.js | 10 +++++----- database/scores/sports.js | 10 +++++----- database/scores/teams.js | 10 +++++----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/database/scores/divisions.js b/database/scores/divisions.js index 8c5510d..9003da9 100644 --- a/database/scores/divisions.js +++ b/database/scores/divisions.js @@ -21,7 +21,7 @@ function getGenderID(gender) { async function create(name, gender, sportID) { - query = `INSERT INTO scores.divisions(division_name,gender,sport_id) + const query = `INSERT INTO scores.divisions(division_name,gender,sport_id) VALUES($1,$2,$3) RETURNING division_id;`; const genderID = getGenderID(gender); @@ -30,7 +30,7 @@ async function create(name, gender, sportID) { } async function rename(id, division) { - query = `UPDATE scores.divisions + const query = `UPDATE scores.divisions SET division_name = $2 WHERE division_id = $1;`; await database.executeQuery(query, [id, name]); @@ -38,15 +38,15 @@ async function rename(id, division) { } async function remove(id) { - query = `DELETE FROM scores.divisions + const query = `DELETE FROM scores.divisions WHERE division_id = $1 RETURNING division_name;`; - name = (await database.executeQuery(query, [id]))[0][0]; + const name = (await database.executeQuery(query, [id]))[0][0]; return new Division(id, name); } async function retrieveBySportAndGender(sportID, gender) { - query = `SELECT * + const query = `SELECT * FROM scores.divisions WHERE sport_id = $1 AND gender = $2 ORDER BY division_name;`; diff --git a/database/scores/sports.js b/database/scores/sports.js index 38f35cf..c801612 100644 --- a/database/scores/sports.js +++ b/database/scores/sports.js @@ -12,7 +12,7 @@ class Sport { async function create(name) { - query = `INSERT INTO scores.sports(sport_name) + const query = `INSERT INTO scores.sports(sport_name) VALUES($1) RETURNING sport_id;`; const id = (await database.executeQuery(query, [name]))[0][0]; @@ -20,7 +20,7 @@ async function create(name) { } async function rename(id, name) { - query = `UPDATE scores.sports + const query = `UPDATE scores.sports SET sport_name = $2 WHERE sport_id = $1;`; await database.executeQuery(query, [id, name]); @@ -28,15 +28,15 @@ async function rename(id, name) { } async function remove(id) { - query = `DELETE FROM scores.sports + const query = `DELETE FROM scores.sports WHERE sport_id = $1 RETURNING sport_name;`; - name = (await database.executeQuery(query, [id]))[0][0]; + const name = (await database.executeQuery(query, [id]))[0][0]; return new Sport(id, name); } async function retrieveAll() { - query = `SELECT * + const query = `SELECT * FROM scores.sports ORDER BY sport_name;`; const table = await database.executeQuery(query); diff --git a/database/scores/teams.js b/database/scores/teams.js index fe27d6b..0bdb33d 100644 --- a/database/scores/teams.js +++ b/database/scores/teams.js @@ -14,7 +14,7 @@ class Team { async function create(name, sportID) { - query = `INSERT INTO scores.teams(team_name, sport_id) + const query = `INSERT INTO scores.teams(team_name, sport_id) VALUES($1, $2) RETURNING team_id;`; const id = (await database.executeQuery(query, [name, sportID]))[0][0]; @@ -22,7 +22,7 @@ async function create(name, sportID) { } async function rename(id, name) { - query = `UPDATE scores.teams + const query = `UPDATE scores.teams SET team_name = $2 WHERE team_id = $1;`; await database.executeQuery(query, [id, name]); @@ -30,15 +30,15 @@ async function rename(id, name) { } async function remove(id) { - query = `DELETE FROM scores.teams + const query = `DELETE FROM scores.teams WHERE team_id = $1 RETURNING team_name;`; - name = (await database.executeQuery(query, [id]))[0][0]; + const name = (await database.executeQuery(query, [id]))[0][0]; return new Team(id, name); } async function retrieveBySport(sportID) { - query = `SELECT * + const query = `SELECT * FROM scores.teams WHERE sport_id = $1 ORDER BY team_name;`; From c2e3c95cfdaa65764f67fdee49c3ec79fd51df14 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:46:28 -0700 Subject: [PATCH 24/53] Add module exports to divisions.js --- database/scores/divisions.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/database/scores/divisions.js b/database/scores/divisions.js index 9003da9..6f0a443 100644 --- a/database/scores/divisions.js +++ b/database/scores/divisions.js @@ -58,4 +58,13 @@ async function retrieveBySportAndGender(sportID, gender) { divisionsList.push(new Division(row[0], row[1])); }); return divisionsList; -} \ No newline at end of file +} + + + + + +exports.create = create; +exports.rename = rename; +exports.remove = remove; +exports.retrieveBySportAndGender = retrieveBySportAndGender; \ No newline at end of file From 90e302c3ae535ad70545f3e0edebae521eeb1f02 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:47:15 -0700 Subject: [PATCH 25/53] Add module exports to teams.js --- database/scores/teams.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/database/scores/teams.js b/database/scores/teams.js index 0bdb33d..a6dac68 100644 --- a/database/scores/teams.js +++ b/database/scores/teams.js @@ -49,4 +49,13 @@ async function retrieveBySport(sportID) { teamsList.push(new Team(row[0], row[1])); }); return teamsList; -} \ No newline at end of file +} + + + + + +exports.create = create; +exports.rename = rename; +exports.remove = remove; +exports.retrieveBySport = retrieveBySport; \ No newline at end of file From b533668445755edec9069951b212dcbe8870578b Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:52:32 -0700 Subject: [PATCH 26/53] Add function to create season --- database/scores/seasons.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 database/scores/seasons.js diff --git a/database/scores/seasons.js b/database/scores/seasons.js new file mode 100644 index 0000000..de32b2b --- /dev/null +++ b/database/scores/seasons.js @@ -0,0 +1,22 @@ +const database = require('./../database'); + + + + + +class Season { + constructor(id, year) { + this.id = id; + this.year = year; + } +} + + + +async function create(year) { + const query = `INSERT INTO scores.seasons(school_year) + VALUES($1) + RETURNING season_id;`; + const id = (await database.executeQuery(query, [year]))[0][0]; + return new Season(id, year); +} \ No newline at end of file From e9bda3c807bcf18c7f5cec6c402ddab12a0ede51 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:55:31 -0700 Subject: [PATCH 27/53] Add function to remove season --- database/scores/seasons.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/database/scores/seasons.js b/database/scores/seasons.js index de32b2b..55e9941 100644 --- a/database/scores/seasons.js +++ b/database/scores/seasons.js @@ -19,4 +19,12 @@ async function create(year) { RETURNING season_id;`; const id = (await database.executeQuery(query, [year]))[0][0]; return new Season(id, year); +} + +async function remove(id) { + const query = `DELETE FROM scores.seasons + WHERE season_id = $1 + RETURNING school_year;`; + const year = (await database.executeQuery(query, [id]))[0][0]; + return new Season(id, year); } \ No newline at end of file From c046eeb6acd42f2e17fd8698ea3e3a9cabc73f96 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 21:57:27 -0700 Subject: [PATCH 28/53] Create function to retrive seasons --- database/scores/seasons.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/database/scores/seasons.js b/database/scores/seasons.js index 55e9941..b8ff917 100644 --- a/database/scores/seasons.js +++ b/database/scores/seasons.js @@ -27,4 +27,17 @@ async function remove(id) { RETURNING school_year;`; const year = (await database.executeQuery(query, [id]))[0][0]; return new Season(id, year); +} + +async function retrieveAll() { + const query = `SELECT * + FROM scores.seasons + ORDER BY school_year;`; + const table = await database.executeQuery(query); + + const seasonsList = []; + table.forEach((row) => { + seasonsList.push(new Season(row[0], row[1])); + }); + return seasonsList; } \ No newline at end of file From 4eadf64fac3692869bcf38fb6846e867c63683d9 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 22:01:55 -0700 Subject: [PATCH 29/53] Rename "create" function to "add" in "scores" modules --- database/scores/divisions.js | 4 ++-- database/scores/seasons.js | 2 +- database/scores/sports.js | 4 ++-- database/scores/teams.js | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/database/scores/divisions.js b/database/scores/divisions.js index 6f0a443..b04d6a5 100644 --- a/database/scores/divisions.js +++ b/database/scores/divisions.js @@ -20,7 +20,7 @@ function getGenderID(gender) { -async function create(name, gender, sportID) { +async function add(name, gender, sportID) { const query = `INSERT INTO scores.divisions(division_name,gender,sport_id) VALUES($1,$2,$3) RETURNING division_id;`; @@ -64,7 +64,7 @@ async function retrieveBySportAndGender(sportID, gender) { -exports.create = create; +exports.add = add; exports.rename = rename; exports.remove = remove; exports.retrieveBySportAndGender = retrieveBySportAndGender; \ No newline at end of file diff --git a/database/scores/seasons.js b/database/scores/seasons.js index b8ff917..bd9704d 100644 --- a/database/scores/seasons.js +++ b/database/scores/seasons.js @@ -13,7 +13,7 @@ class Season { -async function create(year) { +async function add(year) { const query = `INSERT INTO scores.seasons(school_year) VALUES($1) RETURNING season_id;`; diff --git a/database/scores/sports.js b/database/scores/sports.js index c801612..14a6e45 100644 --- a/database/scores/sports.js +++ b/database/scores/sports.js @@ -11,7 +11,7 @@ class Sport { -async function create(name) { +async function add(name) { const query = `INSERT INTO scores.sports(sport_name) VALUES($1) RETURNING sport_id;`; @@ -52,7 +52,7 @@ async function retrieveAll() { -exports.create = create; +exports.add = add; exports.rename = rename; exports.remove = remove; exports.retrieveAll = retrieveAll; \ No newline at end of file diff --git a/database/scores/teams.js b/database/scores/teams.js index a6dac68..1bad3fa 100644 --- a/database/scores/teams.js +++ b/database/scores/teams.js @@ -13,7 +13,7 @@ class Team { -async function create(name, sportID) { +async function add(name, sportID) { const query = `INSERT INTO scores.teams(team_name, sport_id) VALUES($1, $2) RETURNING team_id;`; @@ -55,7 +55,7 @@ async function retrieveBySport(sportID) { -exports.create = create; +exports.add = add; exports.rename = rename; exports.remove = remove; exports.retrieveBySport = retrieveBySport; \ No newline at end of file From 5f68b24d8badf4fb03f0c402d32d14ad9bf8a436 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 22:02:20 -0700 Subject: [PATCH 30/53] Add exports to seasons.js --- database/scores/seasons.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/database/scores/seasons.js b/database/scores/seasons.js index bd9704d..381d064 100644 --- a/database/scores/seasons.js +++ b/database/scores/seasons.js @@ -40,4 +40,12 @@ async function retrieveAll() { seasonsList.push(new Season(row[0], row[1])); }); return seasonsList; -} \ No newline at end of file +} + + + + + +exports.add = add; +exports.remove = remove; +exports.retrieveAll = retrieveAll; \ No newline at end of file From acb987ffd40854aeeab1541f596eb2ebc1db28c2 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 22:14:12 -0700 Subject: [PATCH 31/53] Add function to create game --- database/scores/games.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 database/scores/games.js diff --git a/database/scores/games.js b/database/scores/games.js new file mode 100644 index 0000000..256919b --- /dev/null +++ b/database/scores/games.js @@ -0,0 +1,26 @@ +const database = require('./../database'); + + + + + +class Game { + constructor(id, date, team1ID, team2ID, team1Score, team2Score) { + this.id = id; + this.date = date; + this.team1ID = team1ID; + this.team2ID = team2ID; + this.team1Score = team1Score; + this.team2Score = team2Score; + } +} + + + +async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) { + 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 id = (await database.executeQuery(query, [divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score]))[0][0]; + return new Game(id, date, team1ID, team2ID, team1Score, team2Score); +} \ No newline at end of file From 9ca8641f207a45b2f808454719da9f30f13dafcb Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 22:20:38 -0700 Subject: [PATCH 32/53] Create function to remove games from database --- database/scores/games.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/database/scores/games.js b/database/scores/games.js index 256919b..c0ad895 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -23,4 +23,12 @@ async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, tea RETURNING game_id;`; const id = (await database.executeQuery(query, [divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score]))[0][0]; return new Game(id, date, team1ID, team2ID, team1Score, team2Score); +} + +async function remove(id) { + const query = `DELETE FROM scores.games + WHERE game_id = $1 + RETURNING * ;`; + const row = (await database.executeQuery(query, [id]))[0]; + return new Game(id, row[3], row[4], row[5], row[6], row[7]); } \ No newline at end of file From 42ccda2aba492aa788982fc879e139d4d59e5941 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 23:01:13 -0700 Subject: [PATCH 33/53] Add proper date handling functionality to "add" function of "games.js" --- database/scores/games.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/database/scores/games.js b/database/scores/games.js index c0ad895..94e017c 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -21,7 +21,14 @@ 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 id = (await database.executeQuery(query, [divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score]))[0][0]; + + const day = ("0" + date.getDate()).slice(-2); + const month = ("0" + (date.getMonth() + 1)).slice(-2); + const year = date.getFullYear(); + + const dateISO = year + '-' + month + '-' + day; + + const id = (await database.executeQuery(query, [divisionID, seasonID, dateISO, team1ID, team2ID, team1Score, team2Score]))[0][0]; return new Game(id, date, team1ID, team2ID, team1Score, team2Score); } @@ -31,4 +38,23 @@ async function remove(id) { RETURNING * ;`; const row = (await database.executeQuery(query, [id]))[0]; return new Game(id, row[3], row[4], row[5], row[6], row[7]); +} + +async function retrieveByTeamDivisionAndSeason(teamID, divisionID, seasonID) { + const query = `SELECT * + 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])); + + 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], teamID, opponentID, teamScore, opponentScore)); + }); + return gamesList; } \ No newline at end of file From 78136df5fd809fe526cd3815c562fd4e2a9da467 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sat, 20 Nov 2021 23:02:22 -0700 Subject: [PATCH 34/53] Add module exports for games.js --- database/scores/games.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/database/scores/games.js b/database/scores/games.js index 94e017c..b990a31 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -57,4 +57,12 @@ async function retrieveByTeamDivisionAndSeason(teamID, divisionID, seasonID) { gamesList.push(new Game(row[0], row[3], teamID, opponentID, teamScore, opponentScore)); }); return gamesList; -} \ No newline at end of file +} + + + + + +exports.add = add; +exports.remove = remove; +exports.retrieveByTeamDivisionAndSeason = retrieveByTeamDivisionAndSeason; \ No newline at end of file From 8c3ffcc5fada71f9bf9a957d99a0a47a0c07ea37 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 14:37:35 -0700 Subject: [PATCH 35/53] Start script to fetch data from server --- app.js | 4 +++- public/scripts/submit.js | 0 routes/data.js | 10 ++++++++++ views/layout.pug | 1 + views/submit.pug | 3 +++ 5 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 public/scripts/submit.js create mode 100644 routes/data.js diff --git a/app.js b/app.js index 3cff1ca..29f7b4f 100644 --- a/app.js +++ b/app.js @@ -6,7 +6,8 @@ var logger = require('morgan'); var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); -var submitRouter = require('./routes/submit') +var submitRouter = require('./routes/submit'); +var dataRouter = require('./routes/data'); var app = express(); @@ -23,6 +24,7 @@ app.use(express.static(path.join(__dirname, 'public'))); app.use('/', indexRouter); app.use('/users', usersRouter); app.use('/submit', submitRouter); +app.use('/data', dataRouter); // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/public/scripts/submit.js b/public/scripts/submit.js new file mode 100644 index 0000000..e69de29 diff --git a/routes/data.js b/routes/data.js new file mode 100644 index 0000000..05f5291 --- /dev/null +++ b/routes/data.js @@ -0,0 +1,10 @@ +var express = require('express'); +var router = express.Router(); +var database = require('../database/database'); + +/* GET submit page. */ +router.get('/', function(req, res, next) { + res.json( { message : "test" }); +}); + +module.exports = router; \ No newline at end of file diff --git a/views/layout.pug b/views/layout.pug index 69f5a59..e4f85eb 100644 --- a/views/layout.pug +++ b/views/layout.pug @@ -7,3 +7,4 @@ html block stylesheets body block content + block scripts diff --git a/views/submit.pug b/views/submit.pug index 385ad51..149fde7 100644 --- a/views/submit.pug +++ b/views/submit.pug @@ -39,3 +39,6 @@ block content input(class="score-input", type="number", name="team2-score", value="0") span(class='form-section') button(type="submit") Submit + +block scripts + script(src='/scripts/submit.js') \ No newline at end of file From 30ddb13ec6815007a80c4db7de6a3b2b64108fa1 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 15:30:54 -0700 Subject: [PATCH 36/53] Add function to list sports in submit form dropdown --- public/scripts/data.js | 5 +++++ public/scripts/submit.js | 20 ++++++++++++++++++++ routes/data.js | 7 ++++--- views/submit.pug | 3 +-- 4 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 public/scripts/data.js diff --git a/public/scripts/data.js b/public/scripts/data.js new file mode 100644 index 0000000..01b681b --- /dev/null +++ b/public/scripts/data.js @@ -0,0 +1,5 @@ +export async function getSports() { + const response = await fetch('/data/sports'); + const sportsList = await response.json(); + return sportsList; +} \ No newline at end of file diff --git a/public/scripts/submit.js b/public/scripts/submit.js index e69de29..dc3b2b7 100644 --- a/public/scripts/submit.js +++ b/public/scripts/submit.js @@ -0,0 +1,20 @@ +import * as Data from "./data.js"; + + +const sportDropdown = document.getElementById('sport-dropdown'); + + + + + +async function listSports() { + let 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/data.js b/routes/data.js index 05f5291..072a63e 100644 --- a/routes/data.js +++ b/routes/data.js @@ -1,10 +1,11 @@ var express = require('express'); var router = express.Router(); -var database = require('../database/database'); +var sports = require('../database/scores/sports'); /* GET submit page. */ -router.get('/', function(req, res, next) { - res.json( { message : "test" }); +router.get('/sports', function(req, res, next) { + sports.retrieveAll() + .then(data => res.json(data)); }); module.exports = router; \ No newline at end of file diff --git a/views/submit.pug b/views/submit.pug index 149fde7..25ebe71 100644 --- a/views/submit.pug +++ b/views/submit.pug @@ -15,7 +15,6 @@ block content label Sport span(class='form-section-input') select#sport-dropdown(name="sport" class="main-dropdown") - option(value="Basketball" selected) Basketball select#gender-dropdown(name="gender") option(value="Male" selected) Male option(value="Female") Female @@ -41,4 +40,4 @@ block content button(type="submit") Submit block scripts - script(src='/scripts/submit.js') \ No newline at end of file + script(src='/scripts/submit.js' type="module") \ No newline at end of file From 0d9e70896a0d9fd853a48f26cfe49f1cd7ca7467 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 15:40:48 -0700 Subject: [PATCH 37/53] Add function to list seasons in submit form --- public/scripts/data.js | 6 ++++++ public/scripts/submit.js | 17 +++++++++++++++-- routes/data.js | 8 +++++++- views/submit.pug | 1 - 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/public/scripts/data.js b/public/scripts/data.js index 01b681b..44d50a1 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -2,4 +2,10 @@ export async function getSports() { const response = await fetch('/data/sports'); const sportsList = await response.json(); return sportsList; +} + +export async function getSeasons() { + const response = await fetch('/data/seasons'); + const seasonsList = await response.json(); + return seasonsList; } \ No newline at end of file diff --git a/public/scripts/submit.js b/public/scripts/submit.js index dc3b2b7..64b62ac 100644 --- a/public/scripts/submit.js +++ b/public/scripts/submit.js @@ -2,13 +2,14 @@ import * as Data from "./data.js"; const sportDropdown = document.getElementById('sport-dropdown'); +const seasonDropdown = document.getElementById('year-dropdown'); async function listSports() { - let sportsList = await Data.getSports(); + const sportsList = await Data.getSports(); sportsList.forEach(sport => { const option = document.createElement('option'); @@ -17,4 +18,16 @@ async function listSports() { sportDropdown.appendChild(option); }); } -listSports(); \ No newline at end of file +listSports(); + +async function listSeasons() { + const seasonsList = await Data.getSeasons(); + + seasonsList.forEach(season => { + const option = document.createElement('option'); + option.text = season.year - 1 + "-" + season.year; + option.value = season.id; + seasonDropdown.appendChild(option); + }); +} +listSeasons(); \ No newline at end of file diff --git a/routes/data.js b/routes/data.js index 072a63e..aa61618 100644 --- a/routes/data.js +++ b/routes/data.js @@ -1,11 +1,17 @@ var express = require('express'); var router = express.Router(); var sports = require('../database/scores/sports'); +var seasons = require('../database/scores/seasons'); + -/* GET submit page. */ router.get('/sports', function(req, res, next) { sports.retrieveAll() .then(data => res.json(data)); }); +router.get('/seasons', function(req, res, next) { + seasons.retrieveAll() + .then(data => res.json(data)); +}) + module.exports = router; \ No newline at end of file diff --git a/views/submit.pug b/views/submit.pug index 25ebe71..5ac6ccf 100644 --- a/views/submit.pug +++ b/views/submit.pug @@ -10,7 +10,6 @@ block content label Year span(class='form-section-input') select#year-dropdown(name="year" class="main-dropdown") - option(value="2022" selected) 2021-2022 span(class='form-section') label Sport span(class='form-section-input') From c0ab9d45816ff0051a4ebdcaaa64e03d6ea19f77 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 15:52:54 -0700 Subject: [PATCH 38/53] Create module to retrieve a list of genders by sport --- database/scores/genders.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 database/scores/genders.js diff --git a/database/scores/genders.js b/database/scores/genders.js new file mode 100644 index 0000000..857190f --- /dev/null +++ b/database/scores/genders.js @@ -0,0 +1,34 @@ +const database = require('./../database'); +const genders = require('../../constants/genders'); + + + + + +function getGendersBySport(sportID) { + const query = `SELECT gender + from scores.divisions + WHERE sport_id = $1;`; + const table = await database.executeQuery(query, [sportID]); + + const gendersList = []; + + if(table.length() == 2) { + gendersList.push(genders.female); + gendersList.push(genders.male); + } + else if(table[0][0] = "F") { + gendersList.push(genders.female); + } + else if(table[0][0] = "M") { + gendersList.push(genders.male); + } + + return gendersList; +} + + + + + +exports.getGendersBySport = getGendersBySport; \ No newline at end of file From 07c4d9b6221987da465806de8f4be2cfebf52ba7 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 15:53:58 -0700 Subject: [PATCH 39/53] Retrieve seasons in descending order --- database/scores/seasons.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/scores/seasons.js b/database/scores/seasons.js index 381d064..f787ade 100644 --- a/database/scores/seasons.js +++ b/database/scores/seasons.js @@ -32,7 +32,7 @@ async function remove(id) { async function retrieveAll() { const query = `SELECT * FROM scores.seasons - ORDER BY school_year;`; + ORDER BY school_year DESC;`; const table = await database.executeQuery(query); const seasonsList = []; From a8eb7dae0a9e65fcb88f73b065e16c1d38c59bfe Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 15:57:09 -0700 Subject: [PATCH 40/53] Rename function in genders.js from "getGenderBySport" to "retrieveBySport" --- database/scores/genders.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/scores/genders.js b/database/scores/genders.js index 857190f..9e6ba44 100644 --- a/database/scores/genders.js +++ b/database/scores/genders.js @@ -5,7 +5,7 @@ const genders = require('../../constants/genders'); -function getGendersBySport(sportID) { +function retrieveBySport(sportID) { const query = `SELECT gender from scores.divisions WHERE sport_id = $1;`; @@ -31,4 +31,4 @@ function getGendersBySport(sportID) { -exports.getGendersBySport = getGendersBySport; \ No newline at end of file +exports.retrieveBySport = retrieveBySport; \ No newline at end of file From e5b17912598205daa24502fecd4468fd14f0f6b8 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 15:59:07 -0700 Subject: [PATCH 41/53] Make "retrieveBySport" function in genders.js async --- database/scores/genders.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/scores/genders.js b/database/scores/genders.js index 9e6ba44..c86221a 100644 --- a/database/scores/genders.js +++ b/database/scores/genders.js @@ -5,7 +5,7 @@ const genders = require('../../constants/genders'); -function retrieveBySport(sportID) { +async function retrieveBySport(sportID) { const query = `SELECT gender from scores.divisions WHERE sport_id = $1;`; From ff070acdf7ea9889c7e3b604f28d300acb732577 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 16:24:43 -0700 Subject: [PATCH 42/53] Add function to get genders per sport in submit form --- database/scores/genders.js | 10 +++++++--- public/scripts/data.js | 6 ++++++ public/scripts/submit.js | 23 ++++++++++++++++++++++- routes/data.js | 6 ++++++ views/submit.pug | 2 -- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/database/scores/genders.js b/database/scores/genders.js index c86221a..27e811e 100644 --- a/database/scores/genders.js +++ b/database/scores/genders.js @@ -13,18 +13,22 @@ async function retrieveBySport(sportID) { const gendersList = []; - if(table.length() == 2) { + if(table.length == 0) { + return gendersList; + } + if(table.length == 2) { gendersList.push(genders.female); gendersList.push(genders.male); + return gendersList; } else if(table[0][0] = "F") { gendersList.push(genders.female); + return gendersList; } else if(table[0][0] = "M") { gendersList.push(genders.male); + return gendersList; } - - return gendersList; } diff --git a/public/scripts/data.js b/public/scripts/data.js index 44d50a1..cccd7cf 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -8,4 +8,10 @@ export async function getSeasons() { const response = await fetch('/data/seasons'); const seasonsList = await response.json(); return seasonsList; +} + +export async function getGenders(sportID) { + const response = await fetch(`/data/genders?sport=${+sportID}`); + const gendersList = await response.json(); + return gendersList; } \ No newline at end of file diff --git a/public/scripts/submit.js b/public/scripts/submit.js index 64b62ac..e51fda6 100644 --- a/public/scripts/submit.js +++ b/public/scripts/submit.js @@ -3,6 +3,7 @@ import * as Data from "./data.js"; const sportDropdown = document.getElementById('sport-dropdown'); const seasonDropdown = document.getElementById('year-dropdown'); +const genderDropdown = document.getElementById('gender-dropdown'); @@ -30,4 +31,24 @@ async function listSeasons() { seasonDropdown.appendChild(option); }); } -listSeasons(); \ No newline at end of file +listSeasons(); + +async function listGenders() { + genderDropdown.innerHTML = ""; + + const selectedSportID = sportDropdown.value; + const gendersList = await Data.getGenders(selectedSportID); + + 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); + }); +} + + + + + +sportDropdown.onchange = listGenders; \ No newline at end of file diff --git a/routes/data.js b/routes/data.js index aa61618..8b4ea87 100644 --- a/routes/data.js +++ b/routes/data.js @@ -2,6 +2,7 @@ var express = require('express'); var router = express.Router(); var sports = require('../database/scores/sports'); var seasons = require('../database/scores/seasons'); +var genders = require('../database/scores/genders'); router.get('/sports', function(req, res, next) { @@ -14,4 +15,9 @@ router.get('/seasons', function(req, res, next) { .then(data => res.json(data)); }) +router.get('/genders', function(req, res, next) { + genders.retrieveBySport(req.query.sport) + .then(data => res.json(data)); +}) + module.exports = router; \ No newline at end of file diff --git a/views/submit.pug b/views/submit.pug index 5ac6ccf..93fdec1 100644 --- a/views/submit.pug +++ b/views/submit.pug @@ -15,8 +15,6 @@ block content span(class='form-section-input') select#sport-dropdown(name="sport" class="main-dropdown") select#gender-dropdown(name="gender") - option(value="Male" selected) Male - option(value="Female") Female select#division-dropdown(name="division") option(value="Varsity") Varsity option(value="JV-A") JV-A From bdd943d422fefbec529b5309cb587e63b41d7f46 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 16:30:36 -0700 Subject: [PATCH 43/53] Move gender constants into genders.js file in database directory --- constants/genders.js | 8 -------- database/scores/divisions.js | 4 ++-- database/scores/genders.js | 22 ++++++++++++++++------ 3 files changed, 18 insertions(+), 16 deletions(-) delete mode 100644 constants/genders.js diff --git a/constants/genders.js b/constants/genders.js deleted file mode 100644 index 402d58f..0000000 --- a/constants/genders.js +++ /dev/null @@ -1,8 +0,0 @@ -class Gender { - constructor(name) { - this.name = name; - } -} - -exports.male = new Gender("male"); -exports.female = new Gender("female"); \ No newline at end of file diff --git a/database/scores/divisions.js b/database/scores/divisions.js index b04d6a5..38fa8be 100644 --- a/database/scores/divisions.js +++ b/database/scores/divisions.js @@ -1,5 +1,5 @@ const database = require('./../database'); -const genders = require('../../constants/genders'); +const genders = require('./genders'); @@ -15,7 +15,7 @@ class Division { function getGenderID(gender) { - return (gender == genders.male) ? "M" : "F"; + return (gender == genders.MALE) ? "M" : "F"; } diff --git a/database/scores/genders.js b/database/scores/genders.js index 27e811e..126e57b 100644 --- a/database/scores/genders.js +++ b/database/scores/genders.js @@ -1,10 +1,19 @@ const database = require('./../database'); -const genders = require('../../constants/genders'); +class Gender { + constructor(name) { + this.name = name; + } +} +const MALE = new Gender("male"); +const FEMALE = new Gender("female"); + + + async function retrieveBySport(sportID) { const query = `SELECT gender from scores.divisions @@ -17,16 +26,16 @@ async function retrieveBySport(sportID) { return gendersList; } if(table.length == 2) { - gendersList.push(genders.female); - gendersList.push(genders.male); + gendersList.push(genders.FEMALE); + gendersList.push(genders.MALE); return gendersList; } else if(table[0][0] = "F") { - gendersList.push(genders.female); + gendersList.push(genders.FEMALE); return gendersList; } else if(table[0][0] = "M") { - gendersList.push(genders.male); + gendersList.push(genders.MALE); return gendersList; } } @@ -34,5 +43,6 @@ async function retrieveBySport(sportID) { - +exports.MALE = MALE; +exports.FEMALE = FEMALE; exports.retrieveBySport = retrieveBySport; \ No newline at end of file From d561a34055fc0086cd8edf2322f1bd8d52461afc Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 16:39:51 -0700 Subject: [PATCH 44/53] Fix bug in genders.js regarding constants --- database/scores/genders.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/database/scores/genders.js b/database/scores/genders.js index 126e57b..b59bdb0 100644 --- a/database/scores/genders.js +++ b/database/scores/genders.js @@ -26,16 +26,16 @@ async function retrieveBySport(sportID) { return gendersList; } if(table.length == 2) { - gendersList.push(genders.FEMALE); - gendersList.push(genders.MALE); + gendersList.push(FEMALE); + gendersList.push(MALE); return gendersList; } else if(table[0][0] = "F") { - gendersList.push(genders.FEMALE); + gendersList.push(FEMALE); return gendersList; } else if(table[0][0] = "M") { - gendersList.push(genders.MALE); + gendersList.push(MALE); return gendersList; } } From 9b7ec5f3d679fa0e13f028900dc4c1ba8e88e0e8 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 16:48:03 -0700 Subject: [PATCH 45/53] Add function to list divisions in submit form --- public/scripts/data.js | 6 ++++++ public/scripts/submit.js | 26 +++++++++++++++++++++++++- routes/data.js | 8 ++++++++ views/submit.pug | 3 --- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/public/scripts/data.js b/public/scripts/data.js index cccd7cf..27ba099 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -14,4 +14,10 @@ export async function getGenders(sportID) { const response = await fetch(`/data/genders?sport=${+sportID}`); const gendersList = await response.json(); return gendersList; +} + +export async function getDivisions(sportID, gender) { + const response = await fetch(`/data/divisions?sport=${+sportID}&gender=${gender}`); + const divisionsList = await response.json(); + return divisionsList; } \ No newline at end of file diff --git a/public/scripts/submit.js b/public/scripts/submit.js index e51fda6..85fc410 100644 --- a/public/scripts/submit.js +++ b/public/scripts/submit.js @@ -4,6 +4,7 @@ import * as Data from "./data.js"; const sportDropdown = document.getElementById('sport-dropdown'); const seasonDropdown = document.getElementById('year-dropdown'); const genderDropdown = document.getElementById('gender-dropdown'); +const divisionDropdown = document.getElementById('division-dropdown'); @@ -18,6 +19,8 @@ async function listSports() { option.value = sport.id; sportDropdown.appendChild(option); }); + + listSeasons(); } listSports(); @@ -45,10 +48,31 @@ async function listGenders() { option.value = gender.name; genderDropdown.appendChild(option); }); + + listDivisions(); +} + +async function listDivisions() { + divisionDropdown.innerHTML = ""; + + const selectedSportID = sportDropdown.value; + const selectedGender = genderDropdown.value; + + if(!selectedGender) return; + + const divisionsList = await Data.getDivisions(selectedSportID, selectedGender); + + divisionsList.forEach(division => { + const option = document.createElement('option'); + option.text = division.name; + option.value = division.id; + divisionDropdown.appendChild(option); + }) } -sportDropdown.onchange = listGenders; \ No newline at end of file +sportDropdown.onchange = listGenders; +genderDropdown.onchange = listDivisions; \ No newline at end of file diff --git a/routes/data.js b/routes/data.js index 8b4ea87..68a7e6d 100644 --- a/routes/data.js +++ b/routes/data.js @@ -3,6 +3,7 @@ var router = express.Router(); var sports = require('../database/scores/sports'); var seasons = require('../database/scores/seasons'); var genders = require('../database/scores/genders'); +var divisions = require('../database/scores/divisions'); router.get('/sports', function(req, res, next) { @@ -20,4 +21,11 @@ router.get('/genders', function(req, res, next) { .then(data => res.json(data)); }) +router.get('/divisions', function(req, res, next) { + const gender = req.body.gender == 'female' ? genders.FEMALE : genders.MALE; + + divisions.retrieveBySportAndGender(req.query.sport, gender) + .then(data => res.json(data)); +}) + module.exports = router; \ No newline at end of file diff --git a/views/submit.pug b/views/submit.pug index 93fdec1..97d7b37 100644 --- a/views/submit.pug +++ b/views/submit.pug @@ -16,9 +16,6 @@ block content select#sport-dropdown(name="sport" class="main-dropdown") select#gender-dropdown(name="gender") select#division-dropdown(name="division") - option(value="Varsity") Varsity - option(value="JV-A") JV-A - option(value="JV-B") JV-B span(class='form-section') label Date of match span(class='form-section-input') From e1acf1951ccaf49a61318af046d1507149d1ebc9 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 16:51:35 -0700 Subject: [PATCH 46/53] Fix bugs in submit form dropdowns --- public/scripts/submit.js | 64 ++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/public/scripts/submit.js b/public/scripts/submit.js index 85fc410..678fa16 100644 --- a/public/scripts/submit.js +++ b/public/scripts/submit.js @@ -10,21 +10,9 @@ const divisionDropdown = document.getElementById('division-dropdown'); -async function listSports() { - const sportsList = await Data.getSports(); - - sportsList.forEach(sport => { - const option = document.createElement('option'); - option.text = sport.name; - option.value = sport.id; - sportDropdown.appendChild(option); - }); - - listSeasons(); -} -listSports(); - async function listSeasons() { + seasonDropdown.innerHTML = ""; + const seasonsList = await Data.getSeasons(); seasonsList.forEach(season => { @@ -36,18 +24,36 @@ async function listSeasons() { } listSeasons(); +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); + }); + + listGenders(); +} +listSports(); + async function listGenders() { genderDropdown.innerHTML = ""; const selectedSportID = sportDropdown.value; const gendersList = await Data.getGenders(selectedSportID); - 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(selectedSportID) { + 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); + }); + } listDivisions(); } @@ -58,16 +64,16 @@ async function listDivisions() { const selectedSportID = sportDropdown.value; const selectedGender = genderDropdown.value; - if(!selectedGender) return; + if(selectedGender) { + const divisionsList = await Data.getDivisions(selectedSportID, selectedGender); - const divisionsList = await Data.getDivisions(selectedSportID, selectedGender); - - divisionsList.forEach(division => { - const option = document.createElement('option'); - option.text = division.name; - option.value = division.id; - divisionDropdown.appendChild(option); - }) + divisionsList.forEach(division => { + const option = document.createElement('option'); + option.text = division.name; + option.value = division.id; + divisionDropdown.appendChild(option); + }); + } } From edfd42a6232676053b0e5808e2b3591208d8c6dc Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 17:08:16 -0700 Subject: [PATCH 47/53] Fix bug in "retrieveBySport" function in teams.js --- database/scores/teams.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/scores/teams.js b/database/scores/teams.js index 1bad3fa..89f2c4c 100644 --- a/database/scores/teams.js +++ b/database/scores/teams.js @@ -42,7 +42,7 @@ async function retrieveBySport(sportID) { FROM scores.teams WHERE sport_id = $1 ORDER BY team_name;`; - const table = await database.executeQuery(query); + const table = await database.executeQuery(query, [sportID]); const teamsList = []; table.forEach((row) => { From 27d05752b59d773855dde52bc5f749be87102145 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 17:15:02 -0700 Subject: [PATCH 48/53] Add function to list teams on submit form --- public/scripts/data.js | 6 ++++++ public/scripts/submit.js | 31 ++++++++++++++++++++++++++++++- routes/data.js | 6 ++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/public/scripts/data.js b/public/scripts/data.js index 27ba099..7e21436 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -20,4 +20,10 @@ export async function getDivisions(sportID, gender) { const response = await fetch(`/data/divisions?sport=${+sportID}&gender=${gender}`); const divisionsList = await response.json(); return divisionsList; +} + +export async function getTeams(sportID) { + const response = await fetch(`/data/teams?sport=${+sportID}`); + const teamsList = await response.json(); + return teamsList; } \ No newline at end of file diff --git a/public/scripts/submit.js b/public/scripts/submit.js index 678fa16..bd4d08a 100644 --- a/public/scripts/submit.js +++ b/public/scripts/submit.js @@ -5,6 +5,8 @@ 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 team1Dropdown = document.getElementById('team1-dropdown'); +const team2Dropdown = document.getElementById('team2-dropdown'); @@ -37,6 +39,7 @@ async function listSports() { }); listGenders(); + listTeams(); } listSports(); @@ -76,9 +79,35 @@ async function listDivisions() { } } +async function listTeams() { + team1Dropdown.innerHTML = ""; + team2Dropdown.innerHTML = ""; + + const selectedSportID = sportDropdown.value; + + if(selectedSportID) { + const teamsList = await Data.getTeams(selectedSportID); + + teamsList.forEach(team => { + const optionT1 = document.createElement('option'); + optionT1.text = team.name; + optionT1.value = team.id; + team1Dropdown.appendChild(optionT1); + + const optionT2 = document.createElement('option'); + optionT2.text = team.name; + optionT2.value = team.id; + team2Dropdown.appendChild(optionT2); + }); + } +} -sportDropdown.onchange = listGenders; + +sportDropdown.onchange = (() => { + listGenders(); + listTeams(); +}); genderDropdown.onchange = listDivisions; \ No newline at end of file diff --git a/routes/data.js b/routes/data.js index 68a7e6d..fc6c947 100644 --- a/routes/data.js +++ b/routes/data.js @@ -4,6 +4,7 @@ var sports = require('../database/scores/sports'); var seasons = require('../database/scores/seasons'); var genders = require('../database/scores/genders'); var divisions = require('../database/scores/divisions'); +var teams = require('../database/scores/teams'); router.get('/sports', function(req, res, next) { @@ -28,4 +29,9 @@ router.get('/divisions', function(req, res, next) { .then(data => res.json(data)); }) +router.get('/teams', function(req, res, next) { + teams.retrieveBySport(req.query.sport) + .then(data => res.json(data)); +}) + module.exports = router; \ No newline at end of file From 7d4dcc4d82826c000b5263a0a2d6a247161ba765 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 17:20:14 -0700 Subject: [PATCH 49/53] Add moment.js --- package-lock.json | 14 ++++++++++++++ package.json | 1 + 2 files changed, 15 insertions(+) diff --git a/package-lock.json b/package-lock.json index 225db7b..821a54c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "dotenv": "^10.0.0", "express": "~4.16.0", "http-errors": "~1.6.2", + "moment": "^2.29.1", "morgan": "~1.9.0", "nodemailer": "^6.6.5", "passport": "^0.5.0", @@ -909,6 +910,14 @@ "ms": "2.0.0" } }, + "node_modules/moment": { + "version": "2.29.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", + "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==", + "engines": { + "node": "*" + } + }, "node_modules/morgan": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.0.tgz", @@ -2400,6 +2409,11 @@ } } }, + "moment": { + "version": "2.29.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", + "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" + }, "morgan": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.0.tgz", diff --git a/package.json b/package.json index c94c979..2e3323a 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "dotenv": "^10.0.0", "express": "~4.16.0", "http-errors": "~1.6.2", + "moment": "^2.29.1", "morgan": "~1.9.0", "nodemailer": "^6.6.5", "passport": "^0.5.0", From 4576b7261287dc59d5396162635cb935fab597c6 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 17:37:45 -0700 Subject: [PATCH 50/53] Switch date from native object to moment.js --- database/scores/games.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/database/scores/games.js b/database/scores/games.js index b990a31..8d49aeb 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -1,4 +1,5 @@ const database = require('./../database'); +const moment = require('moment'); @@ -22,11 +23,7 @@ async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, tea VALUES($1, $2, $3, $4, $5, $6, $7) RETURNING game_id;`; - const day = ("0" + date.getDate()).slice(-2); - const month = ("0" + (date.getMonth() + 1)).slice(-2); - const year = date.getFullYear(); - - const dateISO = year + '-' + month + '-' + day; + const dateISO = date.format(YYYY-MM-DD); const id = (await database.executeQuery(query, [divisionID, seasonID, dateISO, team1ID, team2ID, team1Score, team2Score]))[0][0]; return new Game(id, date, team1ID, team2ID, team1Score, team2Score); From 344e8b66d394515176924ac632d1a77bbb2537e0 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 17:39:47 -0700 Subject: [PATCH 51/53] Report date as moment object in list generated by "retrieveByTeamDivisionAndSeason" function in games.js --- database/scores/games.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/scores/games.js b/database/scores/games.js index 8d49aeb..1560768 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -51,7 +51,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], teamID, opponentID, teamScore, opponentScore)); + gamesList.push(new Game(row[0], moment(row[3]), teamID, opponentID, teamScore, opponentScore)); }); return gamesList; } From 49aff3f741c95c52d480cbc35d4c0bd5be5695bf Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 17:41:45 -0700 Subject: [PATCH 52/53] Fix bug regarding date formatting in games.js --- database/scores/games.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/scores/games.js b/database/scores/games.js index 1560768..96463ec 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -23,7 +23,7 @@ async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, tea VALUES($1, $2, $3, $4, $5, $6, $7) RETURNING game_id;`; - const dateISO = date.format(YYYY-MM-DD); + const dateISO = date.format('YYYY-MM-DD'); const id = (await database.executeQuery(query, [divisionID, seasonID, dateISO, team1ID, team2ID, team1Score, team2Score]))[0][0]; return new Game(id, date, team1ID, team2ID, team1Score, team2Score); From 12c39e271e63845819118fa2962e22c3878a06a9 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 17:43:34 -0700 Subject: [PATCH 53/53] Complete submit game feature --- routes/submit.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/routes/submit.js b/routes/submit.js index e79ff92..8510303 100644 --- a/routes/submit.js +++ b/routes/submit.js @@ -1,31 +1,28 @@ var express = require('express'); var router = express.Router(); -var database = require('../database/database'); +var genders = require('../database/scores/genders'); +var games = require('../database/scores/games'); +var moment = require('moment'); /* GET submit page. */ router.get('/', function(req, res, next) { - const date_ob = new Date(); - const date = ("0" + date_ob.getDate()).slice(-2); - const month = ("0" + (date_ob.getMonth() + 1)).slice(-2); - const year = date_ob.getFullYear(); - - const currentDate = year + '-' + month + '-' + date; - res.render('submit', { title: 'Submit Score', date: currentDate }); + res.render('submit', { title: 'Submit Score', date: moment().format('YYYY-MM-DD') }); }); /* POST submit page. */ router.post('/', function(req, res, next) { - const year = req.body['year']; - const sport = req.body['sport']; - const gender = req.body['gender']; - const division = req.body['division']; - const date = req.body['date']; - const team1 = req.body['team1']; + 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 team2 = req.body['team2']; + 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;