Permalink
Browse files

Add lesson README, starter-code & final-code

  • Loading branch information...
jim-clark
jim-clark committed Feb 9, 2019
0 parents commit 8dc89f8aa045b4f69d2ca434c283e4cfbde266f5
Showing with 3,018 additions and 0 deletions.
  1. +660 −0 README.md
  2. +90 −0 final-code/mongoose-movies/bin/www
  3. +12 −0 final-code/mongoose-movies/config/database.js
  4. +47 −0 final-code/mongoose-movies/controllers/movies.js
  5. +32 −0 final-code/mongoose-movies/controllers/performers.js
  6. +14 −0 final-code/mongoose-movies/controllers/reviews.js
  7. +29 −0 final-code/mongoose-movies/models/movie.js
  8. +11 −0 final-code/mongoose-movies/models/performer.js
  9. +549 −0 final-code/mongoose-movies/package-lock.json
  10. +17 −0 final-code/mongoose-movies/package.json
  11. +46 −0 final-code/mongoose-movies/public/images/camera.svg
  12. +146 −0 final-code/mongoose-movies/public/stylesheets/style.css
  13. +8 −0 final-code/mongoose-movies/routes/index.js
  14. +10 −0 final-code/mongoose-movies/routes/movies.js
  15. +9 −0 final-code/mongoose-movies/routes/performers.js
  16. +7 −0 final-code/mongoose-movies/routes/reviews.js
  17. +46 −0 final-code/mongoose-movies/server.js
  18. +3 −0 final-code/mongoose-movies/views/error.ejs
  19. +11 −0 final-code/mongoose-movies/views/index.ejs
  20. +21 −0 final-code/mongoose-movies/views/movies/index.ejs
  21. +20 −0 final-code/mongoose-movies/views/movies/new.ejs
  22. +67 −0 final-code/mongoose-movies/views/movies/show.ejs
  23. +3 −0 final-code/mongoose-movies/views/partials/footer.ejs
  24. +18 −0 final-code/mongoose-movies/views/partials/header.ejs
  25. +18 −0 final-code/mongoose-movies/views/performers/new.ejs
  26. +90 −0 starter-code/mongoose-movies/bin/www
  27. +12 −0 starter-code/mongoose-movies/config/database.js
  28. +38 −0 starter-code/mongoose-movies/controllers/movies.js
  29. +14 −0 starter-code/mongoose-movies/controllers/reviews.js
  30. +28 −0 starter-code/mongoose-movies/models/movie.js
  31. +549 −0 starter-code/mongoose-movies/package-lock.json
  32. +17 −0 starter-code/mongoose-movies/package.json
  33. +46 −0 starter-code/mongoose-movies/public/images/camera.svg
  34. +132 −0 starter-code/mongoose-movies/public/stylesheets/style.css
  35. +8 −0 starter-code/mongoose-movies/routes/index.js
  36. +10 −0 starter-code/mongoose-movies/routes/movies.js
  37. +7 −0 starter-code/mongoose-movies/routes/reviews.js
  38. +44 −0 starter-code/mongoose-movies/server.js
  39. +3 −0 starter-code/mongoose-movies/views/error.ejs
  40. +11 −0 starter-code/mongoose-movies/views/index.ejs
  41. +21 −0 starter-code/mongoose-movies/views/movies/index.ejs
  42. +20 −0 starter-code/mongoose-movies/views/movies/new.ejs
  43. +54 −0 starter-code/mongoose-movies/views/movies/show.ejs
  44. +3 −0 starter-code/mongoose-movies/views/partials/footer.ejs
  45. +17 −0 starter-code/mongoose-movies/views/partials/header.ejs
660 README.md

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../server');
var debug = require('debug')('mongoose-movies:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
@@ -0,0 +1,12 @@
var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/movies',
{ useNewUrlParser: true }
);

// shortcut to mongoose.connection object
var db = mongoose.connection;

db.on('connected', function () {
console.log(`Connected to MongoDB at ${db.host}:${db.port}`);
});
@@ -0,0 +1,47 @@
var Movie = require('../models/movie');
var Performer = require('../models/performer');

module.exports = {
index,
show,
new: newMovie,
create
};

function index(req, res) {
Movie.find({}, function(err, movies) {
res.render('movies/index', { title: 'All Movies', movies });
});
}

function show(req, res) {
Movie.findById(req.params.id)
.populate('cast').exec(function(err, movie) {
// Performer.find({}).where('_id').nin(movie.cast)
Performer.find({_id: {$nin: movie.cast}})
.exec(function(err, performers) {
console.log(performers);
res.render('movies/show', {
title: 'Movie Detail', movie, performers
});
});
});
}

function newMovie(req, res) {
res.render('movies/new', { title: 'Add Movie' });
}

function create(req, res) {
// convert nowShowing's checkbox of nothing or "on" to boolean
req.body.nowShowing = !!req.body.nowShowing;
for (let key in req.body) {
if (req.body[key] === '') delete req.body[key];
}
var movie = new Movie(req.body);
movie.save(function(err) {
if (err) return res.redirect('/movies/new');
// res.redirect('/movies');
res.redirect(`/movies/${movie._id}`);
});
}
@@ -0,0 +1,32 @@
var Performer = require('../models/performer');
var Movie = require('../models/movie');

module.exports = {
new: newPerformer,
create,
addToCast
};

function addToCast(req, res) {
Movie.findById(req.params.id, function(err, movie) {
movie.cast.push(req.body.performerId);
movie.save(function(err) {
res.redirect(`/movies/${movie._id}`);
});
});
}

function create(req, res) {
Performer.create(req.body, function(err, performer) {
res.redirect('/performers/new');
});
}

function newPerformer(req, res) {
Performer.find({}, function(err, performers) {
res.render('performers/new', {
title: 'Add Performer',
performers
});
})
}
@@ -0,0 +1,14 @@
var Movie = require('../models/movie');

module.exports = {
create
};

function create(req, res) {
Movie.findById(req.params.id, function(err, movie) {
movie.reviews.push(req.body);
movie.save(function(err) {
res.redirect(`/movies/${movie._id}`);
});
});
}
@@ -0,0 +1,29 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var reviewSchema = new Schema({
content: String,
rating: {type: Number, min: 1, max: 5, default: 5}
}, {
timestamps: true
});

var movieSchema = new Schema({
title: {
type: String,
required: true
},
releaseYear: {
type: Number,
default: function () {
return new Date().getFullYear();
}
}, mpaaRating: String,
nowShowing: { type: Boolean, default: false },
reviews: [reviewSchema],
cast: [{type: Schema.Types.ObjectId, ref: 'Performer'}]
}, {
timestamps: true
});

module.exports = mongoose.model('Movie', movieSchema);
@@ -0,0 +1,11 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var performerSchema = new Schema({
name: {type: String, required: true, unique: true},
born: Date
}, {
timestamps: true
});

module.exports = mongoose.model('Performer', performerSchema);
Oops, something went wrong.

0 comments on commit 8dc89f8

Please sign in to comment.