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];
const response = await axios({ // Array to store all matches data
url: 'http://api.football-data.org/v4/competitions/2021/matches', let allMatches = [];
method: 'GET',
params:
{
dateFrom: yesterday, // Set dateFrom to today's date
dateTo: today, // Set dateTo to today's date
},
headers:
{
'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here
},
});
// Extract relevant data from the API response // Loop through each year and fetch matches data
const matches = response.data.matches.map(match => ({ for (const year of years) {
homeTeam: const response = await axios({
{ url: `http://api.football-data.org/v4/competitions/${year}/matches`,
name: match.homeTeam.tla, method: 'GET',
crest: match.homeTeam.crest, params: {
}, dateFrom: yesterday, // Set dateFrom to yesterday's date
awayTeam: dateTo: today, // Set dateTo to today's date
{ },
name: match.awayTeam.tla, headers: {
crest: match.awayTeam.crest, 'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here
}, },
score: });
{
homeScore: match.score.fullTime.home,
awayScore: match.score.fullTime.away,
},
minute: match.status, // Set the minute of the game
}));
// Attach matches data to res.locals // Extract relevant data from the API response
res.locals.matches = matches; const matches = response.data.matches.map(match => ({
homeTeam: {
name: match.homeTeam.tla,
crest: match.homeTeam.crest,
},
awayTeam: {
name: match.awayTeam.tla,
crest: match.awayTeam.crest,
},
score: {
homeScore: match.score.fullTime.home,
awayScore: match.score.fullTime.away,
},
minute: match.status, // Set the minute of the game
}));
// Concatenate matches data to allMatches array
allMatches = allMatches.concat(matches);
}
// Attach all matches data to res.locals
res.locals.matches = allMatches;
next(); next();
} } catch (error) {
console.error('Error fetching matches data:', error);
catch (error)
{
console.error('Error fetching Premier League matches:', 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
} }