Skip to content

Instantly share code, notes, and snippets.

@julienlafont-tabmo
Last active September 2, 2015 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julienlafont-tabmo/154700fcab410ee06c20 to your computer and use it in GitHub Desktop.
Save julienlafont-tabmo/154700fcab410ee06c20 to your computer and use it in GitHub Desktop.
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))
})
}
}
}
@loicknuchel
Copy link

Nice ! I like this way (quite composable) but it seems that event & session are not fetched in parallel...

@loicknuchel
Copy link

I added your suggestion to my gist : https://gist.github.com/loicknuchel/faa974c3661351b73227

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment