finished addfavorite team, page recognizes when team is favorited or not. remove favorite team currently not working
This commit is contained in:
@@ -129,8 +129,13 @@ app.get('/club/:clubID', [fetchClubsData], (req, res) => {
|
||||
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);
|
||||
const isTeamIDInFavTeams = fav_teams.some(team => {
|
||||
const teamIdInt = parseInt(team.teamid);
|
||||
const clubIdInt = parseInt(req.params.clubID);
|
||||
console.log('Checking team:', teamIdInt);
|
||||
console.log('equal to', clubIdInt);
|
||||
return teamIdInt === clubIdInt;
|
||||
});
|
||||
if (isTeamIDInFavTeams) {
|
||||
isFav = true
|
||||
}
|
||||
@@ -274,25 +279,6 @@ app.post('/favteam/add', async (req, res, next) => {
|
||||
return res.status(400).json({ message: 'Login or register to add a favorite team.' });
|
||||
}
|
||||
|
||||
// // 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]
|
||||
// );
|
||||
// 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.' });
|
||||
// }
|
||||
|
||||
// Insert the new favorite team into the database
|
||||
const query = {
|
||||
text: 'INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)',
|
||||
@@ -309,10 +295,6 @@ app.post('/favteam/add', async (req, res, next) => {
|
||||
});
|
||||
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
|
||||
@@ -320,8 +302,10 @@ app.post('/favteam/remove', async (req, res) => {
|
||||
'SELECT * FROM FavoriteTeams WHERE UserID = $1 AND TeamName = $2',
|
||||
[userID, teamName]
|
||||
);
|
||||
|
||||
// If the team does not exist for the user, return a 404 error
|
||||
if (!existingTeam) {
|
||||
return res.status(400).json({ message: 'This team is not in your favorites.' });
|
||||
return res.status(404).json({ message: 'This team is not in your favorites.' });
|
||||
}
|
||||
|
||||
// Remove the favorite team from the database
|
||||
@@ -334,9 +318,17 @@ app.post('/favteam/remove', async (req, res) => {
|
||||
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' });
|
||||
|
||||
// If the error is a database error, return a 500 error
|
||||
if (error instanceof QueryResultError) {
|
||||
return res.status(500).json({ error: 'Database error occurred while removing favorite team' });
|
||||
}
|
||||
|
||||
// If the error is a generic error, return a 400 error
|
||||
return res.status(400).json({ error: 'Error occurred while removing favorite team' });
|
||||
}
|
||||
});
|
||||
|
||||
async function getFavoriteTeamsForUser(userId) {
|
||||
try {
|
||||
// Execute the SQL query
|
||||
|
||||
Reference in New Issue
Block a user