From f22aae1b27525bbf1969c8c5ef9c6a4842f92533 Mon Sep 17 00:00:00 2001 From: ohjonah Date: Wed, 2 Aug 2017 14:43:41 -0700 Subject: [PATCH 1/7] Scaffold --- model/bakery.js | 0 route/bakery-route.js | 0 server.js | 0 test/bakery-route.js | 0 4 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 model/bakery.js create mode 100644 route/bakery-route.js create mode 100644 server.js create mode 100644 test/bakery-route.js diff --git a/model/bakery.js b/model/bakery.js new file mode 100644 index 0000000..e69de29 diff --git a/route/bakery-route.js b/route/bakery-route.js new file mode 100644 index 0000000..e69de29 diff --git a/server.js b/server.js new file mode 100644 index 0000000..e69de29 diff --git a/test/bakery-route.js b/test/bakery-route.js new file mode 100644 index 0000000..e69de29 From b263e8e4b6bba2cdfc199093112c07d0e50bef0d Mon Sep 17 00:00:00 2001 From: ohjonah Date: Wed, 2 Aug 2017 14:47:50 -0700 Subject: [PATCH 2/7] Scaffold and Dependencies --- .eslintignore | 5 ++ .eslintrc | 21 ++++++++ .gitignore | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 38 ++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 package.json diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..82ff623 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* \ No newline at end of file diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..b663d77 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,21 @@ +{ + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": [ "error", "single" ], + "semi": ["error", "always"], + "linebreak-style": [ "error", "unix" ] + }, + "env": { + "es6": true, + "node": true, + "mocha": true, + "jasmine": true + }, + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true, + "impliedStrict": true + }, + "extends": "eslint:recommended" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..393ef53 --- /dev/null +++ b/.gitignore @@ -0,0 +1,136 @@ +# Created by https://www.gitignore.io/api/osx,vim,node,macos,windows + +### macOS ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + + +### OSX ### + +# Icon must end with two \r + +# Thumbnails + +# Files that might appear in the root of a volume + +# Directories potentially created on remote AFP share + +### Vim ### +# swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-v][a-z] +[._]sw[a-p] +# session +Session.vim +# temporary +.netrwhist +*~ +# auto-generated tag files +tags + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.gitignore.io/api/osx,vim,node,macos,windows \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..09a3245 --- /dev/null +++ b/package.json @@ -0,0 +1,38 @@ +{ + "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", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ohjonah/13-mongodb.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/ohjonah/13-mongodb/issues" + }, + "homepage": "https://github.com/ohjonah/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" + } +} From c9a9b1da0907705866e34d9770b68d7e7d62f817 Mon Sep 17 00:00:00 2001 From: ohjonah Date: Wed, 2 Aug 2017 18:10:52 -0700 Subject: [PATCH 3/7] Adds Bakery Modules --- model/bakery.js | 11 +++++++++++ package.json | 4 ++-- route/bakery-route.js | 24 ++++++++++++++++++++++++ server.js | 23 +++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/model/bakery.js b/model/bakery.js index e69de29..59fa222 100644 --- a/model/bakery.js +++ b/model/bakery.js @@ -0,0 +1,11 @@ +'use strict'; + +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; + +const bakerySchema = Schema({ + name: { type: String, required: true }, + timestamp: { type: Date, required: true } +}); + +module.exports = mongoose.model('bakery', bakerySchema); \ No newline at end of file diff --git a/package.json b/package.json index 09a3245..0a346be 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "test": "test" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "node server.js" + "test": "DEBUG='bake*' mocha", + "start": "DEBUG='bake*' node server.js" }, "repository": { "type": "git", diff --git a/route/bakery-route.js b/route/bakery-route.js index e69de29..0e392f1 100644 --- a/route/bakery-route.js +++ b/route/bakery-route.js @@ -0,0 +1,24 @@ +'use strict'; + +const Router = require('express').Router; +const jsonParser = require('body-parser').json(); +const debug = require('debug')('note:bakery-router'); +const Bakery = require('../model/bakery.js'); +const bakeryRouter = module.exports = new Router(); + +bakeryRouter.post('/api/bakery', jsonParser, function(req, res, next) { + debug('POST: /api/bakery'); + + req.body.timestamp = new Date(); + new Bakery(req.body).save() + .then( bake => res.json(bake)) + .catch(next); +}); + +bakeryRouter.get('/api/list/:id', function(req, res, next) { + debug('GET: /api/bakery'); + + Bakery.findById(req.params.id) + .then( bake => res.json(bake)) + .catch(next); +}); \ No newline at end of file diff --git a/server.js b/server.js index e69de29..0444e26 100644 --- a/server.js +++ b/server.js @@ -0,0 +1,23 @@ +'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')('bake:server'); + +const app = express(); +const PORT = process.env.PORT || 3000; +const MONGODB_URI = 'mongodb://localhost/bakery'; + +mongoose.connect(MONGODB_URI, { + useMongoClient: true +}); + +app.listen(PORT, () => { + debug(`Server on PORT: ${PORT}`); +}); + + + From 80e1cceebe474d85cefc20566bec3705cc8d4554 Mon Sep 17 00:00:00 2001 From: ohjonah Date: Wed, 2 Aug 2017 19:48:46 -0700 Subject: [PATCH 4/7] Adds and Refactors Tests from Lecture --- test/bakery-route.js | 0 test/bakery-test.js | 81 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) delete mode 100644 test/bakery-route.js create mode 100644 test/bakery-test.js diff --git a/test/bakery-route.js b/test/bakery-route.js deleted file mode 100644 index e69de29..0000000 diff --git a/test/bakery-test.js b/test/bakery-test.js new file mode 100644 index 0000000..8e3af03 --- /dev/null +++ b/test/bakery-test.js @@ -0,0 +1,81 @@ +'use strict'; + +const expect = require('chai').expect; +const request = require('superagent'); +const Bakery = require('../model/bakery.js'); +const PORT = process.env.PORT || 3000; + +require('../server.js'); + +const url = `http://localhost:${PORT}`; + +const exampleBakery = { + name: 'test bakery name' +}; + +describe('Bakery Routes', function() { + describe('POST: /api/bakery', function() { + describe('with a valid req body', function() { + + after( done => { + if (this.tempBakery) { + Bakery.remove({}) + .then( () => done()) + .catch(done); + return; + } + done(); + }); + + it('should return a list', done => { + request.post(`${url}/api/bakery`) + .send(exampleBakery) + .end((err, res) => { + if (err) return done(err); + expect(res.status).to.equal(200); + expect(res.body.name).to.equal('test bakery name'); + this.tempBakery = res.body; + done(); + }); + }); + }); + }); + + describe('GET: /api/bakery/:id', function() { + describe('with a valid body', function() { + + before( done => { + exampleBakery.timestamp = new Date(); + new Bakery(exampleBakery).save() + .then( bakery => { + this.tempBakery = bakery; + done(); + }) + .catch(done); + }); + + after( done => { + delete exampleBakery.timestamp; + if (this.tempBakery) { + Bakery.remove({}) + .then( () => done()) + .catch(done); + return; + } + done(); + }); + + it('should return a bakery', done => { + request.get(`${url}/api/bakery/${this.tempBakery._id}`) + .end((err, res) => { + + console.log('this:', this.tempBakery); + if (err) return done(err); + expect(res.status).to.equal(200); + expect(res.body.name).to.equal('test bakery name'); + done(); + }); + }); + }); + }); +}); \ No newline at end of file From 6448760b7b710dd842a27a75d28f040a3bdf1a9f Mon Sep 17 00:00:00 2001 From: ohjonah Date: Wed, 2 Aug 2017 22:42:39 -0700 Subject: [PATCH 5/7] Adds Put and Delete Method and Tests --- test/bakery-test.js | 61 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/test/bakery-test.js b/test/bakery-test.js index 8e3af03..4cfb5ff 100644 --- a/test/bakery-test.js +++ b/test/bakery-test.js @@ -68,8 +68,6 @@ describe('Bakery Routes', function() { it('should return a bakery', done => { request.get(`${url}/api/bakery/${this.tempBakery._id}`) .end((err, res) => { - - console.log('this:', this.tempBakery); if (err) return done(err); expect(res.status).to.equal(200); expect(res.body.name).to.equal('test bakery name'); @@ -78,4 +76,63 @@ describe('Bakery Routes', function() { }); }); }); + + describe('PUT: /api/bakery/:id', function() { + describe('with a valid id', function() { + + before( done => { + exampleBakery.timestamp = new Date(); + new Bakery(exampleBakery).save() + .then( bakery => { + this.tempBakery = bakery; + done(); + }) + .catch(done); + }); + + after( done => { + delete exampleBakery.timestamp; + if (this.tempBakery) { + Bakery.remove({}) + .then( () => done()) + .catch(done); + return; + } + done(); + }); + + it('should update a bakery', done => { + request.put(`${url}/api/bakery/${this.tempBakery._id}`) + .end((err, res) => { + if (err) return done(err); + expect(res.status).to.equal(200); + done(); + }); + }); + }); + }); + + describe('DELETE: /api/bakery/:id', function() { + describe('with a valid id', function() { + + before( done => { + exampleBakery.timestamp = new Date(); + new Bakery(exampleBakery).save() + .then( bakery => { + this.tempBakery = bakery; + done(); + }) + .catch(done); + }); + + it('should delete a bakery', done => { + request.delete(`${url}/api/bakery/${this.tempBakery._id}`) + .end((err, res) => { + if (err) return done(err); + expect(res.status).to.equal(204); + done(); + }); + }); + }); + }); }); \ No newline at end of file From 1d3b74ecdb511bcfa77eea3c2c6b697d4315e331 Mon Sep 17 00:00:00 2001 From: ohjonah Date: Thu, 3 Aug 2017 08:49:20 -0700 Subject: [PATCH 6/7] Adds some more tests --- test/bakery-test.js | 63 ++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/test/bakery-test.js b/test/bakery-test.js index 4cfb5ff..bb99080 100644 --- a/test/bakery-test.js +++ b/test/bakery-test.js @@ -75,39 +75,54 @@ describe('Bakery Routes', function() { }); }); }); + + describe('with an invalid id', function() { + it('should return a 404', function() { + request.get(`${url}/api/bakery`) + .end((err, res) => { + expect(res.status).to.equal(404); + }); + }); + }); }); describe('PUT: /api/bakery/:id', function() { - describe('with a valid id', function() { + before( done => { + exampleBakery.timestamp = new Date(); + new Bakery(exampleBakery).save() + .then( bakery => { + this.tempBakery = bakery; + done(); + }) + .catch(done); + }); - before( done => { - exampleBakery.timestamp = new Date(); - new Bakery(exampleBakery).save() - .then( bakery => { - this.tempBakery = bakery; - done(); - }) + after( done => { + delete exampleBakery.timestamp; + if (this.tempBakery) { + Bakery.remove({}) + .then( () => done()) .catch(done); - }); + return; + } + done(); + }); - after( done => { - delete exampleBakery.timestamp; - if (this.tempBakery) { - Bakery.remove({}) - .then( () => done()) - .catch(done); - return; - } + it('should update a bakery', done => { + request.put(`${url}/api/bakery/${this.tempBakery._id}`) + .end((err, res) => { + if (err) return done(err); + expect(res.status).to.equal(200); done(); }); + }); - it('should update a bakery', done => { - request.put(`${url}/api/bakery/${this.tempBakery._id}`) - .end((err, res) => { - if (err) return done(err); - expect(res.status).to.equal(200); - done(); - }); + it('should return a 404', done => { + request.put(`${url}/api/bakery`) + .end((err, res) => { + console.log('res:', res); + expect(res.status).to.equal(404); + done(); }); }); }); From 923a001bc7454ab7f12d9f6e10406d304e968dd0 Mon Sep 17 00:00:00 2001 From: ohjonah Date: Thu, 3 Aug 2017 09:08:12 -0700 Subject: [PATCH 7/7] Adds all files --- README.md | 24 ------------------------ package.json | 4 ++-- route/bakery-route.js | 24 ++++++++++++++++++++---- server.js | 9 ++++++++- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 37f3822..ec33b97 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,3 @@ -![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) 13: Single Resource Mongo and Express API -=== - -## Submission Instructions - * fork this repository & create a new branch for your work - * write all of your code in a directory named `lab-` + `` **e.g.** `lab-susan` - * push to your repository - * submit a pull request to this repository - * submit a link to your PR in canvas - * write a question and observation on canvas - -## Learning Objectives -* students will be able to work with the MongoDB database management system -* students will understand the primary concepts of working with a NoSQL database management system -* students will be able to create custom data models *(schemas)* through the use of mongoose.js -* students will be able to use mongoose.js helper methods for interacting with their database persistence layer - -## Requirements -#### Configuration -* `package.json` -* `.eslintrc` -* `.gitignore` -* `README.md` - * your `README.md` should include detailed instructions on how to use your API #### Feature Tasks * create an HTTP Server using `express` diff --git a/package.json b/package.json index 0a346be..5bbffa1 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "test": "test" }, "scripts": { - "test": "DEBUG='bake*' mocha", - "start": "DEBUG='bake*' node server.js" + "test": "DEBUG='bakery*' mocha", + "start": "DEBUG='bakery*' node server.js" }, "repository": { "type": "git", diff --git a/route/bakery-route.js b/route/bakery-route.js index 0e392f1..336feb1 100644 --- a/route/bakery-route.js +++ b/route/bakery-route.js @@ -2,7 +2,7 @@ const Router = require('express').Router; const jsonParser = require('body-parser').json(); -const debug = require('debug')('note:bakery-router'); +const debug = require('debug')('bakery:bakery-router'); const Bakery = require('../model/bakery.js'); const bakeryRouter = module.exports = new Router(); @@ -11,14 +11,30 @@ bakeryRouter.post('/api/bakery', jsonParser, function(req, res, next) { req.body.timestamp = new Date(); new Bakery(req.body).save() - .then( bake => res.json(bake)) + .then( bakery => res.json(bakery)) .catch(next); }); -bakeryRouter.get('/api/list/:id', function(req, res, next) { +bakeryRouter.get('/api/bakery/:id', function(req, res, next) { debug('GET: /api/bakery'); Bakery.findById(req.params.id) - .then( bake => res.json(bake)) + .then( bakery => res.json(bakery)) + .catch(next); +}); + +bakeryRouter.put('/api/bakery/:id', function(req, res, next) { + debug('PUT: /api/bakery'); + + Bakery.findByIdAndUpdate(req.params.id) + .then( bakery => res.json(bakery)) + .catch(next); +}); + +bakeryRouter.delete('/api/bakery/:id', function(req, res, next) { + debug('DELETE: /api/bakery'); + + Bakery.findByIdAndRemove(req.params.id) + .then( bakery => res.status(204).send(bakery)) .catch(next); }); \ No newline at end of file diff --git a/server.js b/server.js index 0444e26..a3afec4 100644 --- a/server.js +++ b/server.js @@ -5,16 +5,23 @@ const morgan = require('morgan'); const cors = require('cors'); const Promise = require('bluebird'); const mongoose = require('mongoose'); -const debug = require('debug')('bake:server'); +const debug = require('debug')('bakery:server'); + +const bakeryRouter = require('./route/bakery-route.js'); const app = express(); const PORT = process.env.PORT || 3000; const MONGODB_URI = 'mongodb://localhost/bakery'; +mongoose.Promise = global.Promise; mongoose.connect(MONGODB_URI, { useMongoClient: true }); +app.use(cors()); +app.use(morgan('dev')); +app.use(bakeryRouter); + app.listen(PORT, () => { debug(`Server on PORT: ${PORT}`); });