Created new users database with userID, Created favoriteteams database, implemented add favorite team function but not working just yet
This commit is contained in:
@@ -181,23 +181,35 @@ app.get('/register', (req, res) => {
|
|||||||
|
|
||||||
// Trigger Registration Form to Post
|
// Trigger Registration Form to Post
|
||||||
app.post('/register', async (req, res) => {
|
app.post('/register', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
if (!req.body.username || !req.body.password) {
|
if (!req.body.username || !req.body.password) {
|
||||||
// If username or password is missing, respond with status 400 and an error message
|
// If username or password is missing, respond with status 400 and an error message
|
||||||
return res.status(400).json({ status: 'error', message: 'Invalid input' });
|
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);
|
|
||||||
|
|
||||||
// Insert username and hashed password into the 'users' table
|
// Check if the username already exists in the database
|
||||||
await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]);
|
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
|
// Hash the password using bcrypt library
|
||||||
res.status(200).json({ status: 'success', message: 'Registration successful' });
|
const hash = await bcrypt.hash(req.body.password, 10);
|
||||||
} catch (error) {
|
|
||||||
// If an error occurs during registration, respond with status 500 and an error message
|
// Insert username and hashed password into the 'users' table
|
||||||
res.status(500).json({ status: 'error', message: 'An error occurred during registration' });
|
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
|
// database configuration
|
||||||
|
|
||||||
app.post('/courses/add', async (req, res) => {
|
app.post('/courses/add', async (req, res) => {
|
||||||
const { userID, teamID, teamName, teamLogo } = req.body;
|
|
||||||
|
|
||||||
try {
|
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(
|
const { team_count } = await db.one(
|
||||||
'SELECT COUNT(*) AS team_count FROM FavoriteTeams WHERE UserID = $1',
|
'SELECT COUNT(*) AS team_count FROM FavoriteTeams WHERE UserID = $1',
|
||||||
[userID]
|
[userID]
|
||||||
@@ -244,6 +260,7 @@ app.post('/courses/add', async (req, res) => {
|
|||||||
console.log('User has reached the maximum number of favorite teams.');
|
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.' });
|
return res.status(400).json({ message: 'User has reached the maximum number of favorite teams.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const query = {
|
const query = {
|
||||||
text: 'INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)',
|
text: 'INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)',
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ CREATE TABLE users (
|
|||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE FavoriteTeams (
|
CREATE TABLE FavoriteTeams (
|
||||||
UserID INT,
|
TeamID INT PRIMARY KEY,
|
||||||
TeamID INT,
|
UserID INT REFERENCES users(UserID),
|
||||||
TeamName VARCHAR(50),
|
TeamName VARCHAR(50),
|
||||||
TeamLogo VARCHAR(100)
|
TeamLogo VARCHAR(100)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
SELECT FavoriteTeams.*
|
||||||
|
FROM FavoriteTeams
|
||||||
|
JOIN users ON FavoriteTeams.UserID = users.UserID
|
||||||
|
WHERE users.UserID = <user_id>;
|
||||||
@@ -15,7 +15,7 @@ const fetchClubsData = async (req, res, next) => {
|
|||||||
|
|
||||||
// Extract relevant data from the API response
|
// Extract relevant data from the API response
|
||||||
const clubData = response.data;
|
const clubData = response.data;
|
||||||
|
res.locals.user = users
|
||||||
// Attach the data to res.locals
|
// Attach the data to res.locals
|
||||||
res.locals.club = {
|
res.locals.club = {
|
||||||
area: {
|
area: {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const fetchLeaguesData = async (req, res, next) => {
|
|||||||
const leagueData = response.data;
|
const leagueData = response.data;
|
||||||
|
|
||||||
// Attach the data to res.locals
|
// 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)
|
console.log(req.session.user)
|
||||||
res.locals.league = {
|
res.locals.league = {
|
||||||
area: {
|
area: {
|
||||||
|
|||||||
@@ -8,9 +8,7 @@ module.exports = function generateLeagueRoutes(app) {
|
|||||||
// Define a route to handle requests to "/league/:leagueName"
|
// Define a route to handle requests to "/league/:leagueName"
|
||||||
app.get('/league/:leagueID', (req, res) => {
|
app.get('/league/:leagueID', (req, res) => {
|
||||||
// Extract the league name from the URL parameters
|
// Extract the league name from the URL parameters
|
||||||
console.log("jskjfhskjhdfkjh")
|
|
||||||
const leagueID = req.params.leagueID;
|
const leagueID = req.params.leagueID;
|
||||||
const user = req.session.user;
|
|
||||||
// Render the league page template using Handlebars
|
// Render the league page template using Handlebars
|
||||||
res.render('pages/leagues-page', { leagueID: leagueID, user: user});
|
res.render('pages/leagues-page', { leagueID: leagueID, user: user});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user