Club page routes prototyped

This commit is contained in:
Lucas Patenaude
2024-04-14 12:10:29 -06:00
parent 2720e4d74c
commit a2b47e4b81
9 changed files with 163 additions and 4 deletions

View File

@@ -90,18 +90,33 @@ app.use(fetchMatchesData);
const convert_time = require('./resources/middleware/navigation-bar/convert-time'); const convert_time = require('./resources/middleware/navigation-bar/convert-time');
app.use(convert_time); app.use(convert_time);
const fetchLeaguesData = require('./resources/middleware/league-page/get-current-league-information');
const fetchLeagueScorerData = require('./resources/middleware/league-page/get-current-league-top-scorers'); // Leagues Page Middleware
const fetchLeaguesData = require('./resources/middleware/leagues-page/get-current-league-information');
const fetchLeagueScorerData = require('./resources/middleware/leagues-page/get-current-league-top-scorers');
app.get('/league/:leagueID', [fetchLeaguesData, fetchLeagueScorerData], (req, res) => { app.get('/league/:leagueID', [fetchLeaguesData, fetchLeagueScorerData], (req, res) => {
// Render the Handlebars view with league data // Render the Handlebars view with league data
res.render('pages/league-page', { res.render('pages/leagues-page', {
leagueID: req.params.leagueID, leagueID: req.params.leagueID,
leagues: res.locals.leagues, leagues: res.locals.leagues,
scorers: res.locals.topScorers // Assuming fetchLeagueScorerData sets the data in res.locals.scorers scorers: res.locals.topScorers // Assuming fetchLeagueScorerData sets the data in res.locals.scorers
}); });
}); });
// Clubs Page Middleware
const fetchClubsData = require('./resources/middleware/clubs-page/get-current-club-information');
app.get('/league/:leagueID', [fetchLeaguesData, fetchLeagueScorerData], (req, res) => {
// Render the Handlebars view with league data
res.render('pages/clubs-page', {
clubID: req.params.clubID,
clubs: res.locals.clubs,
});
});
// ***************************************************** // *****************************************************

View File

@@ -0,0 +1,75 @@
const axios = require('axios');
// Middleware function to fetch clubs data
const fetchClubsData = async (req, res, next) => {
try {
// Extract club ID from the URL
const clubID = req.params.clubID;
// Make GET request to the API endpoint using the club ID
const response = await axios.get(`http://api.football-data.org/v4/teams/${clubID}/?offset=&limit=`, {
headers: {
'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here
},
});
// Extract relevant data from the API response
const clubData = response.data;
// Attach the data to res.locals
res.locals.club = {
area: {
id: clubData.area.id,
name: clubData.area.name,
code: clubData.area.code,
club_flag: clubData.area.flag,
},
club_id: clubData.id,
name: clubData.name,
shortName: clubData.shortName,
tla: clubData.tla,
crest: clubData.crest,
address: clubData.address,
website: clubData.website,
founded: clubData.founded,
clubColors: clubData.clubColors,
venue: clubData.venue,
runningCompetitions: clubData.runningCompetitions.map(competition => ({
id: competition.id,
name: competition.name,
code: competition.code,
type: competition.type,
emblem: competition.emblem
})),
coach: {
id: clubData.coach.id,
firstName: clubData.coach.firstName,
lastName: clubData.coach.lastName,
name: clubData.coach.name,
dateOfBirth: clubData.coach.dateOfBirth,
nationality: clubData.coach.nationality,
contract: {
start: clubData.coach.contract.start,
until: clubData.coach.contract.until
}
},
squad: clubData.squad.map(player => ({
id: player.id,
name: player.name,
position: player.position,
dateOfBirth: player.dateOfBirth,
nationality: player.nationality
})),
staff: clubData.staff,
lastUpdated: clubData.lastUpdated
};
next();
} catch (error) {
console.error('Error fetching clubs data:', error);
res.locals.club = null; // Set to null if there's an error
next(); // Call next middleware or route handler
}
};
module.exports = fetchClubsData;

View File

@@ -0,0 +1,44 @@
const axios = require('axios');
// Middleware function to fetch leagues data
const fetchLeagueScorerData = async (req, res, next) => {
try {
// Extract league ID from the URL
const leagueID = req.params.leagueID;
// Make GET request to the API endpoint using the league ID
const response = await axios.get(`http://api.football-data.org/v4/competitions/${leagueID}/scorers?season&limit=20`, {
headers: {
'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here
},
});
// Extract relevant data from the API response
const scorerData = response.data;
// Attach the data to res.locals
res.locals.topScorers = {
scorers: scorerData.scorers.map(player => ({
player: {
player_id: player.player.id,
player_name: player.player.name,
},
team: {
team_id: player.player.id,
team_name: player.team.name,
team_crest: player.team.crest,
},
games_played: player.playedMatches,
goals: player.goals,
}))
};
next();
} catch (error) {
console.error('Error fetching leagues data:', error);
res.locals.topScorers = null; // Set to null if there's an error
next(); // Call next middleware or route handler
}
};
module.exports = fetchLeagueScorerData;

View File

@@ -0,0 +1,18 @@
const express = require('express');
const app = express();
// generate-league-routes.js
// Define a function to generate league routes
module.exports = function generateLeagueRoutes(app) {
// Define a route to handle requests to "/league/:leagueName"
app.get('/club/:clubID', (req, res) => {
// Extract the league name from the URL parameters
const clubID = req.params.clubID;
// Render the league page template using Handlebars
res.render('pages/club-page', { clubID: clubID });
});
};

View File

@@ -0,0 +1,7 @@
function redirectToLeaguePage(clubID) {
// Append the league name to the URL
var url = "/club/" + clubID;
// Redirect to the league page
window.location.href = url;
}

View File

@@ -11,7 +11,7 @@ module.exports = function generateLeagueRoutes(app) {
const leagueID = req.params.leagueID; const leagueID = req.params.leagueID;
// Render the league page template using Handlebars // Render the league page template using Handlebars
res.render('pages/league-page', { leagueID: leagueID }); res.render('pages/leagues-page', { leagueID: leagueID });
}); });
}; };