Add retrieve all teams in database functionality
parent
28b11b7266
commit
90ef696b71
|
@ -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;
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
})
|
||||
|
||||
|
|
Reference in New Issue