From 637b9002e765e02ce470afe4a42e2c4a983a146d Mon Sep 17 00:00:00 2001 From: Lucas Patenaude Date: Thu, 11 Apr 2024 08:49:19 -0600 Subject: [PATCH] Start of league API implementation --- .../get-current-league-information.js | 51 +++++++++++++++++++ .../src/views/pages/league-page.hbs | 3 ++ 2 files changed, 54 insertions(+) diff --git a/ProjectSourceCode/src/resources/routes/league-pages/get-current-league-information.js b/ProjectSourceCode/src/resources/routes/league-pages/get-current-league-information.js index e69de29..f577f21 100644 --- a/ProjectSourceCode/src/resources/routes/league-pages/get-current-league-information.js +++ b/ProjectSourceCode/src/resources/routes/league-pages/get-current-league-information.js @@ -0,0 +1,51 @@ +const axios = require('axios'); + +// Middleware function to fetch leaguees data +const fetchleagueesData = async (req, res, next) => { + try { + // Array of years to fetch leaguees data + const league_ids = [2021]; /* Readd , 2002, 2014, 2019, 2015, 2013 */ + + // Array to store all leaguees data + let allLeagues = []; + + // Loop through each year and fetch leaguees data + for (const league_id of league_ids) { + const response = await axios({ + url: `http://api.football-data.org/v4/competitions/${league_id}/standings?season`, /* Resinsert ${league_id} for 2021 */ + method: 'GET', + headers: { + 'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here + }, + }); + + // Extract relevant data from the API response + const leagues = response.data.leagues.map(league => ({ + competition: { + league_id: league.competition.id, + league_name: league.competition.name, + league_emblem: league.competition.emblem + }, + standings: { + table: { + league_position: league.standings.table.position, + team_id: league.standings.table.team.id, + }, + }, + })); + + // Concatenate leaguees data to allleaguees array + allLeaguees = allLeaguees.concat(leagues); + } + + // Attach all leaguees data to res.locals + res.locals.leagues = allLeagues; + next(); + } catch (error) { + console.error('Error fetching leaguees data:', error); + res.locals.leagues = []; // Set an empty array if there's an error + next(); // Call next middleware or route handler + } +}; + +module.exports = fetchleagueesData; \ No newline at end of file diff --git a/ProjectSourceCode/src/views/pages/league-page.hbs b/ProjectSourceCode/src/views/pages/league-page.hbs index 4a2eaf0..abd4dcb 100644 --- a/ProjectSourceCode/src/views/pages/league-page.hbs +++ b/ProjectSourceCode/src/views/pages/league-page.hbs @@ -10,6 +10,9 @@
+ {{#each league}} + {{> navigation-bar/scoreboard-header/game-card}} + {{/each}}