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] 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