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; 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; 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 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, () => { 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 + + 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 @@ + + + + + +