简体   繁体   中英

Stacking actions in Play Framework 2.0

I'd like to do something along the lines of:

def findUser[ A ]( bp: BodyParser[ A ] )( id: Int )( f: User => Action[ A ] => Result )
def findProfile[ A ]( bp: BodyParser[ A ] )( id: Int )( f: Profile => Action[ A ] => Result )

Where the two (and more) can be used interchangeably, or as follows:

def create = findUser( parse.json ) { user => findProfile( parse.json ) { profile => implicit request => ...

The two issues I'm having have to do with parse.json (which I think I can fix by passing JsValue as the body parser in the action and remove it from the signature.. And being able to use one or more of those actions at will... Any ideas?

Thank you!

Update

Moved answer to answer comment...

I was able to get it work with the following signature. Here it is with the body parser refactored... You can put these helper functions (bodyParser, findUser, findProfile) in a trait and extend controllers with them... You can also remove the body parser entirely from the signature and pass parse.json in the action and yield findUser( id ) instead of findUser( )( id ).

val bodyParser = parse.json //(I had to include the entire path to it...)
def findUser[ A ]( bp: BodyParser[ A ] = bodyParser )( id: Int )( f: User => Request[ A ] => Result )

def find( id: Int ) =
    IsLoggedIn( ) {
        findUser( )( id ) {
        user =>
        findProfile( )( user.id ){
            profile =>
            request =>
                Ok( toJson( profile ) )
} } }

If you know a cleaner way, please tell me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM