diff --git a/app.js b/app.js index a037764..1e10654 100644 --- a/app.js +++ b/app.js @@ -14,6 +14,8 @@ const express = require('express'); const hbs = require('hbs'); const app = express(); +const path = require('path'); +const Movie = require('./models/Movie.model'); // âšī¸ This function is getting exported from the config folder. It runs most middlewares require('./config')(app); @@ -24,10 +26,40 @@ const capitalized = string => string[0].toUpperCase() + string.slice(1).toLowerC app.locals.title = `${capitalized(projectName)}- Generated with Ironlauncher`; -// đ Start handling routes here +// Set up Handlebars as the view engine +app.set('view engine', 'hbs'); +app.set('views', path.join(__dirname, 'views')); + +// Serve static files from the public directory +app.use(express.static(path.join(__dirname, 'public'))); + +// Define routes const index = require('./routes/index'); app.use('/', index); +app.get('/movies', async (req, res) => { + try { + const movies = await Movie.find(); + res.render('movies', { movies }); + } catch (error) { + console.error('Error fetching movies:', error); + res.status(500).send('Internal Server Error'); + } +}); + +app.get('/movie/:id', async(req, res) => { + try { + const movie = await Movie.findById(req.params.id); + if (!movie) { + return res.status(404).send('Movie not found'); + } + res.render('movieDetails', { movie }); + } catch (error) { + console.error('Error fetching movie details:', error); + res.status(500).send('Internal Server Error'); + } +}); + // â To handle errors. Routes that don't exist or errors that you handle in specific routes require('./error-handling')(app); diff --git a/config/index.js b/config/index.js index 4d9ff5c..4ed0da0 100644 --- a/config/index.js +++ b/config/index.js @@ -28,11 +28,11 @@ module.exports = (app) => { app.use(cookieParser()); // Normalizes the path to the views folder - app.set("views", path.join(__dirname, "..", "views")); + app.set("views", path.join(__dirname, "views")); // Sets the view engine to handlebars app.set("view engine", "hbs"); // Handles access to the public folder - app.use(express.static(path.join(__dirname, "..", "public"))); + app.use(express.static(path.join(__dirname, "public"))); // Handles access to the favicon app.use(favicon(path.join(__dirname, "..", "public", "images", "favicon.ico"))); diff --git a/db/index.js b/db/index.js index c8cf065..4741342 100644 --- a/db/index.js +++ b/db/index.js @@ -5,7 +5,7 @@ const mongoose = require("mongoose"); // âšī¸ Sets the MongoDB URI for our app to have access to it. // If no env has been set, we dynamically set it to whatever the folder name was upon the creation of the app -const MONGO_URI = process.env.MONGODB_URI || "mongodb://localhost/lab-express-cinema"; +const MONGO_URI = process.env.MONGODB_URI || "mongodb://localhost:27017/lab-express-cinema"; mongoose .connect(MONGO_URI) diff --git a/models/Movie.model.js b/models/Movie.model.js new file mode 100644 index 0000000..653d501 --- /dev/null +++ b/models/Movie.model.js @@ -0,0 +1,32 @@ +const mongoose = require('mongoose'); + +const movieSchema = new mongoose.Schema({ + title: { + type: String, + required: true + }, + director: { + type: String, + required: true + }, + stars: { + type: [String], + required: true + }, + image: { + type: String, + required: true + }, + description: { + type: String, + required: true + }, + showtimes: { + type: [String], + required: true + } +}); + +const Movie = mongoose.model('Movie', movieSchema); + +module.exports = Movie; diff --git a/public/stylesheets/index.css b/public/stylesheets/index.css new file mode 100644 index 0000000..947ce31 --- /dev/null +++ b/public/stylesheets/index.css @@ -0,0 +1,44 @@ +body, h1, img { + margin: 0; + padding: 0; +} + + +.container { + max-width: 800px; + margin: 0 auto; + padding: 20px; +} + + +.header { + text-align: center; + margin-bottom: 20px; +} + +h1 { + font-size: 2em; + margin-bottom: 20px; +} + + +.header img { + max-width: 100%; + height: auto; + margin-bottom: 20px; +} + + +.button { + display: inline-block; + padding: 10px 20px; + background-color: #007bff; + color: #fff; + text-decoration: none; + border-radius: 5px; + transition: background-color 0.3s ease; +} + +.button:hover { + background-color: #0056b3; +} diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 9453385..b09a013 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -1,8 +1,50 @@ -body { - padding: 50px; - font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +body, h1, h2, img { + margin: 0; + padding: 0; } -a { - color: #00B7FF; +.container { + max-width: 800px; + margin: 0 auto; + padding: 20px; +} + +h1 { + font-size: 2em; + margin-bottom: 20px; +} + +.movie-list { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); + gap: 20px; +} + +.movie { + border: 1px solid #ccc; + padding: 10px; + text-align: center; +} + +.movie img { + max-width: 100%; + height: auto; +} + +.movie h2 { + margin-top: 10px; +} + +.movie .button { + display: inline-block; + margin-top: 10px; + padding: 5px 10px; + background-color: #007bff; + color: #fff; + text-decoration: none; + border-radius: 5px; +} + +.movie .button:hover { + background-color: #0056b3; } diff --git a/routes/index.js b/routes/index.js index 3647403..8dc03ae 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,4 +4,5 @@ const router = express.Router(); /* GET home page */ router.get('/', (req, res, next) => res.render('index')); + module.exports = router; diff --git a/seeds/movies.seed.js b/seeds/movies.seed.js index 39e1359..4f7e90d 100644 --- a/seeds/movies.seed.js +++ b/seeds/movies.seed.js @@ -1,3 +1,7 @@ +const mongoose = require('mongoose'); +const Movie = require('../models/Movie.model'); + + const movies = [ { title: "A Wrinkle in Time", @@ -86,4 +90,22 @@ const movies = [ -// ... your code here \ No newline at end of file +// ... your code here +mongoose.connect('mongodb://localhost:27017/lab-express-cinema', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => { + console.log('Connected to MongoDB'); + + Movie.create(movies) + .then((createdMovies) => { + console.log(`${createdMovies.length} movies have been created`); + }) + .catch((error) => { + console.error('Error seeding database:', error); + }) + .finally(() => { + mongoose.connection.close(); + }); + }) + .catch((error) => { + console.error('Error connecting to MongoDB:', error); + }); \ No newline at end of file diff --git a/views/index.hbs b/views/index.hbs index 1f308fd..b0c052b 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -1,2 +1,18 @@ -
Welcome to {{title}}
+ + + + + +Director: {{movie.director}}
+Stars: {{movie.stars.join}}
+Description: {{movie.description}}
+Showtimes: {{movie.showtimes.join}}
+