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
|
||||
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) => {
|
||||
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;
|
||||
|
||||
try {
|
||||
//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]
|
||||
@@ -245,6 +261,7 @@ app.post('/courses/add', async (req, res) => {
|
||||
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],
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
|
||||
@@ -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
|
||||
const clubData = response.data;
|
||||
|
||||
res.locals.user = users
|
||||
// Attach the data to res.locals
|
||||
res.locals.club = {
|
||||
area: {
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user