-
Notifications
You must be signed in to change notification settings - Fork 8
Widget routes #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Widget routes #33
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| const schemas = require('mongoose') | ||
| const WidgetSchema = require('./WidgetSchema') | ||
|
|
||
| const DashboardSchema = schemas.Schema ({ | ||
| _user: {type: Number, ref: 'User'}, | ||
| widgets: [{type:schemas.Schema.Types.ObjectId, ref:'Widget'}] | ||
| }) | ||
|
|
||
| const Dashboard = schemas.model('Dashboard', DashboardSchema) | ||
|
|
||
| module.exports = { DashboardSchema } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const schemas = require('mongoose') | ||
| schemas.connect('mongodb://localhost/lizardboard') | ||
| const db= schemas.connection | ||
| const UserSchema = require('./UserSchema') | ||
| db.on('error', console.error.bind(console,'connection error:')) | ||
| db.once('open', function() { | ||
| console.log('connected') | ||
| }) | ||
|
|
||
| const User = schemas.model('User', UserSchema) | ||
|
|
||
| module.exports = { | ||
| db, | ||
| User | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| const schemas = require('mongoose') | ||
| const DashboardSchema = require('./DashboardSchema') | ||
|
|
||
| const UserSchema = schemas.Schema ({ | ||
| username: String, | ||
| dashboards: [{type: schemas.Schema.Types.ObjectId, ref:'Dashboard'}] | ||
| }) | ||
|
|
||
| module.exports = { UserSchema } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| const schemas = require('mongoose') | ||
|
|
||
| const WidgetSchema = schemas.Schema ({ | ||
| _dashboard:{ type:Number, ref:'Dashboard' }, | ||
| type: String, | ||
| title: String, | ||
| size: String, | ||
| contents: String | ||
| }) | ||
|
|
||
| const Widget = schemas.model( 'Widget', WidgetSchema ) | ||
|
|
||
| module.exports = { WidgetSchema } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,109 @@ | ||
| const express = require('express'); | ||
| const { User, db } = require('../database/User.js') | ||
| const router = express.Router(); | ||
| const mongoose = require('mongoose') | ||
|
|
||
|
|
||
| /* GET users listing. */ | ||
| router.get('/', (request, response, next) => { | ||
| response.send('respond with a resource'); | ||
| router.get('/', ( request, response, next ) => { | ||
| response.send( 'respond with a resource' ); | ||
| }); | ||
|
|
||
| router.post('/', ( request, response, next ) => { | ||
| const { username } = request.body | ||
| const newUser = new User({ | ||
| username: username, | ||
| dashboards: { | ||
| widgets: {} | ||
| } | ||
| }) | ||
|
|
||
| newUser.save( (error) => { | ||
| if(error) return handleError(err) | ||
| }) | ||
|
|
||
| }) | ||
|
|
||
| /* GET widget */ | ||
| router.get('/widgets/:widgetId', ( request, response, next ) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would name this parameter |
||
| const { widgetId } = request.params | ||
|
|
||
| User.find( {'widgets._id': widgetId},function(error, widgets){ | ||
| return widgets | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use two spaces for indentation. |
||
| }) | ||
| .then( data => console.log('finished') ) | ||
| }); | ||
|
|
||
| /* CREATE widgets */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comment |
||
| router.post('/:userId/widgets/create', (request, response, next ) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will follow a RESTful approach to API endpoints: Please update endpoints to reflect this approach |
||
| const { userId } = request.params | ||
| const { type, title, size, contents } = request.body | ||
|
|
||
| const newWidget = { | ||
| type: type, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use implicit object creation: const widget = { type, title, size, contents }I also prefer a name like |
||
| title: title, | ||
| size: size, | ||
| contents: contents | ||
| } | ||
|
|
||
| User.findOneAndUpdate( | ||
| { _id: userId }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the |
||
| { | ||
| $push: { | ||
| widgets: newWidget | ||
| } | ||
| }, | ||
| { upsert: true }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not use upsert - it results in the possibility of confusing bugs, and mixes behaviors - we should either knowingly create something, or knowingly update something, never update or create if it didn't exists. |
||
| function( error ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ES6 functions, please. |
||
| if( error ) console.log( error ) | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
| /* UPDATE widgets */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comment |
||
| router.post('/:userId/widgets/:widgetId/update', ( request, response, next ) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See note above about RESTful endpoints, removing upsert, etc. |
||
| const { userId, widgetId } = request.params | ||
| const { type, title, size, contents } = request.body | ||
|
|
||
| const updatedWidget = { | ||
| _id: widgetId, | ||
| type: type, | ||
| title: title, | ||
| size: size, | ||
| contents: contents | ||
| } | ||
|
|
||
| User.findOneAndUpdate( | ||
| { _id: userId, widgets: { $elemMatch:{ _id: widgetId }}}, | ||
| { | ||
| $set: { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't mongoose provide a much better API for updating than |
||
| 'widgets.$': updatedWidget | ||
| } | ||
| }, | ||
| { upsert: true }, | ||
| function( error ) { | ||
| if( error ) console.log( error ) | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
|
|
||
| /* DELETE widgets */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comment and extraneous whitespace. |
||
| router.post( '/:userId/widgets/:widgetId/delete', ( request, response, next ) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See notes above re: REST, mongoose API, ES6. |
||
| const { userId, widgetId } = request.params | ||
|
|
||
| User.findOneAndUpdate( | ||
| { _id: userId }, | ||
| { | ||
| $pull: { | ||
| widgets: { _id: widgetId } | ||
| } | ||
| }, | ||
| {'new': true}, | ||
| function( error, data ){ | ||
| if( error ) console.log( error ) | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
| module.exports = router; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of this route?