6 widget routes (fixes issue #6) (Replaces Widget Routes #33)#34
6 widget routes (fixes issue #6) (Replaces Widget Routes #33)#34pllearns wants to merge 2 commits intoGuildCrafts:masterfrom pllearns:6-Widget-Routes
Conversation
database/DashboardSchema.js
Outdated
| const WidgetSchema = require('./WidgetSchema') | ||
|
|
||
| const DashboardSchema = mongoose.Schema ({ | ||
| _user: {type: Number, ref: 'User'}, |
There was a problem hiding this comment.
Shouldn't this be an ObjectID? Or is this some functionality provided by mongoose? Why is the name _user instead of user?
database/DashboardSchema.js
Outdated
| @@ -0,0 +1,12 @@ | |||
| const mongoose = require('mongoose') | |||
| const Schema = mongoose.Schema | |||
There was a problem hiding this comment.
const { Schema } = mongoose
database/User.js
Outdated
| @@ -0,0 +1,16 @@ | |||
| const mongoose = require('mongoose') | |||
| mongoose.connect('mongodb://localhost/lizardboard') | |||
There was a problem hiding this comment.
Why do we connect here, but not in the other model file?
database/User.js
Outdated
| const db= mongoose.connection | ||
|
|
||
| const UserSchema = require('./UserSchema') | ||
| db.on('error', console.error.bind(console,'connection error:')) |
There was a problem hiding this comment.
We need to separate the setup of the database connection from the definition of the models...
database/UserSchema.js
Outdated
|
|
||
| const UserSchema = mongoose.Schema ({ | ||
| username: String, | ||
| dashboards: [{type: Schema.Types.ObjectId, ref:'Dashboard'}] |
There was a problem hiding this comment.
dashboards are not a property of user - the relationship should be stored in the Dashboard object, unless this provides us with some needed functionality?
routes/users.js
Outdated
| }) | ||
| }) | ||
|
|
||
| router.get('/widgets/:widgetId', ( request, response, next ) => { |
There was a problem hiding this comment.
Please name the variable :id instead of :widgetId - given the name of the route, it is understood that this is a widget id. Also, why is this route declared in the user routes file? It should be in its own module for widgets route.
routes/users.js
Outdated
| .then( data => console.log('finished') ) | ||
| }) | ||
|
|
||
| router.post('/:userId/widgets', (request, response, next ) => { |
There was a problem hiding this comment.
Widgets do not belong to a user, so it does not seem to make sense to include them here. I could see an argument being made for /dashboards/:id/widgets, but we would probably just return the widgets along with the dashboard - it wouldn't make sense to have a set of widgets outside of a dashboard. I think this route will also be removed when we clean up the User model, as requested above (widgets do not belong to users).
routes/users.js
Outdated
| router.post('/:userId/widgets', (request, response, next ) => { | ||
| const { userId } = request.params | ||
| const widget = { type, title, size, contents } | ||
| widget = request.body |
There was a problem hiding this comment.
This is not a legal statement - you can not reassign a constant.
routes/users.js
Outdated
| ) | ||
| }) | ||
|
|
||
| router.put('/:userId/widgets/:widgetId', ( request, response, next ) => { |
There was a problem hiding this comment.
Omit route; it does not make sense in the context of our domain (users do not contain widgets).
routes/users.js
Outdated
| ) | ||
| }) | ||
|
|
||
| router.delete( '/:userId/widgets/:widgetId', ( request, response, next ) => { |
jrobcodes
left a comment
There was a problem hiding this comment.
@pllearns @jessehall3 @harmanlearns Comments inline
.gitignore
Outdated
| *.log | ||
| .env | ||
| /build | ||
| ./userRouteNotes.js |
There was a problem hiding this comment.
How will the other team get this if it's in gitignore? May as well remove, and remove the file, and send the file to them using conventional means, instead of polluting the gitignore file.
bin/www
Outdated
|
|
||
| mongoose.connect(connection) | ||
| .then(() => console.log('connection successful')) | ||
| .catch((error) => console.error(error)) |
There was a problem hiding this comment.
If we fail to connect, we should probably not start the server...
database/DashboardSchema.js
Outdated
| @@ -0,0 +1,12 @@ | |||
| const mongoose = require('mongoose') | |||
| const { Schema } = mongoose | |||
| const WidgetSchema = require('./WidgetSchema') | |||
There was a problem hiding this comment.
WidgetSchema is not used in this file?
database/UserSchema.js
Outdated
| @@ -0,0 +1,10 @@ | |||
| const mongoose = require('mongoose') | |||
| const Schema = mongoose.Schema | |||
There was a problem hiding this comment.
Update as in other files.
package.json
Outdated
| "ejs": "^2.5.2", | ||
| "express": "~4.13.4", | ||
| "mongoose": "^4.6.5", | ||
| "mongodb": "^2.2.11", |
There was a problem hiding this comment.
Are we using this import, and if so, for what? (since we have mongoose...)
routes/userRouteNotes.js
Outdated
| }) | ||
|
|
||
| router.post('/dashboards/:id/widgets', (request, response, next ) => { | ||
| Widget.create(request.body, (error, post) => { |
There was a problem hiding this comment.
Please consider a different name for the post parameter - this is not an intention revealing name
routes/userRouteNotes.js
Outdated
| }) | ||
| }) | ||
|
|
||
| router.put('/:userId/widgets/:widgetId', ( request, response, next ) => { |
There was a problem hiding this comment.
Remove this route, widgets do not belong to users.
routes/userRouteNotes.js
Outdated
| ) | ||
| }) | ||
|
|
||
| router.delete( '/:userId/widgets/:widgetId', ( request, response, next ) => { |
There was a problem hiding this comment.
Remove this route, widgets do not belong to users.
routes/users.js
Outdated
| const User = require('../database/UserSchema.js') | ||
| const router = express.Router() | ||
| const mongoose = require('mongoose') | ||
| const Widget = require('../database/WidgetSchema.js') |
There was a problem hiding this comment.
This import is not used. Also, the filename WidgetSchema is very misleading if this module exports a model - I would move model definition modules into a models folder, instead of database, and remove the Schema part of the file name. As well, files should be named with lower snake case.
routes/users.js
Outdated
| }); | ||
| router.post('/', ( request, response, next ) => { | ||
| User.create(request.body, (error, post) => { | ||
| if ( error ) return next ( error ) |
There was a problem hiding this comment.
See above - multi line if, change variable name, formatting issues.
bin/www
Outdated
| mongoose.Promise = global.Promise | ||
| const connection = 'mongodb://localhost/lizardboard' | ||
|
|
||
| mongoose.connect(connection) |
There was a problem hiding this comment.
This needs to be moved such that it happens prior to server startup. Please move imports to the top of the module, with other imports. We will also need the connection string to be added in a .env file, with instructions added to README on how to set this up.
server.on('error', onError);
server.on('listening', onListening);
mongoose.connect( process.env. MONGODB_URI )
.then( () => server.listen( port ) )
server.listen(port);
routes/users.js
Outdated
| const express = require('express'); | ||
| const router = express.Router(); | ||
| const express = require('express') | ||
| const User = require('../models/user.js') |
There was a problem hiding this comment.
Let's not change the order of existing imports - it introduces changes in the diff that aren't really changes. :) Just shift the User import down below the instantiation of router and it should be good
routes/users.js
Outdated
| router.get('/', (request, response, next) => { | ||
| response.send('respond with a resource'); | ||
| }); | ||
| router.post('/', ( request, response, next ) => { |
There was a problem hiding this comment.
Add space between ( and '/'
routes/users.js
Outdated
| response.send('respond with a resource'); | ||
| }); | ||
| router.post('/', ( request, response, next ) => { | ||
| User.create(request.body, (error, post) => { |
There was a problem hiding this comment.
Add space between ( and request.body, as well as between (, ) and parameters in callback function. Please consider a different name for the parameter post - this does not reveal the intent of this parameter (I'm not sure what it does, is intended to represent, or what data it should hold)
routes/users.js
Outdated
| router.post('/', ( request, response, next ) => { | ||
| User.create(request.body, (error, post) => { | ||
| if ( error ) { | ||
| return next ( error ) |
routes/users.js
Outdated
| if ( error ) { | ||
| return next ( error ) | ||
| } | ||
| response.json( post ) |
There was a problem hiding this comment.
Add whitespace above this line
| div.footer-nav | ||
|
|
||
| include footer | ||
| script(src="/javascripts/index.js") |
There was a problem hiding this comment.
Was this change intentional? Wanted to verify since I'm not sure why we're removing sripts from the layout when working on the API.
There was a problem hiding this comment.
This was intentional since it isn't needed. Harman pointed this out to me as we were refactoring yesterday.
There was a problem hiding this comment.
It causes 304 errors because these files do not exist.
| @@ -12,7 +12,10 @@ A mongodb database named lizardboard must be created prior to starting the appli | |||
| - brew install yarn | |||
There was a problem hiding this comment.
Please revert changes in this file - these are recommendations, not instructions for setting up the application. See the floworky README for guidance adding instructions for setting up .env
bin/www
Outdated
|
|
||
| mongoose.connect( process.env. MONGODB_URI ) | ||
| .then( () => server.listen( port ) ) | ||
| server.listen( port ); |
There was a problem hiding this comment.
Remove this line - no need to start the server twice. I think the server.on lines also need to be above this to make sure set up occurs properly
routes/users.js
Outdated
| User.create( request.body, ( error, user ) => { | ||
|
|
||
| module.exports = router; | ||
| if ( error ) { |
There was a problem hiding this comment.
Indentation is still messed up in this router. Please correct.
README.md
Outdated
| ``` echo MONGODB_URI=mongodb://`whoami`@localhost:3000/lizardboard >> .env | ||
| ``` | ||
|
|
||
| ## (Optional) Configuration for test RESTful APIs |
There was a problem hiding this comment.
Configuration for test_ing_
README.md
Outdated
| - yarn start (start the server) | ||
|
|
||
| Lizardboard needs an .env file to specify environment variables that are required to run the application. | ||
| ``` echo MONGODB_URI=mongodb://`whoami`@localhost:3000/lizardboard >> .env |
There was a problem hiding this comment.
Shouldn't this just be mongodb://localhost/lizardboard?
bin/www
Outdated
| app.set('port', port); | ||
|
|
||
| mongoose.connect( connection ) | ||
| .then( () => server.listen( port ) ) |
routes/users.js
Outdated
| response.send('respond with a resource'); | ||
| }); | ||
| router.post( '/', ( request, response, next ) => { | ||
| User.create( request.body, ( error, user ) => { |
There was a problem hiding this comment.
Missed this in previous commits; sorry `bout that - these should be promisified. See harman and jesse, they've got an example in their PR
-Updated RESTful APIs
-Updated naming conventions for schemas
-Refactored code and cleaned out comments