From eb779f652a4483e1cdd12428d40652d18b564ad9 Mon Sep 17 00:00:00 2001 From: Vishal Vunnam Date: Sun, 21 Apr 2024 23:03:51 -0600 Subject: [PATCH] finished addfavorite team, page recognizes when team is favorited or not. remove favorite team currently not working --- ProjectSourceCode/src/index.js | 46 ++++++++----------- ProjectSourceCode/src/init_data/create.sql | 2 +- .../resources/css/navigation-bar/login.css | 4 ++ .../resources/js/club-page/favorite-button.js | 17 +++---- .../partials/navigation-bar/accountinfo.hbs | 3 +- 5 files changed, 35 insertions(+), 37 deletions(-) diff --git a/ProjectSourceCode/src/index.js b/ProjectSourceCode/src/index.js index 0404b06..3f4b137 100644 --- a/ProjectSourceCode/src/index.js +++ b/ProjectSourceCode/src/index.js @@ -129,8 +129,13 @@ app.get('/club/:clubID', [fetchClubsData], (req, res) => { 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); + const isTeamIDInFavTeams = fav_teams.some(team => { + const teamIdInt = parseInt(team.teamid); + const clubIdInt = parseInt(req.params.clubID); + console.log('Checking team:', teamIdInt); + console.log('equal to', clubIdInt); + return teamIdInt === clubIdInt; + }); if (isTeamIDInFavTeams) { isFav = true } @@ -274,25 +279,6 @@ app.post('/favteam/add', async (req, res, next) => { return res.status(400).json({ message: 'Login or register to add a favorite team.' }); } - // // 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 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.' }); - // } - // Insert the new favorite team into the database const query = { text: 'INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)', @@ -309,10 +295,6 @@ app.post('/favteam/add', async (req, res, next) => { }); app.post('/favteam/remove', async (req, res) => { try { - if (!req.session.user){ - return res.status(400).json({ message: 'Login or register to remove a favorite team.' }); - } - const { userID, teamName } = req.body; // Check if the team exists for the user @@ -320,8 +302,10 @@ app.post('/favteam/remove', async (req, res) => { 'SELECT * FROM FavoriteTeams WHERE UserID = $1 AND TeamName = $2', [userID, teamName] ); + + // If the team does not exist for the user, return a 404 error if (!existingTeam) { - return res.status(400).json({ message: 'This team is not in your favorites.' }); + return res.status(404).json({ message: 'This team is not in your favorites.' }); } // Remove the favorite team from the database @@ -334,9 +318,17 @@ app.post('/favteam/remove', async (req, res) => { return res.status(200).json({ message: 'Favorite team removed successfully.' }); } catch (error) { console.error('Error removing favorite team:', error); - return res.status(500).json({ error: 'Error removing favorite team' }); + + // If the error is a database error, return a 500 error + if (error instanceof QueryResultError) { + return res.status(500).json({ error: 'Database error occurred while removing favorite team' }); + } + + // If the error is a generic error, return a 400 error + return res.status(400).json({ error: 'Error occurred while removing favorite team' }); } }); + async function getFavoriteTeamsForUser(userId) { try { // Execute the SQL query diff --git a/ProjectSourceCode/src/init_data/create.sql b/ProjectSourceCode/src/init_data/create.sql index 477cce4..29ca3ae 100644 --- a/ProjectSourceCode/src/init_data/create.sql +++ b/ProjectSourceCode/src/init_data/create.sql @@ -5,7 +5,7 @@ CREATE TABLE users ( ); CREATE TABLE FavoriteTeams ( - TeamID INT PRIMARY KEY, + TeamID INT, UserID INT REFERENCES users(UserID), TeamName VARCHAR(50), TeamLogo VARCHAR(100) diff --git a/ProjectSourceCode/src/resources/css/navigation-bar/login.css b/ProjectSourceCode/src/resources/css/navigation-bar/login.css index 06d2316..885cda6 100644 --- a/ProjectSourceCode/src/resources/css/navigation-bar/login.css +++ b/ProjectSourceCode/src/resources/css/navigation-bar/login.css @@ -48,5 +48,9 @@ font-weight: bold; color: #333; } + #teamlogo{ + width: 23px; + height: 23px; + } \ No newline at end of file diff --git a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js index 9ddef22..49fa031 100644 --- a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js +++ b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js @@ -9,12 +9,15 @@ document.addEventListener("DOMContentLoaded", function() { if (favoriteButton.src.includes("/favorited.png")) { removeFavoriteTeam(userID, teamID) - .then(() => { + .then((rmv_status) => { + console.log(rmv_status); + if (rmv_status === 200) { favoriteButton.src = "/img/club-page/unfavorited.png"; - }) - .catch(error => { + } + }) + .catch(error => { console.error('Error removing favorite team:', error); - }); + }); } else { addFavoriteTeam(userID, teamID, teamName, teamLogo) .then(() => { @@ -26,7 +29,8 @@ document.addEventListener("DOMContentLoaded", function() { } }); } -}); +}); + async function addFavoriteTeam(userID, teamID, teamName, teamLogo){ try { const response = await fetch('/favteam/add', { @@ -64,9 +68,6 @@ async function removeFavoriteTeam(userID, teamID) { teamID: teamID }) }); - if (!response.ok) { - throw new Error('Failed to remove favorite team'); - } console.log('Favorite team removed successfully.'); } catch (error) { console.error('Error removing favorite team:', error); diff --git a/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs b/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs index 8328210..12ec483 100644 --- a/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs +++ b/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs @@ -4,7 +4,8 @@ Username: {{user.username}} Favorite Teams:
{{#each fav_teams}} - {{this.teamname}}
+ + {{this.teamname}}
{{/each}} \ No newline at end of file