From 6faec75ae2f2c184767a63610082fced3892caa9 Mon Sep 17 00:00:00 2001 From: Vishal Vunnam Date: Wed, 17 Apr 2024 18:06:45 -0600 Subject: [PATCH 1/7] added js and worked on Login feature(still in progress) --- ProjectSourceCode/docker-compose.yaml | 2 +- ProjectSourceCode/src/index.js | 41 ++++++++++++++++++- ProjectSourceCode/src/init_data/create.sql | 4 +- .../js/account-info/favorite-team.js | 0 .../resources/js/club-page/favorite-button.js | 32 ++++++++++++++- .../js/registration/league-and-team-select.js | 1 + .../get-current-club-information.js | 1 - .../get-current-league-information.js | 4 +- .../routes/club-pages/generate-club-routes.js | 2 +- .../league-pages/generate-league-routes.js | 5 ++- .../get-team-names-for-registry.js | 2 +- .../src/views/pages/clubs-page.hbs | 2 +- ProjectSourceCode/src/views/pages/home.hbs | 10 ++--- .../partials/navigation-bar/accountinfo.hbs | 6 +++ .../src/views/partials/navigation-bar/nav.hbs | 10 +++-- 15 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 ProjectSourceCode/src/resources/js/account-info/favorite-team.js create mode 100644 ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs diff --git a/ProjectSourceCode/docker-compose.yaml b/ProjectSourceCode/docker-compose.yaml index eca8a77..ba88fc4 100644 --- a/ProjectSourceCode/docker-compose.yaml +++ b/ProjectSourceCode/docker-compose.yaml @@ -33,6 +33,6 @@ services: volumes: - ../ProjectSourceCode:/home/node/app # Mount ProjectSourceCode directory - ../ProjectSourceCode/node_modules:/home/node/app/node_modules # Mount node_modules directory - command: 'npm run testandrun' + command: 'npm run start' volumes: group-project: \ No newline at end of file diff --git a/ProjectSourceCode/src/index.js b/ProjectSourceCode/src/index.js index 825ddc7..21c071e 100644 --- a/ProjectSourceCode/src/index.js +++ b/ProjectSourceCode/src/index.js @@ -155,13 +155,15 @@ app.post('/login', async (req, res) => { // Render the login page with the message parameter return res.render('pages/home', { message: 'Password does not match' }); } - + else{ // 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'); + } } catch (error) { // Direct user to login screen if no user is found with matching password res.redirect('/register'); @@ -203,7 +205,8 @@ app.post('/register', async (req, res) => { *************************/ app.get('/home', (req, res) => { - res.render('pages/home'); + const loggedIn = req.session.user ? true : false; + res.render('pages/home', { loggedIn: loggedIn, user: req.session.user}); }); /************************ @@ -222,6 +225,40 @@ generateLeagueRoutes(app); const generateClubRoutes = require('./resources/routes/club-pages/generate-club-routes'); generateClubRoutes(app); +/************************ + Favorite Team Database +*************************/ + +// Function to add a new row to the FavoriteTeams table +// database configuration + +app.post('/courses/add', async (req, res) => { + const { userID, teamID, teamName, teamLogo } = req.body; + + try { + 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.' }); + } + + 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' }); + } +}); + // ***************************************************** // // ***************************************************** diff --git a/ProjectSourceCode/src/init_data/create.sql b/ProjectSourceCode/src/init_data/create.sql index de888d7..33d061f 100644 --- a/ProjectSourceCode/src/init_data/create.sql +++ b/ProjectSourceCode/src/init_data/create.sql @@ -5,8 +5,8 @@ CREATE TABLE users ( ); CREATE TABLE FavoriteTeams ( - UserID INT REFERENCES users(UserID) ON DELETE CASCADE ON UPDATE CASCADE, + UserID INT, TeamID INT, TeamName VARCHAR(50), - PRIMARY KEY (UserID, TeamID) + TeamLogo VARCHAR(100) ); diff --git a/ProjectSourceCode/src/resources/js/account-info/favorite-team.js b/ProjectSourceCode/src/resources/js/account-info/favorite-team.js new file mode 100644 index 0000000..e69de29 diff --git a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js index 3969016..bfee40a 100644 --- a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js +++ b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js @@ -2,7 +2,35 @@ document.addEventListener("DOMContentLoaded", function() { var favoriteButton = document.getElementById("club-favorite-button"); if (favoriteButton) { favoriteButton.addEventListener("click", function() { - favoriteButton.src = "/img/club-page/favorited.png"; + if (favoriteButton.src.includes("/favorited.png")) { + favoriteButton.src = "/img/club-page/unfavorited.png"; + } else { + favoriteButton.src = "/img/club-page/favorited.png"; + addFavoriteTeam(1, 65, 'Manchester City FC', 'https://crests.football-data.org/65.png'); + + } }); } -}); + }); +async function addFavoriteTeam(userID, teamID, teamName, teamLogo) { + try { + const response = await fetch('/courses/add', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + userID: userID, + teamID: teamID, + teamName: teamName, + 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); + } +} \ No newline at end of file diff --git a/ProjectSourceCode/src/resources/js/registration/league-and-team-select.js b/ProjectSourceCode/src/resources/js/registration/league-and-team-select.js index 2b9304c..71113fe 100644 --- a/ProjectSourceCode/src/resources/js/registration/league-and-team-select.js +++ b/ProjectSourceCode/src/resources/js/registration/league-and-team-select.js @@ -37,4 +37,5 @@ document.addEventListener("DOMContentLoaded", function() { selectElement.add(option); } }); + \ No newline at end of file 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 c28ec43..de9dc15 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 @@ -63,7 +63,6 @@ const fetchClubsData = async (req, res, next) => { staff: clubData.staff, lastUpdated: clubData.lastUpdated }; - next(); } catch (error) { console.error('Error fetching clubs data:', error); diff --git a/ProjectSourceCode/src/resources/middleware/leagues-page/get-current-league-information.js b/ProjectSourceCode/src/resources/middleware/leagues-page/get-current-league-information.js index 09eaf0f..d8726c9 100644 --- a/ProjectSourceCode/src/resources/middleware/leagues-page/get-current-league-information.js +++ b/ProjectSourceCode/src/resources/middleware/leagues-page/get-current-league-information.js @@ -17,6 +17,8 @@ const fetchLeaguesData = async (req, res, next) => { const leagueData = response.data; // Attach the data to res.locals + res.locals.username = req.session.user.username; + console.log(req.session.user) res.locals.league = { area: { league_flag: leagueData.area.flag, @@ -39,7 +41,7 @@ const fetchLeaguesData = async (req, res, next) => { draws: team.draw, goal_difference: team.goalDifference, points: team.points - })) + })), }; next(); } catch (error) { 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 c3ea81c..070c8d2 100644 --- a/ProjectSourceCode/src/resources/routes/club-pages/generate-club-routes.js +++ b/ProjectSourceCode/src/resources/routes/club-pages/generate-club-routes.js @@ -11,7 +11,7 @@ module.exports = function generateClubRoutes(app) { const clubID = req.params.clubID; // Render the league page template using Handlebars - res.render('pages/club-page', { clubID: clubID }); + res.render('pages/club-page', { clubID: clubID, user:user }); }); }; diff --git a/ProjectSourceCode/src/resources/routes/league-pages/generate-league-routes.js b/ProjectSourceCode/src/resources/routes/league-pages/generate-league-routes.js index 8dca5e8..0ee5d3d 100644 --- a/ProjectSourceCode/src/resources/routes/league-pages/generate-league-routes.js +++ b/ProjectSourceCode/src/resources/routes/league-pages/generate-league-routes.js @@ -8,10 +8,11 @@ module.exports = function generateLeagueRoutes(app) { // Define a route to handle requests to "/league/:leagueName" app.get('/league/:leagueID', (req, res) => { // Extract the league name from the URL parameters + console.log("jskjfhskjhdfkjh") const leagueID = req.params.leagueID; - + const user = req.session.user; // Render the league page template using Handlebars - res.render('pages/leagues-page', { leagueID: leagueID }); + res.render('pages/leagues-page', { leagueID: leagueID, user: user}); }); }; diff --git a/ProjectSourceCode/src/resources/routes/league-pages/get-team-names-for-registry.js b/ProjectSourceCode/src/resources/routes/league-pages/get-team-names-for-registry.js index 9f61dac..920bf33 100644 --- a/ProjectSourceCode/src/resources/routes/league-pages/get-team-names-for-registry.js +++ b/ProjectSourceCode/src/resources/routes/league-pages/get-team-names-for-registry.js @@ -9,7 +9,7 @@ const fetchTeamNames = async (selectedLeague) => { 'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', }, }); - + const teams = response.data.teams.map(team => team.name); return teams; } catch (error) { diff --git a/ProjectSourceCode/src/views/pages/clubs-page.hbs b/ProjectSourceCode/src/views/pages/clubs-page.hbs index fb35c60..4ebfc17 100644 --- a/ProjectSourceCode/src/views/pages/clubs-page.hbs +++ b/ProjectSourceCode/src/views/pages/clubs-page.hbs @@ -5,7 +5,7 @@

{{club.name}}

{{club.name}} Flag - Favorite Button + Favorite Button
diff --git a/ProjectSourceCode/src/views/pages/home.hbs b/ProjectSourceCode/src/views/pages/home.hbs index 8fe6d22..23f8549 100644 --- a/ProjectSourceCode/src/views/pages/home.hbs +++ b/ProjectSourceCode/src/views/pages/home.hbs @@ -4,17 +4,17 @@ diff --git a/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs b/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs new file mode 100644 index 0000000..930abe5 --- /dev/null +++ b/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/ProjectSourceCode/src/views/partials/navigation-bar/nav.hbs b/ProjectSourceCode/src/views/partials/navigation-bar/nav.hbs index 38357c1..744a5b4 100644 --- a/ProjectSourceCode/src/views/partials/navigation-bar/nav.hbs +++ b/ProjectSourceCode/src/views/partials/navigation-bar/nav.hbs @@ -48,10 +48,12 @@
- +{{#if user}} +
+ {{> navigation-bar/accountinfo}} +
+{{else}}
{{> navigation-bar/login}}
- - - +{{/if}} From 86dbef4bf12ce10f6dba78dcc04da866c8a95c7c Mon Sep 17 00:00:00 2001 From: Vishal Vunnam Date: Wed, 17 Apr 2024 21:45:27 -0600 Subject: [PATCH 2/7] Created new users database with userID, Created favoriteteams database, implemented add favorite team function but not working just yet --- ProjectSourceCode/src/index.js | 51 ++++++++++++------- ProjectSourceCode/src/init_data/create.sql | 4 +- ProjectSourceCode/src/init_data/insert.sql | 4 ++ .../get-current-club-information.js | 2 +- .../get-current-league-information.js | 2 +- .../league-pages/generate-league-routes.js | 2 - 6 files changed, 42 insertions(+), 23 deletions(-) diff --git a/ProjectSourceCode/src/index.js b/ProjectSourceCode/src/index.js index 21c071e..f023e89 100644 --- a/ProjectSourceCode/src/index.js +++ b/ProjectSourceCode/src/index.js @@ -181,23 +181,35 @@ app.get('/register', (req, res) => { // Trigger Registration Form to Post app.post('/register', async (req, res) => { - try { - if (!req.body.username || !req.body.password) { - // If username or password is missing, respond with status 400 and an error message - return res.status(400).json({ status: 'error', message: 'Invalid input' }); - } - // Hash the password using bcrypt library - const hash = await bcrypt.hash(req.body.password, 10); + try { + if (!req.body.username || !req.body.password) { + // If username or password is missing, respond with status 400 and an error message + return res.status(400).json({ status: 'error', message: 'Invalid input' }); + } - // Insert username and hashed password into the 'users' table - await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]); + // Check if the username already exists in the database + const existingUser = await db.oneOrNone('SELECT * FROM users WHERE username = $1', req.body.username); + if (existingUser) { + // If a user with the same username already exists, respond with status 409 and an error message + return res.status(409).json({ status: 'error', message: 'Username already exists' }); + } - // Redirect user to the login page after successful registration - res.status(200).json({ status: 'success', message: 'Registration successful' }); - } catch (error) { - // If an error occurs during registration, respond with status 500 and an error message - res.status(500).json({ status: 'error', message: 'An error occurred during registration' }); - } + // Hash the password using bcrypt library + const hash = await bcrypt.hash(req.body.password, 10); + + // Insert username and hashed password into the 'users' table + await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]); + 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) { + // If an error occurs during registration, respond with status 500 and an error message + res.status(500).json({ status: 'error', message: 'An error occurred during registration' }); + } }); /************************ @@ -233,9 +245,13 @@ generateClubRoutes(app); // database configuration app.post('/courses/add', async (req, res) => { - const { userID, teamID, teamName, teamLogo } = req.body; - 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; + + //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] @@ -244,6 +260,7 @@ 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.' }); } + const query = { text: 'INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)', diff --git a/ProjectSourceCode/src/init_data/create.sql b/ProjectSourceCode/src/init_data/create.sql index 33d061f..477cce4 100644 --- a/ProjectSourceCode/src/init_data/create.sql +++ b/ProjectSourceCode/src/init_data/create.sql @@ -5,8 +5,8 @@ CREATE TABLE users ( ); CREATE TABLE FavoriteTeams ( - UserID INT, - TeamID INT, + TeamID INT PRIMARY KEY, + UserID INT REFERENCES users(UserID), TeamName VARCHAR(50), TeamLogo VARCHAR(100) ); diff --git a/ProjectSourceCode/src/init_data/insert.sql b/ProjectSourceCode/src/init_data/insert.sql index e69de29..4669a7a 100644 --- a/ProjectSourceCode/src/init_data/insert.sql +++ b/ProjectSourceCode/src/init_data/insert.sql @@ -0,0 +1,4 @@ +SELECT FavoriteTeams.* +FROM FavoriteTeams +JOIN users ON FavoriteTeams.UserID = users.UserID +WHERE users.UserID = ; \ No newline at end of file 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 de9dc15..a2eea7d 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 // Attach the data to res.locals res.locals.club = { area: { diff --git a/ProjectSourceCode/src/resources/middleware/leagues-page/get-current-league-information.js b/ProjectSourceCode/src/resources/middleware/leagues-page/get-current-league-information.js index d8726c9..4363e25 100644 --- a/ProjectSourceCode/src/resources/middleware/leagues-page/get-current-league-information.js +++ b/ProjectSourceCode/src/resources/middleware/leagues-page/get-current-league-information.js @@ -17,7 +17,7 @@ const fetchLeaguesData = async (req, res, next) => { const leagueData = response.data; // Attach the data to res.locals - res.locals.username = req.session.user.username; + //res.locals.username = req.session.user.username; console.log(req.session.user) res.locals.league = { area: { diff --git a/ProjectSourceCode/src/resources/routes/league-pages/generate-league-routes.js b/ProjectSourceCode/src/resources/routes/league-pages/generate-league-routes.js index 0ee5d3d..ae7a9ff 100644 --- a/ProjectSourceCode/src/resources/routes/league-pages/generate-league-routes.js +++ b/ProjectSourceCode/src/resources/routes/league-pages/generate-league-routes.js @@ -8,9 +8,7 @@ module.exports = function generateLeagueRoutes(app) { // Define a route to handle requests to "/league/:leagueName" app.get('/league/:leagueID', (req, res) => { // Extract the league name from the URL parameters - console.log("jskjfhskjhdfkjh") const leagueID = req.params.leagueID; - const user = req.session.user; // Render the league page template using Handlebars res.render('pages/leagues-page', { leagueID: leagueID, user: user}); }); From ac19be3cd9beb4ed02e3e93cb8ee2ef90823fd7f Mon Sep 17 00:00:00 2001 From: Vishal Vunnam Date: Fri, 19 Apr 2024 19:19:42 -0600 Subject: [PATCH 3/7] login feature is fully complete, testing is required, favorite teams database is fully functioning --- ProjectSourceCode/src/index.js | 81 +++++++++++++++++-- .../resources/css/navigation-bar/login.css | 17 ++++ .../resources/js/club-page/favorite-button.js | 30 ++++++- .../get-current-club-information.js | 2 +- .../partials/navigation-bar/accountinfo.hbs | 14 ++-- .../src/views/partials/navigation-bar/nav.hbs | 2 +- 6 files changed, 129 insertions(+), 17 deletions(-) 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 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/ProjectSourceCode/src/views/partials/navigation-bar/nav.hbs b/ProjectSourceCode/src/views/partials/navigation-bar/nav.hbs index 744a5b4..66404ef 100644 --- a/ProjectSourceCode/src/views/partials/navigation-bar/nav.hbs +++ b/ProjectSourceCode/src/views/partials/navigation-bar/nav.hbs @@ -48,7 +48,7 @@ -{{#if user}} +{{#if user.username}}
{{> navigation-bar/accountinfo}}
From 75d9560293e454414042b1fc9256377f97c13349 Mon Sep 17 00:00:00 2001 From: Vishal Vunnam Date: Sun, 21 Apr 2024 14:11:35 -0600 Subject: [PATCH 4/7] addFavorite team is working, button and remove will be finished tonight --- ProjectSourceCode/src/index.js | 83 +++++++++++-------- .../resources/js/club-page/favorite-button.js | 34 +++++--- .../get-current-club-top-scorers.js | 36 -------- .../routes/club-pages/generate-club-routes.js | 3 +- .../src/views/pages/clubs-page.hbs | 16 +++- 5 files changed, 87 insertions(+), 85 deletions(-) delete mode 100644 ProjectSourceCode/src/resources/middleware/clubs-page/get-current-club-top-scorers.js 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 @@

{{club.name}}

{{club.name}} Flag - Favorite Button + {{#if user}} + {{#if isFav}} + Favorite Button + {{else}} + Favorite Button + {{/if}} + {{else}} + Please sign in + {{/if}} + + + + + +
From eb779f652a4483e1cdd12428d40652d18b564ad9 Mon Sep 17 00:00:00 2001 From: Vishal Vunnam Date: Sun, 21 Apr 2024 23:03:51 -0600 Subject: [PATCH 5/7] 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 From eef7797f57a692297513120fd937a4df4e6640d1 Mon Sep 17 00:00:00 2001 From: Vishal Vunnam Date: Mon, 22 Apr 2024 11:39:37 -0600 Subject: [PATCH 6/7] remove favorite team now working --- ProjectSourceCode/src/index.js | 10 +++---- .../resources/js/club-page/favorite-button.js | 27 +++++++------------ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/ProjectSourceCode/src/index.js b/ProjectSourceCode/src/index.js index 3f4b137..f9dc19f 100644 --- a/ProjectSourceCode/src/index.js +++ b/ProjectSourceCode/src/index.js @@ -295,12 +295,12 @@ app.post('/favteam/add', async (req, res, next) => { }); app.post('/favteam/remove', async (req, res) => { try { - const { userID, teamName } = req.body; + const { userID, teamID } = 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] + 'SELECT * FROM FavoriteTeams WHERE UserID = $1 AND TeamID = $2', + [userID, teamID] ); // If the team does not exist for the user, return a 404 error @@ -310,8 +310,8 @@ app.post('/favteam/remove', async (req, res) => { // Remove the favorite team from the database await db.none( - 'DELETE FROM FavoriteTeams WHERE UserID = $1 AND TeamName = $2', - [userID, teamName] + 'DELETE FROM FavoriteTeams WHERE UserID = $1 AND TeamID = $2', + [userID, teamID] ); console.log('Favorite team removed successfully.'); diff --git a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js index 49fa031..5697a60 100644 --- a/ProjectSourceCode/src/resources/js/club-page/favorite-button.js +++ b/ProjectSourceCode/src/resources/js/club-page/favorite-button.js @@ -9,23 +9,9 @@ document.addEventListener("DOMContentLoaded", function() { if (favoriteButton.src.includes("/favorited.png")) { removeFavoriteTeam(userID, teamID) - .then((rmv_status) => { - console.log(rmv_status); - if (rmv_status === 200) { - favoriteButton.src = "/img/club-page/unfavorited.png"; - } - }) - .catch(error => { - console.error('Error removing favorite team:', error); - }); - } else { + } + else { addFavoriteTeam(userID, teamID, teamName, teamLogo) - .then(() => { - favoriteButton.src = "/img/club-page/favorited.png"; - }) - .catch(error => { - console.error('Error adding favorite team:', error); - }); } }); } @@ -49,8 +35,11 @@ async function addFavoriteTeam(userID, teamID, teamName, teamLogo){ if (!response.ok) { throw new Error('Failed to add favorite team'); } - + //Changes button if favorite team is added// console.log('New favorite team added successfully.'); + var favoriteButton = document.getElementById("club-favorite-button"); + favoriteButton.src = "/img/club-page/favorited.png"; + } catch (error) { console.error('Error adding favorite team:', error); @@ -69,6 +58,10 @@ async function removeFavoriteTeam(userID, teamID) { }) }); console.log('Favorite team removed successfully.'); + //Change button source// + var favoriteButton = document.getElementById("club-favorite-button"); + favoriteButton.src = "/img/club-page/unfavorited.png"; + } catch (error) { console.error('Error removing favorite team:', error); } From 1e6fd3794d8682f742b84b2bb1ed93feb413afb7 Mon Sep 17 00:00:00 2001 From: Vishal Vunnam Date: Mon, 22 Apr 2024 17:26:10 -0600 Subject: [PATCH 7/7] Added logout --- ProjectSourceCode/src/index.js | 10 ++++++++++ .../src/views/partials/navigation-bar/accountinfo.hbs | 1 + 2 files changed, 11 insertions(+) diff --git a/ProjectSourceCode/src/index.js b/ProjectSourceCode/src/index.js index f9dc19f..7894c9b 100644 --- a/ProjectSourceCode/src/index.js +++ b/ProjectSourceCode/src/index.js @@ -246,6 +246,16 @@ app.get('/home', (req, res) => { const loggedIn = req.session.user ? true : false; res.render('pages/home'); }); +app.get('/logout', (req, res) => { + req.session.destroy(err => { + if (err) { + console.error('Error destroying session:', err); + res.status(500).send('Internal Server Error'); + } else { + res.render('pages/home', { message: 'Logged out Successfully' }); + } + }); +}); /************************ League Page Routes diff --git a/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs b/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs index 12ec483..95a2e3d 100644 --- a/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs +++ b/ProjectSourceCode/src/views/partials/navigation-bar/accountinfo.hbs @@ -7,5 +7,6 @@ {{this.teamname}}
{{/each}} + Logout \ No newline at end of file