Skip to content

Commit

Permalink
bugfixes(tests): Fixing Controller after tests revealed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
pilsy committed Jun 8, 2014
1 parent 75c7b09 commit 43b0981
Showing 1 changed file with 71 additions and 67 deletions.
138 changes: 71 additions & 67 deletions lib/classes/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,81 @@ module.exports = Controller.extend(
app: require( 'injector' ).getInstance( 'app' ),
service: null
},
// Inherit from EventEmitter
uberUtil.inherits(
/* @Prototype */
{
listAction: function() {
if ( this.Class.service !== null ) {
this.Class
.service
.findAll( this.req.query )
.then( this.proxy( 'handleServiceMessage' ) )
.catch( this.proxy( 'handleException' ) );
} else {
this.next();
}
},

getAction: function() {
if ( this.Class.service !== null ) {
this.Class
.service
.findById( this.req.params.id )
.then( this.proxy( 'handleServiceMessage' ) )
.catch( this.proxy( 'handleException' ) );
} else {
this.next();
}
},
/* @Prototype */
{
listAction: function() {
if ( this.Class.service !== null ) {
this.Class
.service
.findAll( this.req.query )
.then( this.proxy( 'handleServiceMessage' ) )
.catch( this.proxy( 'handleException' ) );
} else {
this.next();
}
},

postAction: function() {
if ( this.Class.service !== null ) {
this.Class
.service
.create( this.req.body )
.then( this.proxy( 'handleServiceMessage' ) )
.catch( this.proxy( 'handleException' ) );
} else {
this.next();
}
},
getAction: function() {
if ( this.Class.service !== null ) {
this.Class
.service
.findById( this.req.params.id )
.then( this.proxy( 'handleServiceMessage' ) )
.catch( this.proxy( 'handleException' ) );
} else {
this.next();
}
},

putAction: function() {
if ( this.Class.service !== null ) {
this.Class
.service
.update( this.req.params.id, this.req.body )
.then( this.proxy( 'handleServiceMessage' ) )
.catch( this.proxy( 'handleException' ) );
} else {
this.next();
postAction: function() {
if ( !!this.req.body.id || !!this.req.params.id ) {
this.action = 'putAction';
if ( !this.req.params.id ) {
this.req.params.id = this.req.body.id;
delete this.req.body.id;
}
},
return this.putAction();
} else if ( this.Class.service !== null ) {
this.Class
.service
.create( this.req.body )
.then( this.proxy( 'handleServiceMessage' ) )
.catch( this.proxy( 'handleException' ) );
} else {
this.next();
}
},

deleteAction: function() {
if ( this.Class.service !== null ) {
this.Class
.service
.destroy( this.req.params.id )
.then( this.proxy( 'handleServiceMessage' ) )
.catch( this.proxy( 'handleException' ) );
} else {
this.next();
}
},
putAction: function() {
if ( this.Class.service !== null ) {
this.Class
.service
.update( this.req.params.id, this.req.body )
.then( this.proxy( 'handleServiceMessage' ) )
.catch( this.proxy( 'handleException' ) );
} else {
this.next();
}
},

handleServiceMessage: function( obj ) {
if ( obj.statuscode ) {
this.send( obj.message, obj.statuscode );
return;
}
deleteAction: function() {
if ( this.Class.service !== null ) {
this.Class
.service
.destroy( this.req.params.id )
.then( this.proxy( 'handleServiceMessage' ) )
.catch( this.proxy( 'handleException' ) );
} else {
this.next();
}
},

this.send( obj, 200 );
handleServiceMessage: function( obj ) {
if ( obj.statuscode ) {
this.send( obj.message, obj.statuscode );
return;
}
}, EventEmitter )
);

this.send( obj, 200 );
}
});

0 comments on commit 43b0981

Please sign in to comment.