diff --git a/database/scores/divisions.js b/database/scores/divisions.js index 38fa8be..80fe35e 100644 --- a/database/scores/divisions.js +++ b/database/scores/divisions.js @@ -6,9 +6,11 @@ const genders = require('./genders'); class Division { - constructor(id, name) { + constructor(id, name, gender, sportID) { this.id = id; this.name = name; + this.gender = gender; + this.sportID = sportID; } } @@ -29,7 +31,7 @@ async function add(name, gender, sportID) { return new Division(id, name); } -async function rename(id, division) { +async function rename(id, name) { const query = `UPDATE scores.divisions SET division_name = $2 WHERE division_id = $1;`; @@ -45,17 +47,29 @@ async function remove(id) { return new Division(id, name); } -async function retrieveBySportAndGender(sportID, gender) { - const query = `SELECT * +async function retrieve(sportID = undefined, gender = undefined) { + let table; + + if(sportID && gender) { + const query = `SELECT division_id, division_name, gender, sport_id FROM scores.divisions WHERE sport_id = $1 AND gender = $2 ORDER BY division_name;`; - const genderID = getGenderID(gender); - const table = await database.executeQuery(query, [sportID, genderID]); + const genderID = getGenderID(gender); + table = await database.executeQuery(query, [sportID, genderID]); + } + else { + const query = `SELECT division_id, division_name, gender, sport_id + FROM scores.divisions + ORDER BY division_name, + gender;`; + table = await database.executeQuery(query); + } const divisionsList = []; table.forEach((row) => { - divisionsList.push(new Division(row[0], row[1])); + let gender = (row[2] == "F") ? genders.FEMALE : genders.MALE; + divisionsList.push(new Division(row[0], row[1], gender, row[3])); }); return divisionsList; } @@ -67,4 +81,4 @@ async function retrieveBySportAndGender(sportID, gender) { exports.add = add; exports.rename = rename; exports.remove = remove; -exports.retrieveBySportAndGender = retrieveBySportAndGender; \ No newline at end of file +exports.retrieve = retrieve; \ No newline at end of file diff --git a/routes/data.js b/routes/data.js index 4344d2d..588ed2f 100644 --- a/routes/data.js +++ b/routes/data.js @@ -24,9 +24,10 @@ router.get('/genders', function(req, res, next) { }) router.get('/divisions', function(req, res, next) { - const gender = req.query.gender == 'female' ? genders.FEMALE : genders.MALE; + let gender; + if(req.query.gender) gender = (req.query.gender == 'female' ? genders.FEMALE : genders.MALE); - divisions.retrieveBySportAndGender(req.query.sport, gender) + divisions.retrieve(req.query.sport, gender) .then(data => res.json(data)); })