Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// ℹ️ Gets access to environment variables/settings
// https://www.npmjs.com/package/dotenv
require('dotenv/config');

// ℹ️ Connects to the database
require('./db');

// Handles http requests (express is node js framework)
Expand All @@ -12,9 +8,13 @@ const express = require('express');
// Handles the handlebars
// https://www.npmjs.com/package/hbs
const hbs = require('hbs');

const exphbs = require('express-handlebars');
const app = express();

// Set up Handlebars as the view engine
app.engine('hbs', exphbs({ extname: 'hbs' }));
app.set('view engine', 'hbs');

// ℹ️ This function is getting exported from the config folder. It runs most middlewares
require('./config')(app);

Expand All @@ -30,5 +30,16 @@ app.use('/', index);

// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require('./error-handling')(app);
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;
31 changes: 31 additions & 0 deletions models/Movie.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"dependencies": {
"cookie-parser": "^1.4.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express": "^4.18.2",
"express-handlebars": "^7.1.2",
"hbs": "^4.1.1",
"mongoose": "^6.1.2",
"mongoose": "^6.12.5",
"morgan": "^1.10.0",
"serve-favicon": "^2.5.0"
},
Expand Down
24 changes: 21 additions & 3 deletions seeds/movies.seed.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
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",
Expand Down Expand Up @@ -83,7 +92,16 @@ const movies = [


// Add here the script that will be run to actually seed the database (feel free to refer to the previous lesson)
async function seedDatabase() {
try {
await Movie.deleteMany({});
await Movie.insertMany(moviesData);
console.log('Database seeded successfully!');
} catch (error) {
console.error('Error seeding database:', error);
} finally {
mongoose.connection.close();
}
}



// ... your code here
seedDatabase();
3 changes: 2 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const app = require("./app");
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;
Expand Down
20 changes: 18 additions & 2 deletions views/index.hbs
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
<h1>{{title}}</h1>
<p>Welcome to {{title}}</p>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{title}}</title>

</head>
<body>
<div>
<h1>Welcome to the {{title}}</h1>
<p>Discover and explore a collection of amazing movies!</p>
<a href="/movies">
<button>Explore Movies</button>
</a>
</div>
</body>
</html>
20 changes: 20 additions & 0 deletions views/movie-details.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{movie.title}}</title>

</head>
<body>
<h1>{{movie.title}}</h1>
<img src="{{movie.image}}" alt="{{movie.title}}" width="300">
<p>Director: {{movie.director}}</p>
<p>Stars: {{movie.stars.join(', ')}}</p>
<p>Showtimes: {{movie.showtimes.join(', ')}}</p>
<p>{{movie.description}}</p>


<a href="/movies">Back to Movies</a>
</body>
</html>
21 changes: 21 additions & 0 deletions views/movies.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{title}}</title>

</head>
<body>
<h1>All Movies</h1>
<ul>
{{#each movies}}
<li>
<img src="{{this.image}}" alt="{{this.title}}" width="100">
<h2>{{this.title}}</h2>
<a href="/movies/{{this._id}}">See more</a>
</li>
{{/each}}
</ul>
</body>
</html>