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
29 changes: 23 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,47 @@ 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();

// ℹ️ 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;
33 changes: 33 additions & 0 deletions models/Movie.model.js
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 30 additions & 2 deletions seeds/movies.seed.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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
// ... your code here
6 changes: 4 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -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, () => {
Expand Down
18 changes: 18 additions & 0 deletions views/index.hbs
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
<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>