diff --git a/ProjectSourceCode/src/index.js b/ProjectSourceCode/src/index.js
index c980ea8..0404b06 100644
--- a/ProjectSourceCode/src/index.js
+++ b/ProjectSourceCode/src/index.js
@@ -121,11 +121,22 @@ app.get('/league/:leagueID', [fetchLeaguesData, fetchLeagueScorerData], (req, re
// Clubs Page Middleware
const fetchClubsData = require('./resources/middleware/clubs-page/get-current-club-information');
-const fetchClubTopScorers = require('./resources/middleware/clubs-page/get-current-club-top-scorers');
-app.get('/club/:clubID', [fetchClubsData, fetchClubTopScorers], (req, res) => {
+app.get('/club/:clubID', [fetchClubsData], (req, res) => {
// Render the Handlebars view with league data
+
+ var isFav = false;
+ var fav_teams = res.locals.fav_teams;
+ if(res.locals.user && fav_teams)
+ {
+ const isTeamIDInFavTeams = fav_teams.some(team => team.teamid === req.params.clubID);
+ console.log(isTeamIDInFavTeams);
+ if (isTeamIDInFavTeams) {
+ isFav = true
+ }
+ }
res.render('pages/clubs-page', {
+ isFav: isFav,
clubID: req.params.clubID,
clubs: res.locals.club
});
@@ -172,7 +183,6 @@ app.post('/login', async (req, res) => {
// Save user information in the session variable
req.session.user = user;
req.session.save();
- console.log(user);
// Redirect user to the home page
res.redirect('/home');
@@ -255,45 +265,46 @@ generateClubRoutes(app);
// Function to add a new row to the FavoriteTeams table
// database configuration
-app.post('/favteam/add', async (req, res) => {
+app.post('/favteam/add', async (req, res, next) => {
try {
- if (!req.session.user){
- return res.status(400).json({ message: 'Login or register to add a favorite team.' });
- }
-
- const { userID, teamID, teamName, teamLogo } = req.body;
+ const { userID, teamID, teamName, teamLogo } = req.body;
- // Check if the team name already exists for the user
- const existingTeam = await db.oneOrNone(
- 'SELECT * FROM FavoriteTeams WHERE UserID = $1 AND TeamName = $2',
- [userID, teamName]
- );
- if (existingTeam) {
- return res.status(400).json({ message: 'This team is already in your favorites.' });
- }
+ // Check if the user is logged in
+ if (!req.session.user) {
+ return res.status(400).json({ message: 'Login or register to add a favorite team.' });
+ }
- // Check if user has more than 5 teams stored
- const { team_count } = await db.one(
- 'SELECT COUNT(*) AS team_count FROM FavoriteTeams WHERE UserID = $1',
- [userID]
- );
- if (team_count >= 5) {
- console.log('User has reached the maximum number of favorite teams.');
- return res.status(400).json({ message: 'User has reached the maximum number of favorite teams.' });
- }
+ // // Check if the team name already exists for the user
+ // const existingTeam = await db.oneOrNone(
+ // 'SELECT * FROM FavoriteTeams WHERE UserID = $1 AND TeamName = $2',
+ // [userID, teamName]
+ // );
+ // if (existingTeam) {
+ // return res.status(400).json({ message: 'This team is already in your favorites.' });
+ // }
- // Insert the new favorite team into the database
- const query = {
- text: 'INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)',
- values: [userID, teamID, teamName, teamLogo],
- };
+ // // Check if user has more than 5 teams stored
+ // const { team_count } = await db.one(
+ // 'SELECT COUNT(*) AS team_count FROM FavoriteTeams WHERE UserID = $1',
+ // [userID]
+ // );
+ // if (team_count >= 5) {
+ // console.log('User has reached the maximum number of favorite teams.');
+ // return res.status(400).json({ message: 'User has reached the maximum number of favorite teams.' });
+ // }
- await db.none(query);
- console.log('New favorite team added successfully.');
- return res.status(200).json({ message: 'New favorite team added successfully.' });
+ // Insert the new favorite team into the database
+ const query = {
+ text: 'INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)',
+ values: [userID, teamID, teamName, teamLogo],
+ };
+
+ await db.none(query);
+ console.log('New favorite team added successfully.');
+ return res.status(200).json({ message: 'New favorite team added successfully.' });
} catch (error) {
- console.error('Error adding favorite team:', error);
- return res.status(500).json({ error: 'Error adding favorite team' });
+ console.error('Error adding favorite team:', error);
+ return res.status(500).json({ error: 'Error adding favorite team' });
}
});
app.post('/favteam/remove', async (req, res) => {
diff --git a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js
index fa3ba29..9ddef22 100644
--- a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js
+++ b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js
@@ -1,23 +1,34 @@
document.addEventListener("DOMContentLoaded", function() {
- console.log(clubData.club.club_ID);
var favoriteButton = document.getElementById("club-favorite-button");
if (favoriteButton) {
favoriteButton.addEventListener("click", function() {
+ var userID = document.getElementById("userID").value;
+ var teamID = document.getElementById("teamID").value;
+ var teamName = document.getElementById("teamName").value;
+ var teamLogo = document.getElementById("teamLogo").value;
+
if (favoriteButton.src.includes("/favorited.png")) {
- favoriteButton.src = "/img/club-page/unfavorited.png";
- removeFavoriteTeam(1, 3);
+ removeFavoriteTeam(userID, teamID)
+ .then(() => {
+ favoriteButton.src = "/img/club-page/unfavorited.png";
+ })
+ .catch(error => {
+ console.error('Error removing favorite team:', error);
+ });
} else {
- console.log("kjhdsgkjh");
- favoriteButton.src = "/img/club-page/favorited.png";
- addFavoriteTeam(1, 3, 'Manchester City FC', 'https://crests.football-data.org/65.png');
-
+ addFavoriteTeam(userID, teamID, teamName, teamLogo)
+ .then(() => {
+ favoriteButton.src = "/img/club-page/favorited.png";
+ })
+ .catch(error => {
+ console.error('Error adding favorite team:', error);
+ });
}
});
}
- });
-async function addFavoriteTeam(userID, teamID, teamName, teamLogo) {
+});
+async function addFavoriteTeam(userID, teamID, teamName, teamLogo){
try {
- console.log("yesss")
const response = await fetch('/favteam/add', {
method: 'POST',
headers: {
@@ -30,12 +41,15 @@ async function addFavoriteTeam(userID, teamID, teamName, teamLogo) {
teamLogo: teamLogo
})
});
+
if (!response.ok) {
throw new Error('Failed to add favorite team');
}
+
console.log('New favorite team added successfully.');
} catch (error) {
console.error('Error adding favorite team:', error);
+
}
}
async function removeFavoriteTeam(userID, teamID) {
diff --git a/ProjectSourceCode/src/resources/middleware/clubs-page/get-current-club-top-scorers.js b/ProjectSourceCode/src/resources/middleware/clubs-page/get-current-club-top-scorers.js
deleted file mode 100644
index 5c2ea41..0000000
--- a/ProjectSourceCode/src/resources/middleware/clubs-page/get-current-club-top-scorers.js
+++ /dev/null
@@ -1,36 +0,0 @@
-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/teams/${teamID}/scorers?limit=5`, {
- 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 => ({
- playerID: scorer.player.id,
- playerName: scorer.player.name,
- goals: scorer.numberOfGoals,
- }))
- };
-
- 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;
diff --git a/ProjectSourceCode/src/resources/routes/club-pages/generate-club-routes.js b/ProjectSourceCode/src/resources/routes/club-pages/generate-club-routes.js
index 070c8d2..d813205 100644
--- a/ProjectSourceCode/src/resources/routes/club-pages/generate-club-routes.js
+++ b/ProjectSourceCode/src/resources/routes/club-pages/generate-club-routes.js
@@ -9,9 +9,8 @@ module.exports = function generateClubRoutes(app) {
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, user:user });
+ res.render('pages/club-page', { clubID: clubID, });
});
};
diff --git a/ProjectSourceCode/src/views/pages/clubs-page.hbs b/ProjectSourceCode/src/views/pages/clubs-page.hbs
index 4ebfc17..ebb3aef 100644
--- a/ProjectSourceCode/src/views/pages/clubs-page.hbs
+++ b/ProjectSourceCode/src/views/pages/clubs-page.hbs
@@ -5,7 +5,21 @@
+ {{#if user}}
+ {{#if isFav}}
+
+ {{else}}
+
+ {{/if}}
+ {{else}}
+ Please sign in
+ {{/if}}
+
+
+
+
+
+