forked from lucaspatenaude/ScoreSpot
login feature is fully complete, testing is required, favorite teams database is fully functioning
This commit is contained in:
@@ -73,6 +73,19 @@ app.use(
|
|||||||
extended: true,
|
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
|
// Serve static files from the 'public' directory
|
||||||
app.use(express.static(path.join(__dirname, 'resources')));
|
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);
|
const user = await db.oneOrNone('SELECT * FROM users WHERE username = $1', req.body.username);
|
||||||
req.session.user = user;
|
req.session.user = user;
|
||||||
req.session.save();
|
req.session.save();
|
||||||
console.log(user);
|
|
||||||
|
|
||||||
// Redirect user to the home page
|
// Redirect user to the home page
|
||||||
res.redirect('/home');
|
res.redirect('/home');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -218,7 +229,7 @@ app.post('/register', async (req, res) => {
|
|||||||
|
|
||||||
app.get('/home', (req, res) => {
|
app.get('/home', (req, res) => {
|
||||||
const loggedIn = req.session.user ? true : false;
|
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
|
// Function to add a new row to the FavoriteTeams table
|
||||||
// database configuration
|
// database configuration
|
||||||
|
|
||||||
app.post('/courses/add', async (req, res) => {
|
app.post('/favteam/add', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
if (!req.session.user){
|
if (!req.session.user){
|
||||||
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.' });
|
||||||
}
|
}
|
||||||
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(
|
const { team_count } = await db.one(
|
||||||
'SELECT COUNT(*) AS team_count FROM FavoriteTeams WHERE UserID = $1',
|
'SELECT COUNT(*) AS team_count FROM FavoriteTeams WHERE UserID = $1',
|
||||||
[userID]
|
[userID]
|
||||||
@@ -260,8 +281,8 @@ app.post('/courses/add', async (req, res) => {
|
|||||||
console.log('User has reached the maximum number of favorite teams.');
|
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.' });
|
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 = {
|
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)',
|
||||||
values: [userID, teamID, teamName, teamLogo],
|
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' });
|
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-->
|
// <!-- Section 5 : Start Server-->
|
||||||
|
|||||||
@@ -30,6 +30,23 @@
|
|||||||
border-color: darkred;
|
border-color: darkred;
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
/* CSS for the account info card */
|
||||||
|
.account-info-card {
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||||
|
padding: 20px;
|
||||||
|
width: 300px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.info-container {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#username {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
document.addEventListener("DOMContentLoaded", function() {
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
|
console.log(clubData.club.club_ID);
|
||||||
var favoriteButton = document.getElementById("club-favorite-button");
|
var favoriteButton = document.getElementById("club-favorite-button");
|
||||||
if (favoriteButton) {
|
if (favoriteButton) {
|
||||||
favoriteButton.addEventListener("click", function() {
|
favoriteButton.addEventListener("click", function() {
|
||||||
if (favoriteButton.src.includes("/favorited.png")) {
|
if (favoriteButton.src.includes("/favorited.png")) {
|
||||||
favoriteButton.src = "/img/club-page/unfavorited.png";
|
favoriteButton.src = "/img/club-page/unfavorited.png";
|
||||||
|
removeFavoriteTeam(1, 3);
|
||||||
} else {
|
} else {
|
||||||
|
console.log("kjhdsgkjh");
|
||||||
favoriteButton.src = "/img/club-page/favorited.png";
|
favoriteButton.src = "/img/club-page/favorited.png";
|
||||||
addFavoriteTeam(1, 65, 'Manchester City FC', 'https://crests.football-data.org/65.png');
|
addFavoriteTeam(1, 3, 'Manchester City FC', 'https://crests.football-data.org/65.png');
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -14,7 +17,8 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
});
|
});
|
||||||
async function addFavoriteTeam(userID, teamID, teamName, teamLogo) {
|
async function addFavoriteTeam(userID, teamID, teamName, teamLogo) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/courses/add', {
|
console.log("yesss")
|
||||||
|
const response = await fetch('/favteam/add', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
@@ -33,4 +37,24 @@ async function addFavoriteTeam(userID, teamID, teamName, teamLogo) {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error adding favorite team:', error);
|
console.error('Error adding favorite team:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async function removeFavoriteTeam(userID, teamID) {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/favteam/remove', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
userID: userID,
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const fetchClubsData = async (req, res, next) => {
|
|||||||
|
|
||||||
// Extract relevant data from the API response
|
// Extract relevant data from the API response
|
||||||
const clubData = response.data;
|
const clubData = response.data;
|
||||||
res.locals.user = users
|
// res.locals.user = users
|
||||||
// Attach the data to res.locals
|
// Attach the data to res.locals
|
||||||
res.locals.club = {
|
res.locals.club = {
|
||||||
area: {
|
area: {
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
<!-- Linking forms.css -->
|
<!-- Linking forms.css -->
|
||||||
<div class="login-page" id="login-page">
|
<div class="login-page account-info-card" id="login-page">
|
||||||
<div class="form-container" id="login-form">
|
<div class="form-container" id="login-form">
|
||||||
<a>{{user.username}}</a>
|
<a id="username">Username: {{user.username}}</a>
|
||||||
</div>
|
<a style="text-decoration: underline;">Favorite Teams:</a> <br>
|
||||||
</div>
|
{{#each fav_teams}}
|
||||||
|
<span>{{this.teamname}}</span><br>
|
||||||
|
{{/each}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -48,7 +48,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
{{#if user}}
|
{{#if user.username}}
|
||||||
<div class="container" id="login-container">
|
<div class="container" id="login-container">
|
||||||
{{> navigation-bar/accountinfo}}
|
{{> navigation-bar/accountinfo}}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user