Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ app.use( express.static( path.join( __dirname, '../front_end/public' )))

app.use( express.static( path.join( __dirname, 'public' )))

app.use ( cors )

app.use( '/api/v1/dashboards', api.dashboards )
app.use( '/api/v1/users', api.users )
app.use( '/api/v1/widget_specifications', api.widget_specifications )

app.use ( cors )

// catch 404 and forward to error handler
app.use( function( req, res, next ) {
Expand Down
2 changes: 1 addition & 1 deletion bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ function onListening() {
? 'pipe ' + addr
: 'port ' + addr.port
debug('Listening on ' + bind)
}
}
2 changes: 2 additions & 0 deletions models/dashboards.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const mongoose = require('mongoose')
const { Schema } = mongoose
const User = require('./users')
const { WidgetSchema } = require('./widgets')

const DashboardSchema = new Schema ({
name: { type: String, required: true, default: 'Company Dashboard' },
Expand All @@ -10,6 +11,7 @@ const DashboardSchema = new Schema ({
responsive: { type: Boolean, required: true, default: true },
created_at: { type: Date, required: true, default: Date.now },
users: [{ type: Schema.Types.ObjectId, ref:'User' }],
widgets: [ WidgetSchema ]
})

const Dashboard = mongoose.model('Dashboard', DashboardSchema)
Expand Down
4 changes: 2 additions & 2 deletions models/widgets.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const mongoose = require( 'mongoose' )
const { Schema } = mongoose

const WidgetSchema = new Schema({})
const WidgetSchema = new Schema({}, {strict: false})

const Widget = mongoose.model( 'Widget', WidgetSchema )

module.exports = Widget
module.exports = { Widget, WidgetSchema }
2 changes: 1 addition & 1 deletion routes/api/v1/dashboard/addWidget.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Widget } = require( '../../../../models/widgets' )

const addWidget = widgetData => dashboard => {
const addWidget = dashboard => {
dashboard.widgets.push( new Widget( widgetData ))
return dashboard.save()
}
Expand Down
5 changes: 3 additions & 2 deletions routes/api/v1/dashboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ router.delete( '/:id',( request, response ) => {
})

router.post( '/:id/widgets', ( request, response ) => {
Dashboard.findById( request.params.id ).exec()
.then( addWidget( request.body ))
Dashboard.findByIdAndUpdate( request.params.id,
{ $push: { widgets:request.body } } ).exec()
.then( result => result )
.then( dashboard => response.status( 201 ).json( dashboard ))
.catch( errorResponse( response ))
})
Expand Down