Links to other leagues added

This commit is contained in:
Lucas Patenaude
2024-04-04 22:29:18 -06:00
parent 3983d00caf
commit ba6cd1ee7c
2 changed files with 47 additions and 45 deletions

View File

@@ -58,7 +58,7 @@
} }
.team img { .team img {
width: 20px; /* Set the width of the image to 100% of its container */ max-width: 20px; /* Set the width of the image to 100% of its container */
height: auto; /* Maintain the aspect ratio */ max-height: 20px; /* Maintain the aspect ratio */
margin-right: 10px; margin-right: 10px;
} }

View File

@@ -3,59 +3,61 @@ const axios = require('axios');
// Middleware function to fetch matches data // Middleware function to fetch matches data
const fetchMatchesData = async (req, res, next) => { const fetchMatchesData = async (req, res, next) => {
try try {
{
const today = moment().format('YYYY-MM-DD'); // Get today's date in YYYY-MM-DD format const today = moment().format('YYYY-MM-DD'); // Get today's date in YYYY-MM-DD format
// Subtract one day to get yesterday's date // Subtract one day to get yesterday's date
var yesterdayUnformatted = moment().subtract(1, 'days'); const yesterdayUnformatted = moment().subtract(1, 'days');
// Format yesterday's date as YYYY-MM-DD // Format yesterday's date as YYYY-MM-DD
var yesterday = yesterdayUnformatted.format('YYYY-MM-DD'); const yesterday = yesterdayUnformatted.format('YYYY-MM-DD');
// Array of years to fetch matches data
const years = [2002, 2003, 2021];
// Array to store all matches data
let allMatches = [];
// Loop through each year and fetch matches data
for (const year of years) {
const response = await axios({ const response = await axios({
url: 'http://api.football-data.org/v4/competitions/2021/matches', url: `http://api.football-data.org/v4/competitions/${year}/matches`,
method: 'GET', method: 'GET',
params: params: {
{ dateFrom: yesterday, // Set dateFrom to yesterday's date
dateFrom: yesterday, // Set dateFrom to today's date
dateTo: today, // Set dateTo to today's date dateTo: today, // Set dateTo to today's date
}, },
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 matches = response.data.matches.map(match => ({ const matches = response.data.matches.map(match => ({
homeTeam: homeTeam: {
{
name: match.homeTeam.tla, name: match.homeTeam.tla,
crest: match.homeTeam.crest, crest: match.homeTeam.crest,
}, },
awayTeam: awayTeam: {
{
name: match.awayTeam.tla, name: match.awayTeam.tla,
crest: match.awayTeam.crest, crest: match.awayTeam.crest,
}, },
score: score: {
{
homeScore: match.score.fullTime.home, homeScore: match.score.fullTime.home,
awayScore: match.score.fullTime.away, awayScore: match.score.fullTime.away,
}, },
minute: match.status, // Set the minute of the game minute: match.status, // Set the minute of the game
})); }));
// Attach matches data to res.locals // Concatenate matches data to allMatches array
res.locals.matches = matches; allMatches = allMatches.concat(matches);
next();
} }
catch (error) // Attach all matches data to res.locals
{ res.locals.matches = allMatches;
console.error('Error fetching Premier League matches:', error); next();
} catch (error) {
console.error('Error fetching matches data:', error);
res.locals.matches = []; // Set an empty array if there's an error res.locals.matches = []; // Set an empty array if there's an error
next(); // Call next middleware or route handler next(); // Call next middleware or route handler
} }