diff --git a/ProjectSourceCode/src/index.js b/ProjectSourceCode/src/index.js index f023e89..c980ea8 100644 --- a/ProjectSourceCode/src/index.js +++ b/ProjectSourceCode/src/index.js @@ -73,6 +73,19 @@ app.use( extended: true, }) ); +app.use(async function(req, res, next) { + res.locals.user = req.session.user; + + if(res.locals.user) { + try { + res.locals.fav_teams = await getFavoriteTeamsForUser(res.locals.user.userid); + } catch (error) { + console.error('Error fetching favorite teams:', error); + } + } + + next(); +}); // Serve static files from the 'public' directory app.use(express.static(path.join(__dirname, 'resources'))); @@ -202,8 +215,6 @@ app.post('/register', async (req, res) => { const user = await db.oneOrNone('SELECT * FROM users WHERE username = $1', req.body.username); req.session.user = user; req.session.save(); - console.log(user); - // Redirect user to the home page res.redirect('/home'); } catch (error) { @@ -218,7 +229,7 @@ app.post('/register', async (req, res) => { app.get('/home', (req, res) => { const loggedIn = req.session.user ? true : false; - res.render('pages/home', { loggedIn: loggedIn, user: req.session.user}); + res.render('pages/home'); }); /************************ @@ -244,14 +255,24 @@ generateClubRoutes(app); // Function to add a new row to the FavoriteTeams table // database configuration -app.post('/courses/add', async (req, res) => { +app.post('/favteam/add', async (req, res) => { 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 user has more than 5 teams stored + // 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] @@ -260,8 +281,8 @@ app.post('/courses/add', async (req, res) => { 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)', values: [userID, teamID, teamName, teamLogo], @@ -275,6 +296,52 @@ app.post('/courses/add', async (req, res) => { return res.status(500).json({ error: 'Error adding favorite team' }); } }); +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 + 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 not in your favorites.' }); + } + + // Remove the favorite team from the database + await db.none( + 'DELETE FROM FavoriteTeams WHERE UserID = $1 AND TeamName = $2', + [userID, teamName] + ); + + console.log('Favorite team removed successfully.'); + 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' }); + } +}); +async function getFavoriteTeamsForUser(userId) { + try { + // Execute the SQL query + const favoriteTeams = await db.any(` + SELECT * FROM FavoriteTeams + WHERE UserID = $1; + `, userId);`a` + + // Return the result + return favoriteTeams; + } catch (error) { + console.error('Error fetching favorite teams:', error); + throw error; // Rethrow the error for handling at a higher level + } +} + // ***************************************************** // diff --git a/ProjectSourceCode/src/resources/css/navigation-bar/login.css b/ProjectSourceCode/src/resources/css/navigation-bar/login.css index 5539061..06d2316 100644 --- a/ProjectSourceCode/src/resources/css/navigation-bar/login.css +++ b/ProjectSourceCode/src/resources/css/navigation-bar/login.css @@ -30,6 +30,23 @@ border-color: darkred; color: white; } +/* CSS for the account info card */ +.account-info-card { + border: 1px solid #ccc; + border-radius: 8px; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); + padding: 20px; + width: 300px; + } + .info-container { + margin-bottom: 10px; + } + + #username { + font-size: 18px; + font-weight: bold; + color: #333; + } \ 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 bfee40a..fa3ba29 100644 --- a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js +++ b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js @@ -1,12 +1,15 @@ document.addEventListener("DOMContentLoaded", function() { + console.log(clubData.club.club_ID); var favoriteButton = document.getElementById("club-favorite-button"); if (favoriteButton) { favoriteButton.addEventListener("click", function() { if (favoriteButton.src.includes("/favorited.png")) { favoriteButton.src = "/img/club-page/unfavorited.png"; + removeFavoriteTeam(1, 3); } else { + console.log("kjhdsgkjh"); favoriteButton.src = "/img/club-page/favorited.png"; - addFavoriteTeam(1, 65, 'Manchester City FC', 'https://crests.football-data.org/65.png'); + addFavoriteTeam(1, 3, 'Manchester City FC', 'https://crests.football-data.org/65.png'); } }); @@ -14,7 +17,8 @@ document.addEventListener("DOMContentLoaded", function() { }); async function addFavoriteTeam(userID, teamID, teamName, teamLogo) { try { - const response = await fetch('/courses/add', { + console.log("yesss") + const response = await fetch('/favteam/add', { method: 'POST', headers: { 'Content-Type': 'application/json' @@ -33,4 +37,24 @@ async function addFavoriteTeam(userID, teamID, teamName, teamLogo) { } catch (error) { console.error('Error adding favorite team:', error); } -} \ No newline at end of file +} +async function removeFavoriteTeam(userID, teamID) { + try { + const response = await fetch('/favteam/remove', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + userID: userID, + 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/resources/middleware/clubs-page/get-current-club-information.js b/ProjectSourceCode/src/resources/middleware/clubs-page/get-current-club-information.js index a2eea7d..4e2d8ca 100644 --- a/ProjectSourceCode/src/resources/middleware/clubs-page/get-current-club-information.js +++ b/ProjectSourceCode/src/resources/middleware/clubs-page/get-current-club-information.js @@ -15,7 +15,7 @@ const fetchClubsData = async (req, res, next) => { // Extract relevant data from the API response const clubData = response.data; - res.locals.user = users + // res.locals.user = users // Attach the data to res.locals res.locals.club = { area: { diff --git a/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs b/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs index 930abe5..8328210 100644 --- a/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs +++ b/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs @@ -1,6 +1,10 @@ -