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;
|
var fav_teams = res.locals.fav_teams;
|
||||||
if(res.locals.user && fav_teams)
|
if(res.locals.user && fav_teams)
|
||||||
{
|
{
|
||||||
const isTeamIDInFavTeams = fav_teams.some(team => team.teamid === req.params.clubID);
|
const isTeamIDInFavTeams = fav_teams.some(team => {
|
||||||
console.log(isTeamIDInFavTeams);
|
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) {
|
if (isTeamIDInFavTeams) {
|
||||||
isFav = true
|
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.' });
|
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
|
// Insert the new favorite team into the database
|
||||||
const query = {
|
const query = {
|
||||||
text: 'INSERT INTO FavoriteTeams (UserID, TeamID, TeamName, TeamLogo) VALUES ($1, $2, $3, $4)',
|
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) => {
|
app.post('/favteam/remove', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
if (!req.session.user){
|
|
||||||
return res.status(400).json({ message: 'Login or register to remove a favorite team.' });
|
|
||||||
}
|
|
||||||
|
|
||||||
const { userID, teamName } = req.body;
|
const { userID, teamName } = req.body;
|
||||||
|
|
||||||
// Check if the team exists for the user
|
// 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',
|
'SELECT * FROM FavoriteTeams WHERE UserID = $1 AND TeamName = $2',
|
||||||
[userID, teamName]
|
[userID, teamName]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// If the team does not exist for the user, return a 404 error
|
||||||
if (!existingTeam) {
|
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
|
// 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.' });
|
return res.status(200).json({ message: 'Favorite team removed successfully.' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error removing favorite team:', 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) {
|
async function getFavoriteTeamsForUser(userId) {
|
||||||
try {
|
try {
|
||||||
// Execute the SQL query
|
// Execute the SQL query
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ CREATE TABLE users (
|
|||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE FavoriteTeams (
|
CREATE TABLE FavoriteTeams (
|
||||||
TeamID INT PRIMARY KEY,
|
TeamID INT,
|
||||||
UserID INT REFERENCES users(UserID),
|
UserID INT REFERENCES users(UserID),
|
||||||
TeamName VARCHAR(50),
|
TeamName VARCHAR(50),
|
||||||
TeamLogo VARCHAR(100)
|
TeamLogo VARCHAR(100)
|
||||||
|
|||||||
@@ -48,5 +48,9 @@
|
|||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
color: #333;
|
color: #333;
|
||||||
}
|
}
|
||||||
|
#teamlogo{
|
||||||
|
width: 23px;
|
||||||
|
height: 23px;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -9,8 +9,11 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
|
|
||||||
if (favoriteButton.src.includes("/favorited.png")) {
|
if (favoriteButton.src.includes("/favorited.png")) {
|
||||||
removeFavoriteTeam(userID, teamID)
|
removeFavoriteTeam(userID, teamID)
|
||||||
.then(() => {
|
.then((rmv_status) => {
|
||||||
|
console.log(rmv_status);
|
||||||
|
if (rmv_status === 200) {
|
||||||
favoriteButton.src = "/img/club-page/unfavorited.png";
|
favoriteButton.src = "/img/club-page/unfavorited.png";
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error('Error removing favorite team:', error);
|
console.error('Error removing favorite team:', error);
|
||||||
@@ -27,6 +30,7 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
async function addFavoriteTeam(userID, teamID, teamName, teamLogo){
|
async function addFavoriteTeam(userID, teamID, teamName, teamLogo){
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/favteam/add', {
|
const response = await fetch('/favteam/add', {
|
||||||
@@ -64,9 +68,6 @@ async function removeFavoriteTeam(userID, teamID) {
|
|||||||
teamID: teamID
|
teamID: teamID
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error('Failed to remove favorite team');
|
|
||||||
}
|
|
||||||
console.log('Favorite team removed successfully.');
|
console.log('Favorite team removed successfully.');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error removing favorite team:', error);
|
console.error('Error removing favorite team:', error);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
<a id="username">Username: {{user.username}}</a>
|
<a id="username">Username: {{user.username}}</a>
|
||||||
<a style="text-decoration: underline;">Favorite Teams:</a> <br>
|
<a style="text-decoration: underline;">Favorite Teams:</a> <br>
|
||||||
{{#each fav_teams}}
|
{{#each fav_teams}}
|
||||||
|
<img id="teamlogo" src="{{this.teamlogo}}" alt="teamlogo">
|
||||||
<span>{{this.teamname}}</span> <br>
|
<span>{{this.teamname}}</span> <br>
|
||||||
{{/each}}
|
{{/each}}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user