From d251bcf4f267b044cfb3e95a95c17d5aa7d370c6 Mon Sep 17 00:00:00 2001 From: Shashank519915 <120128150+Shashank519915@users.noreply.github.com> Date: Sun, 4 Feb 2024 16:07:38 +0530 Subject: [PATCH 1/7] Update app.js --- app.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index a037764..7f25189 100644 --- a/app.js +++ b/app.js @@ -5,12 +5,10 @@ require('dotenv/config'); // âšī¸ Connects to the database require('./db'); -// Handles http requests (express is node js framework) +// Handles http requests (express is a Node.js framework) // https://www.npmjs.com/package/express const express = require('express'); - -// Handles the handlebars -// https://www.npmjs.com/package/hbs +const exphbs = require('express-handlebars'); const hbs = require('hbs'); const app = express(); @@ -18,17 +16,36 @@ const app = express(); // âšī¸ This function is getting exported from the config folder. It runs most middlewares require('./config')(app); +// Set up Handlebars as the view engine +app.engine('hbs', exphbs({ extname: 'hbs' })); +app.set('view engine', 'hbs'); + // default value for title local const projectName = 'lab-express-cinema'; const capitalized = string => string[0].toUpperCase() + string.slice(1).toLowerCase(); - app.locals.title = `${capitalized(projectName)}- Generated with Ironlauncher`; // đ Start handling routes here const index = require('./routes/index'); app.use('/', index); -// â To handle errors. Routes that don't exist or errors that you handle in specific routes + +// â To handle errors. require('./error-handling')(app); + + +// Define a route to render the movies page +app.get('/movies', async (req, res) => { + try { + // Fetch movies from the database + const movies = await MovieModel.find(); + + res.render('movies', { title: 'Movies', movies }); + } catch (error) { + console.error('Error fetching movies:', error); + res.status(500).send('Internal Server Error'); + } + }); + module.exports = app; From 2a82ab41f564b49d561571e6e4bcd2943035694d Mon Sep 17 00:00:00 2001 From: Shashank519915 <120128150+Shashank519915@users.noreply.github.com> Date: Sun, 4 Feb 2024 16:08:28 +0530 Subject: [PATCH 2/7] Create Movie.model.js --- models/Movie.model.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 models/Movie.model.js diff --git a/models/Movie.model.js b/models/Movie.model.js new file mode 100644 index 0000000..4783932 --- /dev/null +++ b/models/Movie.model.js @@ -0,0 +1,33 @@ +// models/Movie.model.js +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; From df39262c56237fd32ae21d2ca3f716777f31aabb Mon Sep 17 00:00:00 2001 From: Shashank519915 <120128150+Shashank519915@users.noreply.github.com> Date: Sun, 4 Feb 2024 16:09:25 +0530 Subject: [PATCH 3/7] Update movies.seed.js --- seeds/movies.seed.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/seeds/movies.seed.js b/seeds/movies.seed.js index 39e1359..9c30fc6 100644 --- a/seeds/movies.seed.js +++ b/seeds/movies.seed.js @@ -1,3 +1,13 @@ +const mongoose = require('mongoose'); +const Movie = require('../models/Movie.model'); + +// Connect to your MongoDB database +mongoose.connect('mongodb://your-mongodb-connection-string', { + useNewUrlParser: true, + useUnifiedTopology: true, +}); + + const movies = [ { title: "A Wrinkle in Time", @@ -84,6 +94,24 @@ const movies = [ // Add here the script that will be run to actually seed the database (feel free to refer to the previous lesson) - + // Seed the database +async function seedDatabase() { + try { + // Remove existing data + await Movie.deleteMany({}); + // Insert new data + await Movie.insertMany(moviesData); + + console.log('Database seeded successfully!'); + } catch (error) { + console.error('Error seeding database:', error); + } finally { + // Close the database connection + mongoose.connection.close(); + } +} + +// Run the seed function +seedDatabase(); -// ... your code here \ No newline at end of file +// ... your code here From 515f91b6ce9b9f797fbaded696989609ea9a1d84 Mon Sep 17 00:00:00 2001 From: Shashank519915 <120128150+Shashank519915@users.noreply.github.com> Date: Sun, 4 Feb 2024 16:09:52 +0530 Subject: [PATCH 4/7] Update server.js --- server.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index b815b16..d3f6fd5 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,8 @@ -const app = require("./app"); +// server.js +const express = require('express'); +const app = express(); + -// âšī¸ Sets the PORT for our app to have access to it. If no env has been set, we hard code it to 3000 const PORT = process.env.PORT || 3000; app.listen(PORT, () => { From 939a9795033020f0141a66c87e2f3f8002ccb84e Mon Sep 17 00:00:00 2001 From: Shashank519915 <120128150+Shashank519915@users.noreply.github.com> Date: Sun, 4 Feb 2024 16:10:42 +0530 Subject: [PATCH 5/7] Update index.hbs --- views/index.hbs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/views/index.hbs b/views/index.hbs index 1f308fd..3e3f48c 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -1,2 +1,20 @@
Welcome to {{title}}
+ + + + + +Director: {{movie.director}}
+Stars: {{movie.stars.join(', ')}}
+Showtimes: {{movie.showtimes.join(', ')}}
+{{movie.description}}
+ + + Back to Movies + + From 994dafae3632a6a352f4daf38808557747adeb5b Mon Sep 17 00:00:00 2001 From: Shashank519915 <120128150+Shashank519915@users.noreply.github.com> Date: Sun, 4 Feb 2024 16:12:08 +0530 Subject: [PATCH 7/7] Create movies.hbs --- views/movies.hbs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 views/movies.hbs diff --git a/views/movies.hbs b/views/movies.hbs new file mode 100644 index 0000000..9a6e3b5 --- /dev/null +++ b/views/movies.hbs @@ -0,0 +1,21 @@ + + + + + +