Create function to retrieve divisions from database

main
sudoer777 2021-11-20 21:25:29 -07:00
parent 2db5bc4480
commit 6e108ef975
1 changed files with 22 additions and 1 deletions

View File

@ -14,11 +14,17 @@ class Division {
function getGenderID(gender) {
return (gender == genders.male) ? "M" : "F";
}
async function create(name, gender, sportID) { async function create(name, gender, sportID) {
query = `INSERT INTO scores.divisions(division_name,gender,sport_id) query = `INSERT INTO scores.divisions(division_name,gender,sport_id)
VALUES($1,$2,$3) VALUES($1,$2,$3)
RETURNING division_id;`; 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]; const id = (await database.executeQuery(query, [name, genderID, sportID]))[0][0];
return new Division(id, name); return new Division(id, name);
} }
@ -37,4 +43,19 @@ async function remove(id) {
RETURNING division_name;`; RETURNING division_name;`;
name = (await database.executeQuery(query, [id]))[0][0]; name = (await database.executeQuery(query, [id]))[0][0];
return new Division(id, name); 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;
} }