Last active
September 2, 2015 08:42
-
-
Save julienlafont-tabmo/154700fcab410ee06c20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Shortcut { self: Controller => | |
def withEvent[T](eventId: String)(block: Event => Future[T]): Future[T] = { | |
EventRepository.getByUuid(eventId).flatMap { | |
case Some(event) => block(event) | |
case None => Future.successful(NotFound("event not found")) | |
} | |
} | |
def withSession[T](sessionId: String)(block: Session => Future[T]): Future[T] = { | |
SessionRepository.getByUuid(eventId).flatMap { | |
case Some(session) => block(session) | |
case None => Future.successful(NotFound("session not found")) | |
} | |
} | |
} | |
object Sessions extends SilhouetteEnvironment with Shortcut { | |
def details(eventId: String, sessionId: String) = SecuredAction.async { implicit req => | |
withEvent(eventId) { event => | |
withSession(sessionId) { session => | |
Future.successful(Ok(views.html.Sessions.details(session, event))) | |
} | |
} | |
} | |
def doUpdate(eventId: String, sessionId: String) = SecuredAction.async { implicit req => | |
implicit val user = req.identity | |
withEvent(eventId) { event => | |
withSession(sessionId) { session => | |
createForm.bindFromRequest.fold( | |
formWithErrors => Future(BadRequest(views.html.Sessions.update(formWithErrors, session, event))), | |
formData => SessionRepository.update(sessionId, formData).map { err => | |
Redirect(controllers.routes.Sessions.details(eventId, sessionId)) | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice ! I like this way (quite composable) but it seems that event & session are not fetched in parallel...