login feature is fully complete, testing is required, favorite teams database is fully functioning

This commit is contained in:
Vishal Vunnam
2024-04-19 19:19:42 -06:00
parent 86dbef4bf1
commit ac19be3cd9
6 changed files with 129 additions and 17 deletions

View File

@@ -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
}
}
// *****************************************************
// <!-- Section 5 : Start Server-->