So, I'm making a football webapp in which currently few things are only one is you can…
- User
So, I'm making a football webapp in which currently few things are only one is you can see live score for which I have controller and another thing is You can see any team information teams in that league And front end of it now I want you to modify my leaguecontroller such that it can also provide data of matches which it may take from scoreController and standings which it will take from API which I have provided and also top scorer in that league which has category like from which team is playing // controllers/leagueController.js
const axios = require("axios");
/**
-
Fetches detailed information about a specific league and its teams.
-
Route: GET /leagues/:code
-
Example: GET /leagues/PL */ async function leagueController(req, res) { try { const leagueCode = req.params.code.toUpperCase(); // Ensure the league code is uppercase (e.g., 'PL')
// Define the API endpoints const leagueInfoUrl =
https://api.football-data.org/v4/competitions/${leagueCode}; const leagueTeamsUrl =https://api.football-data.org/v4/competitions/${leagueCode}/teams;// Make parallel API requests for league info and teams const [leagueResponse, teamsResponse] = await Promise.all([ axios.get(leagueInfoUrl, { headers: { "X-Auth-Token": process.env.API_FOOTBALL_KEY1 }, }), axios.get(leagueTeamsUrl, { headers: { "X-Auth-Token": process.env.API_FOOTBALL_KEY1 }, }), ]);
const leagueData = leagueResponse.data; const teamsData = teamsResponse.data.teams;
// Structure the response res.status(200).json({ league: { id: leagueData.id, name: leagueData.name, code: leagueData.code, type: leagueData.type, emblem: leagueData.emblem, area: leagueData.area, currentSeason: leagueData.currentSeason, }, teams: teamsData.map((team) => ({ id: team.id, name: team.name, shortName: team.shortName, tla: team.tla, crest: team.crest, venue: team.venue, website: team.website, founded: team.founded, clubColors: team.clubColors, })), }); } catch (error) { console.error("Error fetching league data:", error.message);
// Handle specific API errors if (error.response) { const { status, data } = error.response; return res.status(status).json({ message: data.message || "Error fetching league data from API.", }); }
// Generic server error res.status(500).json({ message: "Unable to retrieve league data. Please try again later.", }); } }
module.exports = leagueController;
const axios = require("axios");
async function scoresController(req, res) { try { const apiUrl = "https://api.football-data.org/v4/matches"; const response = await axios.get(apiUrl, { headers: { "X-Auth-Token": process.env.API_FOOTBALL_KEY, }, params: { dateFrom: new Date().toISOString().split("T")[0], dateTo: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000) .toISOString() .split("T")[0], }, });
-
- Assistant