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
|
||||
|
||||
@@ -5,7 +5,7 @@ CREATE TABLE users (
|
||||
);
|
||||
|
||||
CREATE TABLE FavoriteTeams (
|
||||
TeamID INT PRIMARY KEY,
|
||||
TeamID INT,
|
||||
UserID INT REFERENCES users(UserID),
|
||||
TeamName VARCHAR(50),
|
||||
TeamLogo VARCHAR(100)
|
||||
|
||||
@@ -48,5 +48,9 @@
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
}
|
||||
#teamlogo{
|
||||
width: 23px;
|
||||
height: 23px;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,12 +9,15 @@ document.addEventListener("DOMContentLoaded", function() {
|
||||
|
||||
if (favoriteButton.src.includes("/favorited.png")) {
|
||||
removeFavoriteTeam(userID, teamID)
|
||||
.then(() => {
|
||||
.then((rmv_status) => {
|
||||
console.log(rmv_status);
|
||||
if (rmv_status === 200) {
|
||||
favoriteButton.src = "/img/club-page/unfavorited.png";
|
||||
})
|
||||
.catch(error => {
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error removing favorite team:', error);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
addFavoriteTeam(userID, teamID, teamName, teamLogo)
|
||||
.then(() => {
|
||||
@@ -26,7 +29,8 @@ document.addEventListener("DOMContentLoaded", function() {
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
async function addFavoriteTeam(userID, teamID, teamName, teamLogo){
|
||||
try {
|
||||
const response = await fetch('/favteam/add', {
|
||||
@@ -64,9 +68,6 @@ async function removeFavoriteTeam(userID, teamID) {
|
||||
teamID: teamID
|
||||
})
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to remove favorite team');
|
||||
}
|
||||
console.log('Favorite team removed successfully.');
|
||||
} catch (error) {
|
||||
console.error('Error removing favorite team:', error);
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
<a id="username">Username: {{user.username}}</a>
|
||||
<a style="text-decoration: underline;">Favorite Teams:</a> <br>
|
||||
{{#each fav_teams}}
|
||||
<span>{{this.teamname}}</span><br>
|
||||
<img id="teamlogo" src="{{this.teamlogo}}" alt="teamlogo">
|
||||
<span>{{this.teamname}}</span> <br>
|
||||
{{/each}}
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user