addFavorite team is working, button and remove will be finished tonight

This commit is contained in:
Vishal Vunnam
2024-04-21 14:11:35 -06:00
parent ac19be3cd9
commit 75d9560293
5 changed files with 87 additions and 85 deletions

View File

@@ -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) => {