Changes to try and get league infromation in. Still a work in progress

This commit is contained in:
Lucas Patenaude
2024-04-13 15:09:24 -06:00
parent edd7d17f42
commit f142b8e285
4 changed files with 83 additions and 72 deletions

View File

@@ -1,6 +1,6 @@
function redirectToLeaguePage(leagueName, leagueID) {
function redirectToLeaguePage(leagueID) {
// Append the league name to the URL
var url = "/league/" + leagueName.toLowerCase().replace(/\s+/g, '-');
var url = "/league/" + leagueID;
// Redirect to the league page
window.location.href = url;

View File

@@ -3,50 +3,37 @@ const axios = require('axios');
// Middleware function to fetch leagues data
const fetchLeaguesData = async (req, res, next) => {
try {
// Array of years to fetch leagues data
const league_ids = [2021]; /* Add more league IDs if needed */
// Extract league ID from the URL
const leagueID = req.params.leagueID;
// Array to store all leagues data
let allLeagues = [];
// Make GET request to the API endpoint using the league ID
const response = await axios.get(`http://api.football-data.org/v4/competitions/${leagueID}/standings?season`, {
headers: {
'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here
},
});
// Loop through each year and fetch leagues data
for (const league_id of league_ids) {
const response = await axios({
url: `http://api.football-data.org/v4/competitions/${league_id}/standings?season`,
method: 'GET',
headers: {
'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here
// Extract relevant data from the API response
const leagueData = response.data;
// Attach the data to res.locals
res.locals.leagues = [{
competition: {
league_id: leagueData.competition.id,
league_name: leagueData.competition.name,
league_emblem: leagueData.competition.emblem
},
standings: leagueData.standings[0].table.map(team => ({
table: {
league_position: team.position,
team_id: team.team.id,
team_name: team.team.name,
team_crest: team.team.crest
},
});
// Extract relevant data from the API response
const league_data = response.data.standings[0].table.map(team => ({
competition: {
league_id: response.data.competition.id,
league_name: response.data.competition.name,
league_emblem: response.data.competition.emblem
},
standings: {
table: {
league_position: team.position,
team_id: team.team.id,
team_name: team.team.name,
team_crest: team.team.crest
},
},
}));
// Concatenate leagues data to allLeagues array
allLeagues = allLeagues.concat(league_data);
}
// Attach all leagues data to res.locals
res.locals.leagues = allLeagues;
})),
}];
next();
}
catch (error)
{
} catch (error) {
console.error('Error fetching leagues data:', error);
res.locals.leagues = []; // Set an empty array if there's an error
next(); // Call next middleware or route handler