Add retrieve all teams in database functionality
parent
28b11b7266
commit
90ef696b71
|
@ -5,9 +5,10 @@ const database = require('./../database');
|
||||||
|
|
||||||
|
|
||||||
class Team {
|
class Team {
|
||||||
constructor(id, name) {
|
constructor(id, name, sportID) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.sportID = sportID;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,16 +38,28 @@ async function remove(id) {
|
||||||
return new Team(id, name);
|
return new Team(id, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function retrieveBySport(sportID) {
|
async function retrieve(sportID = undefined) {
|
||||||
const query = `SELECT *
|
let table;
|
||||||
|
|
||||||
|
if(sportID) {
|
||||||
|
const query = `SELECT team_id, team_name, sport_id
|
||||||
FROM scores.teams
|
FROM scores.teams
|
||||||
WHERE sport_id = $1
|
WHERE sport_id = $1
|
||||||
ORDER BY team_name;`;
|
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 = [];
|
const teamsList = [];
|
||||||
table.forEach((row) => {
|
table.forEach((row) => {
|
||||||
teamsList.push(new Team(row[0], row[1]));
|
teamsList.push(new Team(row[0], row[1], row[2]));
|
||||||
});
|
});
|
||||||
return teamsList;
|
return teamsList;
|
||||||
}
|
}
|
||||||
|
@ -66,5 +79,5 @@ async function getFromID(id) {
|
||||||
exports.add = add;
|
exports.add = add;
|
||||||
exports.rename = rename;
|
exports.rename = rename;
|
||||||
exports.remove = remove;
|
exports.remove = remove;
|
||||||
exports.retrieveBySport = retrieveBySport;
|
exports.retrieve = retrieve;
|
||||||
exports.getFromID = getFromID;
|
exports.getFromID = getFromID;
|
|
@ -32,8 +32,11 @@ export async function getDivisions(sportID = undefined, gender = undefined) {
|
||||||
return divisionsList;
|
return divisionsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getTeams(sportID) {
|
export async function getTeams(sportID = undefined) {
|
||||||
const response = await fetch(`/data/teams?sport=${+sportID}`);
|
let URL = '/data/teams?';
|
||||||
|
if(sportID) URL += `sport=${+sportID}&`;
|
||||||
|
|
||||||
|
const response = await fetch(URL);
|
||||||
const teamsList = await response.json();
|
const teamsList = await response.json();
|
||||||
return teamsList;
|
return teamsList;
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ router.get('/divisions', function(req, res, next) {
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/teams', 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));
|
.then(data => res.json(data));
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Reference in New Issue