diff --git a/ProjectSourceCode/src/index.js b/ProjectSourceCode/src/index.js index c54a6c7..5df08c9 100644 --- a/ProjectSourceCode/src/index.js +++ b/ProjectSourceCode/src/index.js @@ -78,18 +78,30 @@ app.use( app.use(express.static(path.join(__dirname, 'resources'))); // ***************************************************** -// +// +// ***************************************************** + +// Importing middleware +const fetchMatchesData = require('./resources/routes/navigation-bar/current-match-information'); +const convert_time = require('./resources/js/navigation-bar/scoreboard-header/convert-time'); +const fetchLeaguesData = require('./resources/routes/league-pages/get-current-league-information'); + +// Using middleware +app.use(fetchMatchesData); +app.use(convert_time); +app.use(fetchLeaguesData); + +// ***************************************************** +// // ***************************************************** /************************ - Header Scoreboard Routes + League Page Routes *************************/ -const fetchMatchesData = require('./resources/routes/navigation-bar/current-match-information'); -app.use(fetchMatchesData); - -const convert_time = require('./resources/js/navigation-bar/scoreboard-header/convert-time'); -app.use(convert_time); +// Import and call generateLeagueRoutes function +const generateLeagueRoutes = require('./resources/routes/league-pages/generate-league-routes'); +generateLeagueRoutes(app); /************************ Login Page Routes @@ -98,91 +110,83 @@ app.use(convert_time); // Redirect to the /login endpoint app.get('/', (req, res) => { res.redirect('/home'); - }); +}); - // Render login page for /login route - app.get('/login', (req, res) => { +// Render login page for /login route +app.get('/login', (req, res) => { res.render('pages/home'); - }); +}); - // Trigger login form to check database for matching username and password - app.post('/login', async (req, res) => { +// Trigger login form to check database for matching username and password +app.post('/login', async (req, res) => { try { + // Check if username exists in DB + const user = await db.oneOrNone('SELECT * FROM users WHERE username = $1', req.body.username); - // Check if username exists in DB - const user = await db.oneOrNone('SELECT * FROM users WHERE username = $1', req.body.username); + if (!user) { + // Redirect user to login screen if no user is found with the provided username + return res.redirect('/register'); + } - if (!user) { - // Redirect user to login screen if no user is found with the provided username - return res.redirect('/register'); - } + // Check if password from request matches with password in DB + const match = await bcrypt.compare(req.body.password, user.password); - // Check if password from request matches with password in DB - const match = await bcrypt.compare(req.body.password, user.password); + // Check if match returns no data + if (!match) { + // Render the login page with the message parameter + return res.render('pages/home', { message: 'Password does not match' }); + } - // Check if mathc returns no data - if (!match) { - // Render the login page with the message parameter - return res.render('pages/home', { message: 'Password does not match' }); - } + // Save user information in the session variable + req.session.user = user; + req.session.save(); - // Save user information in the session variable - req.session.user = user; - req.session.save(); - - // Redirect user to the home page - res.redirect('/home'); - + // Redirect user to the home page + res.redirect('/home'); } catch (error) { - // Direct user to login screen if no user is found with matching password - res.redirect('/register'); + // Direct user to login screen if no user is found with matching password + res.redirect('/register'); } - }); +}); - /************************ - Registration Page Routes - *************************/ +/************************ + Registration Page Routes +*************************/ - // Render registration page for /register route - app.get('/register', (req, res) => { +// Render registration page for /register route +app.get('/register', (req, res) => { res.render('pages/register'); - }); - +}); // Trigger Registration Form to Post app.post('/register', async (req, res) => { - try { - if (!req.body.username || !req.body.password) { - // If username or password is missing, respond with status 400 and an error message - return res.status(400).json({ status: 'error', message: 'Invalid input' }); + try { + if (!req.body.username || !req.body.password) { + // If username or password is missing, respond with status 400 and an error message + return res.status(400).json({ status: 'error', message: 'Invalid input' }); + } + // Hash the password using bcrypt library + const hash = await bcrypt.hash(req.body.password, 10); + + // Insert username and hashed password into the 'users' table + await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]); + + // Redirect user to the login page after successful registration + res.status(200).json({ status: 'success', message: 'Registration successful' }); + } catch (error) { + // If an error occurs during registration, respond with status 500 and an error message + res.status(500).json({ status: 'error', message: 'An error occurred during registration' }); } - // Hash the password using bcrypt library - const hash = await bcrypt.hash(req.body.password, 10); - - // Insert username and hashed password into the 'users' table - await db.none('INSERT INTO users (username, password) VALUES ($1, $2)', [req.body.username, hash]); - - // Redirect user to the login page after successful registration - res.status(200).json({ status: 'success', message: 'Registration successful' }); - } catch (error) { - // If an error occurs during registration, respond with status 500 and an error message - res.status(500).json({ status: 'error', message: 'An error occurred during registration' }); - } }); - /************************ Home Page Routes *************************/ app.get('/home', (req, res) => { - res.render('pages/home'); + res.render('pages/home'); }); -// Import and call generateLeagueRoutes function -const generateLeagueRoutes = require('./resources/routes/league-pages/generate-league-routes'); -generateLeagueRoutes(app); - // ***************************************************** // // ***************************************************** diff --git a/ProjectSourceCode/src/resources/routes/league-pages/get-current-league-information.js b/ProjectSourceCode/src/resources/routes/league-pages/get-current-league-information.js index b2fc193..45badbd 100644 --- a/ProjectSourceCode/src/resources/routes/league-pages/get-current-league-information.js +++ b/ProjectSourceCode/src/resources/routes/league-pages/get-current-league-information.js @@ -1,18 +1,18 @@ const axios = require('axios'); -// Middleware function to fetch leaguees data -const fetchleagueesData = async (req, res, next) => { +// Middleware function to fetch leagues data +const fetchLeaguesData = async (req, res, next) => { try { - // Array of years to fetch leaguees data - const league_ids = [2021]; /* Readd , 2002, 2014, 2019, 2015, 2013 */ + // Array of years to fetch leagues data + const league_ids = [2021]; /* Add more league IDs if needed */ - // Array to store all leaguees data + // Array to store all leagues data let allLeagues = []; - // Loop through each year and fetch leaguees data + // Loop through each year and fetch leagues data for (const league_id of league_ids) { const response = await axios({ - url: `http://api.football-data.org/v4/competitions/${league_id}/standings?season`, /* Resinsert ${league_id} for 2021 */ + url: `http://api.football-data.org/v4/competitions/${league_id}/standings?season`, method: 'GET', headers: { 'X-Auth-Token': '0aa1ed31245d4a36b1ef5a79150324b3', // Add your API key here @@ -20,34 +20,37 @@ const fetchleagueesData = async (req, res, next) => { }); // Extract relevant data from the API response - const leagues = response.data.leagues.map(league => ({ + const league_data = response.data.standings[0].table.map(team => ({ competition: { - league_id: league.competition.id, - league_name: league.competition.name, - league_emblem: league.competition.emblem + league_id: response.data.competition.id, + league_name: response.data.competition.name, + league_emblem: response.data.competition.emblem }, standings: { table: { - league_position: league.standings.table.position, - team_id: league.standings.table.team.id, - team_name: league.standings.table.team.name, - team_crest: league.standings.table.team.crest + league_position: team.position, + team_id: team.team.id, + team_name: team.team.name, + team_crest: team.team.crest }, }, })); - // Concatenate leaguees data to allleaguees array - allLeagues = allLeagues.concat(leagues); + // Concatenate leagues data to allLeagues array + allLeagues = allLeagues.concat(league_data); } - // Attach all leaguees data to res.locals + // Attach all leagues data to res.locals res.locals.leagues = allLeagues; next(); - } catch (error) { - console.error('Error fetching leaguees data:', error); + } + + catch (error) + { + console.error('Error fetching leagues data:', error); res.locals.leagues = []; // Set an empty array if there's an error next(); // Call next middleware or route handler } }; -module.exports = fetchleagueesData; \ No newline at end of file +module.exports = fetchLeaguesData; diff --git a/ProjectSourceCode/src/views/pages/league-page.hbs b/ProjectSourceCode/src/views/pages/league-page.hbs index 5e1ed52..1036cb7 100644 --- a/ProjectSourceCode/src/views/pages/league-page.hbs +++ b/ProjectSourceCode/src/views/pages/league-page.hbs @@ -2,7 +2,7 @@
- League Emblem + League Emblem
diff --git a/ProjectSourceCode/src/views/partials/footer.hbs b/ProjectSourceCode/src/views/partials/footer.hbs index 3f78c75..0ea6ed2 100644 --- a/ProjectSourceCode/src/views/partials/footer.hbs +++ b/ProjectSourceCode/src/views/partials/footer.hbs @@ -12,7 +12,7 @@ - + \ No newline at end of file