2012-08-28 02:25:52 +00:00
// Generated by CoffeeScript 1.3.3
2012-08-28 03:37:20 +00:00
var scoring ;
scoring = require ( '../app/scoring' ) ;
2012-08-28 02:25:52 +00:00
2012-09-15 19:06:51 +00:00
module . exports = function ( expressApp , root , derby ) {
2012-10-12 15:44:03 +00:00
var deprecatedMessage , staticPages ;
2012-10-12 11:11:06 +00:00
staticPages = derby . createStatic ( root ) ;
2012-10-12 15:44:03 +00:00
expressApp . get ( '/privacy' , function ( req , res ) {
return staticPages . render ( 'privacy' , res ) ;
} ) ;
expressApp . get ( '/terms' , function ( req , res ) {
return staticPages . render ( 'terms' , res ) ;
} ) ;
deprecatedMessage = 'This REST resource is no longer supported, use /users/:uid/tasks/:taskId/:direction instead.' ;
2012-08-28 03:37:20 +00:00
expressApp . get ( '/:uid/up/:score?' , function ( req , res ) {
2012-10-12 15:44:03 +00:00
return res . send ( 200 , deprecatedMessage ) ;
2012-08-28 03:37:20 +00:00
} ) ;
2012-09-07 21:21:09 +00:00
expressApp . get ( '/:uid/down/:score?' , function ( req , res ) {
2012-10-12 15:44:03 +00:00
return res . send ( 200 , deprecatedMessage ) ;
} ) ;
expressApp . post ( '/users/:uid/tasks/:taskId/:direction' , function ( req , res ) {
var direction , icon , model , service , taskId , title , uid , _ref , _ref1 ;
_ref = req . params , uid = _ref . uid , taskId = _ref . taskId , direction = _ref . direction ;
_ref1 = req . body , title = _ref1 . title , service = _ref1 . service , icon = _ref1 . icon ;
console . log ( {
params : req . params ,
body : req . body
} ) ;
if ( direction !== 'up' && direction !== 'down' ) {
return res . send ( 500 , ":direction must be 'up' or 'down'" ) ;
}
2012-08-28 02:25:52 +00:00
model = req . getModel ( ) ;
2012-10-12 15:44:03 +00:00
model . session . userId = uid ;
return model . fetch ( "users." + uid , function ( err , user ) {
var delta , result ;
2012-08-28 02:25:52 +00:00
if ( err || ! user . get ( ) ) {
2012-10-12 15:44:03 +00:00
err || ( err = 'No user with that ID' ) ;
return res . send ( 500 , err ) ;
}
model . ref ( '_user' , user ) ;
if ( ! model . get ( "_user.tasks." + taskId ) ) {
model . refList ( "_habitList" , "_user.tasks" , "_user.habitIds" ) ;
model . at ( '_habitList' ) . push ( {
id : taskId ,
type : 'habit' ,
text : ( title || taskId ) + ' *' ,
value : 0 ,
up : true ,
down : true ,
notes : "This task was created by a third-party service. Feel free to edit, it won't harm the connection to that service. Additionally, multiple services may piggy-back off this task."
} ) ;
2012-08-28 02:25:52 +00:00
}
2012-09-24 12:48:05 +00:00
scoring . setModel ( model ) ;
2012-10-12 15:44:03 +00:00
delta = scoring . score ( taskId , direction ) ;
result = model . get ( '_user.stats' ) ;
result . delta = delta ;
return res . send ( result ) ;
2012-08-28 02:25:52 +00:00
} ) ;
2012-09-07 21:21:09 +00:00
} ) ;
2012-09-15 19:06:51 +00:00
expressApp . post ( '/' , function ( req ) {
2012-09-07 21:21:09 +00:00
return require ( '../app/reroll' ) . stripeResponse ( req ) ;
} ) ;
2012-09-15 19:06:51 +00:00
return expressApp . all ( '*' , function ( req ) {
throw "404: " + req . url ;
} ) ;
2012-08-28 02:25:52 +00:00
} ;