diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/model/movie.js b/model/movie.js new file mode 100644 index 0000000..cc36b36 --- /dev/null +++ b/model/movie.js @@ -0,0 +1,11 @@ +'use strict'; + +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; + +const movieSchema = Schema({ + name: {type: String, required:true}, + dateReleased: { type: Date, required: true} +}); + +module.exports = mongoose.model('movie', movieSchema); diff --git a/model/npm-debug.log b/model/npm-debug.log new file mode 100644 index 0000000..31ea8b9 --- /dev/null +++ b/model/npm-debug.log @@ -0,0 +1,48 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/Users/Andymartin/.nvm/versions/node/v6.11.1/bin/node', +1 verbose cli '/Users/Andymartin/.nvm/versions/node/v6.11.1/bin/npm', +1 verbose cli 'run', +1 verbose cli 'test' ] +2 info using npm@3.10.10 +3 info using node@v6.11.1 +4 verbose run-script [ 'pretest', 'test', 'posttest' ] +5 info lifecycle 13-mongodb@1.0.0~pretest: 13-mongodb@1.0.0 +6 silly lifecycle 13-mongodb@1.0.0~pretest: no script for pretest, continuing +7 info lifecycle 13-mongodb@1.0.0~test: 13-mongodb@1.0.0 +8 verbose lifecycle 13-mongodb@1.0.0~test: unsafe-perm in lifecycle true +9 verbose lifecycle 13-mongodb@1.0.0~test: PATH: /Users/Andymartin/.nvm/versions/node/v6.11.1/lib/node_modules/npm/bin/node-gyp-bin:/Users/Andymartin/codefellows/401/labs/week-3/13-mongodb/node_modules/.bin:/Users/Andymartin/.nvm/versions/node/v6.11.1/bin:/Users/Andymartin/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin +10 verbose lifecycle 13-mongodb@1.0.0~test: CWD: /Users/Andymartin/codefellows/401/labs/week-3/13-mongodb +11 silly lifecycle 13-mongodb@1.0.0~test: Args: [ '-c', 'DEBUG=\'actors*\' mocha' ] +12 silly lifecycle 13-mongodb@1.0.0~test: Returned: code: 2 signal: null +13 info lifecycle 13-mongodb@1.0.0~test: Failed to exec test script +14 verbose stack Error: 13-mongodb@1.0.0 test: `DEBUG='actors*' mocha` +14 verbose stack Exit status 2 +14 verbose stack at EventEmitter. (/Users/Andymartin/.nvm/versions/node/v6.11.1/lib/node_modules/npm/lib/utils/lifecycle.js:255:16) +14 verbose stack at emitTwo (events.js:106:13) +14 verbose stack at EventEmitter.emit (events.js:191:7) +14 verbose stack at ChildProcess. (/Users/Andymartin/.nvm/versions/node/v6.11.1/lib/node_modules/npm/lib/utils/spawn.js:40:14) +14 verbose stack at emitTwo (events.js:106:13) +14 verbose stack at ChildProcess.emit (events.js:191:7) +14 verbose stack at maybeClose (internal/child_process.js:891:16) +14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5) +15 verbose pkgid 13-mongodb@1.0.0 +16 verbose cwd /Users/Andymartin/codefellows/401/labs/week-3/13-mongodb/model +17 error Darwin 15.6.0 +18 error argv "/Users/Andymartin/.nvm/versions/node/v6.11.1/bin/node" "/Users/Andymartin/.nvm/versions/node/v6.11.1/bin/npm" "run" "test" +19 error node v6.11.1 +20 error npm v3.10.10 +21 error code ELIFECYCLE +22 error 13-mongodb@1.0.0 test: `DEBUG='actors*' mocha` +22 error Exit status 2 +23 error Failed at the 13-mongodb@1.0.0 test script 'DEBUG='actors*' mocha'. +23 error Make sure you have the latest version of node.js and npm installed. +23 error If you do, this is most likely a problem with the 13-mongodb package, +23 error not with npm itself. +23 error Tell the author that this fails on your system: +23 error DEBUG='actors*' mocha +23 error You can get information on how to open an issue for this project with: +23 error npm bugs 13-mongodb +23 error Or if that isn't available, you can get their info via: +23 error npm owner ls 13-mongodb +23 error There is likely additional logging output above. +24 verbose exit [ 1, true ] diff --git a/package.json b/package.json new file mode 100644 index 0000000..503fc33 --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "13-mongodb", + "version": "1.0.0", + "description": "![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) 13: Single Resource Mongo and Express API ===", + "main": "server.js", + "scripts": { + "test": "DEBUG='movie*' mocha", + "start": "DEBUG='movie*' node server.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/andyfiveeleven/13-mongodb.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/andyfiveeleven/13-mongodb/issues" + }, + "homepage": "https://github.com/andyfiveeleven/13-mongodb#readme", + "dependencies": { + "bluebird": "^3.5.0", + "body-parser": "^1.17.2", + "cors": "^2.8.4", + "debug": "^2.6.8", + "express": "^4.15.3", + "mongoose": "^4.11.5", + "morgan": "^1.8.2" + }, + "devDependencies": { + "chai": "^4.1.0", + "mocha": "^3.5.0", + "superagent": "^3.5.2" + } +} diff --git a/route/movie-route.js b/route/movie-route.js new file mode 100644 index 0000000..0427174 --- /dev/null +++ b/route/movie-route.js @@ -0,0 +1,39 @@ +'use strict'; + +const Router = require('express').Router; +const jsonParser = require('body-parser').json(); +const debug = require('debug')('actor:movie-router'); +const Movie = require('../model/movie.js'); +const movieRouter = module.exports = new Router(); + +movieRouter.post('/api/movie', jsonParser, function(req, res, next) { + debug('POST: /api/movie'); + + req.body.dateReleased = new Date(); + new Movie(req.body).save() + .then( movie => res.json(movie)) + .catch(next); +}); + +movieRouter.get('/api/movie/:id', function(req, res, next) { + debug('GET: /api/movie'); + + Movie.findById(req.params.id) + .then( movie => res.json(movie)) + .catch(next); +}); + +movieRouter.put('/api/movie/:id', function(req,res,next) { + debug('PUT: /api/band/:id'); + + Movie.findByIdAndUpdate(req.params.id, req.body, { 'new': true }) //maybe need {new:true}? + .then(movie => res.json(movie)) + .catch(next); +}); + +movieRouter.delete('/api/movie/:id', function(req, res, next) { + debug('DELETe: /api/movie'); + Movie.findByIdAndRemove(req.params.id) + .then(() => res.sendStatus(204)) + .catch(next); +}); diff --git a/server.js b/server.js new file mode 100644 index 0000000..c24103d --- /dev/null +++ b/server.js @@ -0,0 +1,24 @@ +'use strict'; + +const express = require('express'); +const morgan = require('morgan'); +const cors = require('cors') +const Promise = require('bluebird'); +const mongoose = require('mongoose'); +const debug = require ('debug')('movie:server'); +const movieRouter = require('./route/movie-route.js'); + +const app = express(); +const PORT = process.env.PORT || 3000; +const MONGODB_URI = 'mongodb://localhost/actorsinmovie'; + +mongoose.Promise= Promise; +mongoose.connect(MONGODB_URI); + +app.use(cors()); +app.use(morgan('dev')); +app.use(movieRouter); + +app.listen(PORT, ()=> { + debug(`listening on port ${PORT}`); +}); diff --git a/test/movie-test.js b/test/movie-test.js new file mode 100644 index 0000000..e9146c3 --- /dev/null +++ b/test/movie-test.js @@ -0,0 +1,132 @@ +'use strict' + +const expect = require('chai').expect; +const request = require('superagent'); +const Movie = require('../model/movie.js'); +const PORT = process.env.PORT || 3000; +const mongoose = require('mongoose'); + +mongoose.Promise = Promise; +require('../server.js'); + +const url = `http://localhost:${PORT}`; +const exampleMovie = { + name: 'test movie name' +}; + +describe('Movie Routes', function(){ + describe('POST: /api/movie', function(){ + describe('with a valid req body', function() { + after (done => { + if (this.tempMovie){ + Movie.remove({}) + .then( () => done()) + .catch(done); + console.log('finished valid req body') + return; + } + done(); + }); + + it('should return a movie', done => { + request.post(`${url}/api/movie`) + .send(exampleMovie) + .end((err,res) => { + if(err) return done(err); + expect(res.status).to.equal(200); + expect(res.body.name).to.equal('test movie name'); + this.tempMovie = res.body; + done(); + }); + }); + }); + }); + + describe('GET: /api/movie/:id', function(){ + describe('with a valid body', function() { + before(done => { + exampleMovie.dateReleased = new Date(); + new Movie(exampleMovie).save() + .then( movie => { + this.tempMovie = movie; + done(); + }) + .catch(done); + }); + + after(done => { + delete exampleMovie.dateReleased; + if(this.tempMovie) { + Movie.remove({}) + .then( () => done()) + .catch(done); + return; + } + done(); + }); + + it('should return a movie', done => { + request.get(`${url}/api/movie/${this.tempMovie._id}`) + .end((err ,res)=> { + if(err) return done(err); + expect(res.status).to.equal(200); + expect(res.body.name).to.equal('test movie name'); + done(); + }); + }); + }); + }); + + describe('PUT: /api/movie', function(){ + describe('with a valid body', function(){ + before(done => { + exampleMovie.dateReleased = new Date(); + new Movie(exampleMovie).save() + .then( movie => { + this.tempMovie = movie; + done(); + }) + .catch(done); + }); + + after(done => { + delete exampleMovie.dateReleased; + if(this.tempMovie) { + Movie.remove({}) + .then( () => done()) + .catch(done); + return; + } + done(); + }); + + it('should return a 200 and update the movie', done =>{ + request.put(`${url}/api/movie/${this.tempMovie._id}`) + .send({name:'a new movie'}) + .end((err, res) => { + expect(res.status).to.equal(200); + done(); + }); + }); + }); + }); + + describe('DELETE: /api/movie', function(){ + before(done => { + exampleMovie.dateReleased = new Date(); + new Movie(exampleMovie).save() + .then( movie => { + this.tempMovie = movie; + done(); + }) + .catch(done); + }); + + it('should delete a movie', done => { + request.delete(`${url}/api/movie/${this.tempMovie._id}`, function(error, response) { + expect(response.status).to.equal(204); + done(); + }); + }); + }); +}); diff --git a/test/npm-debug.log b/test/npm-debug.log new file mode 100644 index 0000000..bbaed29 --- /dev/null +++ b/test/npm-debug.log @@ -0,0 +1,48 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/Users/Andymartin/.nvm/versions/node/v6.11.1/bin/node', +1 verbose cli '/Users/Andymartin/.nvm/versions/node/v6.11.1/bin/npm', +1 verbose cli 'run', +1 verbose cli 'test' ] +2 info using npm@3.10.10 +3 info using node@v6.11.1 +4 verbose run-script [ 'pretest', 'test', 'posttest' ] +5 info lifecycle 13-mongodb@1.0.0~pretest: 13-mongodb@1.0.0 +6 silly lifecycle 13-mongodb@1.0.0~pretest: no script for pretest, continuing +7 info lifecycle 13-mongodb@1.0.0~test: 13-mongodb@1.0.0 +8 verbose lifecycle 13-mongodb@1.0.0~test: unsafe-perm in lifecycle true +9 verbose lifecycle 13-mongodb@1.0.0~test: PATH: /Users/Andymartin/.nvm/versions/node/v6.11.1/lib/node_modules/npm/bin/node-gyp-bin:/Users/Andymartin/codefellows/401/labs/week-3/13-mongodb/node_modules/.bin:/Users/Andymartin/.nvm/versions/node/v6.11.1/bin:/Users/Andymartin/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin +10 verbose lifecycle 13-mongodb@1.0.0~test: CWD: /Users/Andymartin/codefellows/401/labs/week-3/13-mongodb +11 silly lifecycle 13-mongodb@1.0.0~test: Args: [ '-c', 'DEBUG=\'actors*\' mocha' ] +12 silly lifecycle 13-mongodb@1.0.0~test: Returned: code: 1 signal: null +13 info lifecycle 13-mongodb@1.0.0~test: Failed to exec test script +14 verbose stack Error: 13-mongodb@1.0.0 test: `DEBUG='actors*' mocha` +14 verbose stack Exit status 1 +14 verbose stack at EventEmitter. (/Users/Andymartin/.nvm/versions/node/v6.11.1/lib/node_modules/npm/lib/utils/lifecycle.js:255:16) +14 verbose stack at emitTwo (events.js:106:13) +14 verbose stack at EventEmitter.emit (events.js:191:7) +14 verbose stack at ChildProcess. (/Users/Andymartin/.nvm/versions/node/v6.11.1/lib/node_modules/npm/lib/utils/spawn.js:40:14) +14 verbose stack at emitTwo (events.js:106:13) +14 verbose stack at ChildProcess.emit (events.js:191:7) +14 verbose stack at maybeClose (internal/child_process.js:891:16) +14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5) +15 verbose pkgid 13-mongodb@1.0.0 +16 verbose cwd /Users/Andymartin/codefellows/401/labs/week-3/13-mongodb/test +17 error Darwin 15.6.0 +18 error argv "/Users/Andymartin/.nvm/versions/node/v6.11.1/bin/node" "/Users/Andymartin/.nvm/versions/node/v6.11.1/bin/npm" "run" "test" +19 error node v6.11.1 +20 error npm v3.10.10 +21 error code ELIFECYCLE +22 error 13-mongodb@1.0.0 test: `DEBUG='actors*' mocha` +22 error Exit status 1 +23 error Failed at the 13-mongodb@1.0.0 test script 'DEBUG='actors*' mocha'. +23 error Make sure you have the latest version of node.js and npm installed. +23 error If you do, this is most likely a problem with the 13-mongodb package, +23 error not with npm itself. +23 error Tell the author that this fails on your system: +23 error DEBUG='actors*' mocha +23 error You can get information on how to open an issue for this project with: +23 error npm bugs 13-mongodb +23 error Or if that isn't available, you can get their info via: +23 error npm owner ls 13-mongodb +23 error There is likely additional logging output above. +24 verbose exit [ 1, true ]