From 90ef696b717ec05e92ffd55b840b377ec44e2488 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 15:45:19 -0700 Subject: [PATCH] Add retrieve all teams in database functionality --- database/scores/teams.js | 25 +++++++++++++++++++------ public/scripts/data.js | 7 +++++-- routes/data.js | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/database/scores/teams.js b/database/scores/teams.js index 893cc34..3717841 100644 --- a/database/scores/teams.js +++ b/database/scores/teams.js @@ -5,9 +5,10 @@ const database = require('./../database'); class Team { - constructor(id, name) { + constructor(id, name, sportID) { this.id = id; this.name = name; + this.sportID = sportID; } } @@ -37,16 +38,28 @@ async function remove(id) { return new Team(id, name); } -async function retrieveBySport(sportID) { - const query = `SELECT * +async function retrieve(sportID = undefined) { + let table; + + if(sportID) { + const query = `SELECT team_id, team_name, sport_id FROM scores.teams WHERE sport_id = $1 ORDER BY team_name;`; - const table = await database.executeQuery(query, [sportID]); + table = await database.executeQuery(query, [sportID]); + } + else { + const query = `SELECT team_id, team_name, sport_id + FROM scores.teams + ORDER BY + sport_id, + team_name;`; + table = await database.executeQuery(query); + } const teamsList = []; table.forEach((row) => { - teamsList.push(new Team(row[0], row[1])); + teamsList.push(new Team(row[0], row[1], row[2])); }); return teamsList; } @@ -66,5 +79,5 @@ async function getFromID(id) { exports.add = add; exports.rename = rename; exports.remove = remove; -exports.retrieveBySport = retrieveBySport; +exports.retrieve = retrieve; exports.getFromID = getFromID; \ No newline at end of file diff --git a/public/scripts/data.js b/public/scripts/data.js index 567c1ac..b83fdd0 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -32,8 +32,11 @@ export async function getDivisions(sportID = undefined, gender = undefined) { return divisionsList; } -export async function getTeams(sportID) { - const response = await fetch(`/data/teams?sport=${+sportID}`); +export async function getTeams(sportID = undefined) { + let URL = '/data/teams?'; + if(sportID) URL += `sport=${+sportID}&`; + + const response = await fetch(URL); const teamsList = await response.json(); return teamsList; } diff --git a/routes/data.js b/routes/data.js index fc61052..f9c6431 100644 --- a/routes/data.js +++ b/routes/data.js @@ -37,7 +37,7 @@ router.get('/divisions', function(req, res, next) { }) router.get('/teams', function(req, res, next) { - teams.retrieveBySport(req.query.sport) + teams.retrieve(req.query.sport) .then(data => res.json(data)); })