Skip to content

Instantly share code, notes, and snippets.

@seratch
Created September 29, 2012 00:36
Show Gist options
  • Save seratch/3802747 to your computer and use it in GitHub Desktop.
Save seratch/3802747 to your computer and use it in GitHub Desktop.
Play20 Transactional Action sample
// zentasks example
object Projects extends Controller with Secured with Tx {
/**
* Display the dashboard.
*/
def index = localTx { IsAuthenticated { username => _ =>
User.findByEmail(username).map { user =>
Ok(
html.dashboard(
Project.findInvolving(username),
Task.findTodoInvolving(username),
user
)
)
}.getOrElse(Forbidden)
}}
}
trait Tx { self: Controller =>
def localTx[A](action: Action[A]) = LocalTxActionDecorator[A](action)
}
case class LocalTxActionDecorator[A](action: Action[A]) extends Action[A] {
def parser: BodyParser[A] = action.parser
override def apply() = DB localTx { implicit s => action.apply() }
def apply(request: Request[A]): Result = DB localTx { implicit s => action.apply(request) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment