Add functions to retrieve sport by ID

main
sudoer777 2021-11-22 15:30:02 -07:00
parent 435748ae76
commit b6f8b6bdaa
3 changed files with 23 additions and 3 deletions

View File

@ -48,6 +48,14 @@ async function retrieveAll() {
return sportsList; return sportsList;
} }
async function getFromID(id) {
const query = `SELECT sport_name
FROM scores.sports
WHERE sport_id = $1;`;
const name = (await database.executeQuery(query, [id]))[0][0];
return new Sport(id, name);
}
@ -56,3 +64,4 @@ exports.add = add;
exports.rename = rename; exports.rename = rename;
exports.remove = remove; exports.remove = remove;
exports.retrieveAll = retrieveAll; exports.retrieveAll = retrieveAll;
exports.getFromID = getFromID;

View File

@ -1,11 +1,17 @@
export async function getSports() { export async function getSports() {
const response = await fetch('/data/sports'); const response = await fetch(`/data/sports`);
const sportsList = await response.json(); const sportsList = await response.json();
return sportsList; return sportsList;
} }
export async function getSportName(sportID) {
const response = await fetch(`/data/sport?sport=${sportID}`);
const sport = await response.json();
return sport.name;
}
export async function getSeasons() { export async function getSeasons() {
const response = await fetch('/data/seasons'); const response = await fetch(`/data/seasons`);
const seasonsList = await response.json(); const seasonsList = await response.json();
return seasonsList; return seasonsList;
} }

View File

@ -13,6 +13,11 @@ router.get('/sports', function(req, res, next) {
.then(data => res.json(data)); .then(data => res.json(data));
}); });
router.get('/sport', function(req, res, next) {
sports.getFromID(req.query.sport)
.then(data => res.json(data));
});
router.get('/seasons', function(req, res, next) { router.get('/seasons', function(req, res, next) {
seasons.retrieveAll() seasons.retrieveAll()
.then(data => res.json(data)); .then(data => res.json(data));