From 86dbef4bf12ce10f6dba78dcc04da866c8a95c7c Mon Sep 17 00:00:00 2001 From: Vishal Vunnam Date: Wed, 17 Apr 2024 21:45:27 -0600 Subject: [PATCH] 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}); });