2024-05-03 02:35:24 -06:00
// *****************************************************
// <!-- Section 1 : Import Dependencies -->
// *****************************************************
const express = require ( "express" ) ; // To build an application server or API
const app = express ( ) ;
const handlebars = require ( "express-handlebars" ) ;
const Handlebars = require ( "handlebars" ) ;
const path = require ( "path" ) ;
const bodyParser = require ( "body-parser" ) ;
const session = require ( "express-session" ) ; // To set the session object. To store or access session data, use the `req.session`, which is (generally) serialized as JSON by the store.
const bcrypt = require ( "bcryptjs" ) ; // To hash passwords
const axios = require ( "axios" ) ; // To make HTTP requests from our server. We'll learn more about it in Part C.
const moment = require ( "moment" ) ; // To extract current time data
// *****************************************************
// <!-- Section 2 : Connect to DB -->
// *****************************************************
2024-05-28 00:49:06 -05:00
// Start the Database
const db = require ( "./database/db" ) ; // Import the db module
2024-05-03 02:35:24 -06:00
// *****************************************************
2024-05-28 00:49:06 -05:00
// <!-- Section 3 : Handlebars COnfiguration -->
2024-05-03 02:35:24 -06:00
// *****************************************************
2024-05-28 00:49:06 -05:00
// express-handlebars is a Handlebars view engine for Express. Handlebars.js is a popular templating engine that is powerful, flexible, and helps to create reusable HTML templates.
const hbs = require ( "./config/handlebars" ) ; // Import the hbs module
2024-05-03 02:35:24 -06:00
// Register `hbs` as our view engine using its bound `engine()` function.
app . engine ( "hbs" , hbs . engine ) ;
app . set ( "view engine" , "hbs" ) ;
2024-05-28 00:49:06 -05:00
app . set ( "views" , path . join ( _ _dirname , "../public/views" ) ) ;
app . use ( express . static ( path . join ( _ _dirname , "../public/assets" ) ) ) ; // Serve asset files from the 'public/assets' directory
2024-05-03 02:35:24 -06:00
app . use ( bodyParser . json ( ) ) ; // specify the usage of JSON for parsing request body.
2024-05-28 00:49:06 -05:00
// *****************************************************
// <!-- Section 4 : Session Setup -->
// *****************************************************
2024-05-29 01:16:31 -05:00
const sessionConfig = require ( "./config/session" ) ; // Import the hbs module
sessionConfig ( app ) ;
2024-05-28 00:49:06 -05:00
// *****************************************************
// <!-- Section 5 : Website Routes -->
// *****************************************************
2024-05-03 02:35:24 -06:00
// *****************************************************
// <!-- Section 4 : Middleware -->
// *****************************************************
// Middleware to automatically update live scoreboard
const fetchMatchesData = require ( "./middleware/navigation-bar/current-match-information" ) ;
app . use ( fetchMatchesData ) ;
//Middleware to automatically update in-game time abbreviations
const convert _time = require ( "./middleware/navigation-bar/convert-time" ) ;
app . use ( convert _time ) ;
2024-05-31 02:44:25 -05:00
// Other middleware and route imports
const leagueRoutes = require ( "./routes/league-pages/league-routes" ) ;
leagueRoutes ( app ) ;
2024-05-03 02:35:24 -06:00
// Clubs Page Middleware
const fetchClubsData = require ( "./middleware/clubs-page/get-current-club-information" ) ;
app . get ( "/club/:clubID" , [ fetchClubsData ] , ( req , res ) => {
// Render the Handlebars view with league data
var isFav = false ;
var fav _teams = res . locals . fav _teams ;
if ( res . locals . user && fav _teams ) {
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 ;
}
}
res . render ( "pages/clubs-page" , {
isFav : isFav ,
clubID : req . params . clubID ,
clubs : res . locals . club ,
} ) ;
} ) ;
// *****************************************************
2024-05-29 01:16:31 -05:00
// <!-- Section 5 : Routes -->
2024-05-03 02:35:24 -06:00
// *****************************************************
2024-05-29 01:23:22 -05:00
/ * * * * * * * * * * * * * * * * * * * * * * * *
Home Page Routes
* * * * * * * * * * * * * * * * * * * * * * * * * /
2024-05-29 01:16:31 -05:00
// Redirect to the home page
2024-05-03 02:35:24 -06:00
app . get ( "/" , ( req , res ) => {
res . redirect ( "/home" ) ;
} ) ;
2024-05-29 01:16:31 -05:00
// Render the home page
app . get ( "/home" , ( req , res ) => {
const loggedIn = req . session . user ? true : false ;
res . render ( "pages/home" ) ;
} ) ;
/ * * * * * * * * * * * * * * * * * * * * * * * *
Login Page Routes
* * * * * * * * * * * * * * * * * * * * * * * * * /
2024-05-28 20:42:45 -05:00
// Account Routes
2024-05-29 01:23:22 -05:00
const loginRoutes = require ( "./routes/account/login" ) ;
2024-05-28 20:42:45 -05:00
app . use ( "/" , loginRoutes ) ;
2024-05-03 02:35:24 -06:00
/ * * * * * * * * * * * * * * * * * * * * * * * *
Registration Page Routes
* * * * * * * * * * * * * * * * * * * * * * * * * /
2024-05-29 01:16:31 -05:00
// Account Routes
2024-05-29 01:23:22 -05:00
const registerRoutes = require ( "./routes/account/register" ) ;
2024-05-29 01:16:31 -05:00
app . use ( "/" , registerRoutes ) ;
2024-05-03 02:35:24 -06:00
/ * * * * * * * * * * * * * * * * * * * * * * * *
2024-05-29 01:23:22 -05:00
Logout Route
2024-05-03 02:35:24 -06:00
* * * * * * * * * * * * * * * * * * * * * * * * * /
2024-05-29 01:23:22 -05:00
const logoutRoute = require ( "./routes/account/logout" ) ;
app . use ( "/" , logoutRoute ) ;
2024-05-03 02:35:24 -06:00
/ * * * * * * * * * * * * * * * * * * * * * * * *
League Page Routes
* * * * * * * * * * * * * * * * * * * * * * * * * /
// Import and call generateLeagueRoutes function
const generateLeagueRoutes = require ( "./routes/league-pages/generate-league-routes" ) ;
generateLeagueRoutes ( app ) ;
/ * * * * * * * * * * * * * * * * * * * * * * * *
Club Page Routes
* * * * * * * * * * * * * * * * * * * * * * * * * /
// Import and call generateLeagueRoutes function
const generateClubRoutes = require ( "./routes/club-pages/generate-club-routes" ) ;
generateClubRoutes ( app ) ;
/ * * * * * * * * * * * * * * * * * * * * * * * *
Favorite Team Database
* * * * * * * * * * * * * * * * * * * * * * * * * /
2024-05-30 23:26:02 -05:00
const databaseRoutes = require ( "./routes/database/database-routes" ) . router ;
2024-05-28 02:41:40 -05:00
app . use ( "/" , databaseRoutes ) ;
2024-05-28 00:49:06 -05:00
// *****************************************************
// <!-- Section 6 : Export the App Module to Index.js -->
// *****************************************************
// Export the app object to index.js
module . exports = app ;