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

@@ -84,25 +84,24 @@ app.use(express.static(path.join(__dirname, 'resources')));
// Importing middleware // Importing middleware
const fetchMatchesData = require('./resources/routes/navigation-bar/current-match-information'); const fetchMatchesData = require('./resources/routes/navigation-bar/current-match-information');
const convert_time = require('./resources/js/navigation-bar/scoreboard-header/convert-time'); const convert_time = require('./resources/js/navigation-bar/scoreboard-header/convert-time');
const fetchLeaguesData = require('./resources/routes/league-pages/get-current-league-information');
// Using middleware // Using middleware
app.use(fetchMatchesData); app.use(fetchMatchesData);
app.use(convert_time); app.use(convert_time);
app.use(fetchLeaguesData);
// Middleware function to fetch leagues data
const fetchLeaguesData = require('./resources/routes/league-pages/get-current-league-information');
// Define the route for fetching league data
app.get('/league/:leagueID', fetchLeaguesData, (req, res) => {
// Handle the response here
res.json(res.locals.leagues);
});
// ***************************************************** // *****************************************************
// <!-- Section 5 : API Routes --> // <!-- Section 5 : API Routes -->
// ***************************************************** // *****************************************************
/************************
League Page Routes
*************************/
// Import and call generateLeagueRoutes function
const generateLeagueRoutes = require('./resources/routes/league-pages/generate-league-routes');
generateLeagueRoutes(app);
/************************ /************************
Login Page Routes Login Page Routes
*************************/ *************************/
@@ -187,6 +186,14 @@ app.get('/home', (req, res) => {
res.render('pages/home'); res.render('pages/home');
}); });
/************************
League Page Routes
*************************/
// Import and call generateLeagueRoutes function
const generateLeagueRoutes = require('./resources/routes/league-pages/generate-league-routes');
generateLeagueRoutes(app);
// ***************************************************** // *****************************************************
// <!-- Section 5 : Start Server--> // <!-- Section 5 : Start Server-->
// ***************************************************** // *****************************************************

View File

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

View File

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

View File

@@ -1,23 +1,40 @@
<div class="container" id="league-page-body"> <!-- league-page.hbs -->
<!-- Container for all league information (logo, name, country, etc. ) <- top 100px --> <!DOCTYPE html>
<div class="container" id="league-information-container"> <html lang="en">
<img src="{{ competition.league_emblem }}" alt="League Emblem"> <head>
</div> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ league_name }}</title>
</head>
<body>
<h1>{{ leagueID }}</h1>
<h1>{{ league_name }}</h1>
<!-- Container to display all stats for league <- bottom rest of the container --> <!-- Display league emblem -->
<div class="container" id="league-stats-container"> {{#if leagueEmblem}}
<img src="{{ leagueEmblem }}" alt="{{ leagueName }} Emblem">
{{/if}}
<!-- Container to display league table <- split 50% --> <!-- Display league standings -->
<div class="container" id="league-table-container"> <h2>Standings</h2>
<table>
<thead>
</div> <tr>
<th>Position</th>
<!-- Container to display top scorers for league <- Split 50% --> <th>Team</th>
<div class="container" id="top-scorers-container"> <th>Crest</th>
</tr>
</div> </thead>
<tbody>
</div> {{#each standings}}
</div> <tr>
<td>{{ this.league_position }}</td>
<td>{{ this.team_name }}</td>
<td><img src="{{ this.team_crest }}" alt="{{ this.team_name }} Crest"></td>
</tr>
{{/each}}
</tbody>
</table>
</body>
</html>