Skip to content

Instantly share code, notes, and snippets.

@loicknuchel
Last active March 30, 2018 19:31
Show Gist options
  • Save loicknuchel/faa974c3661351b73227 to your computer and use it in GitHub Desktop.
Save loicknuchel/faa974c3661351b73227 to your computer and use it in GitHub Desktop.
Scala for-comprehension
object Sessions extends SilhouetteEnvironment {
def details(eventId: String, sessionId: String) = SecuredAction.async { implicit req =>
implicit val user = req.identity
val res: Future[Result] = for {
eventOpt: Option[Event] <- EventRepository.getByUuid(eventId)
sessionOpt: Option[Session] <- SessionRepository.getByUuid(sessionId)
} yield {
var res2: Option[Result] = for {
event: Event <- eventOpt
session: Session <- sessionOpt
} yield {
Ok(backend.views.html.Events.Sessions.details(session, List(), event))
}
res2.getOrElse { NotFound(views.html.error("404", "Event not found...")) }
}
res
}
def doUpdate(eventId: String, sessionId: String) = SecuredAction.async { implicit req =>
implicit val user = req.identity
val res: Future[Future[Result]] = for {
eventOpt: Option[Event] <- EventRepository.getByUuid(eventId)
sessionOpt: Option[Session] <- SessionRepository.getByUuid(sessionId)
} yield {
val res2: Option[Future[Result]] = for {
event: Event <- eventOpt
session: Session <- sessionOpt
} yield {
createForm.bindFromRequest.fold(
formWithErrors => Future(BadRequest(views.html.Sessions.update(formWithErrors, session, event))),
formData: Session => SessionRepository.update(sessionId, formData).map { err: LastError =>
Redirect(controllers.routes.Sessions.details(eventId, sessionId))
})
}
res2.getOrElse(Future(NotFound(views.html.error("404", "Event not found..."))))
}
res.flatMap(identity)
}
}
object Sessions extends SilhouetteEnvironment {
def details(eventId: String, sessionId: String) = SecuredAction.async { implicit req =>
implicit val user = req.identity
val res: Future[Result] = for {
Some(event: Event) <- EventRepository.getByUuid(eventId)
Some(session: Session) <- SessionRepository.getByUuid(sessionId)
} yield {
Ok(backend.views.html.Events.Sessions.details(session, List(), event))
}
// throws 'NoSuchElementException: Future.filter predicate is not satisfied' on None :(
res.recover {
case NoSuchElementException => Future(NotFound("Not found"))
}
}
def doUpdate(eventId: String, sessionId: String) = SecuredAction.async { implicit req =>
implicit val user = req.identity
val res: Future[Future[Result]] = for {
Some(event: Event) <- EventRepository.getByUuid(eventId)
Some(session: Session) <- SessionRepository.getByUuid(sessionId)
} yield {
createForm.bindFromRequest.fold(
formWithErrors => Future(BadRequest(views.html.Sessions.update(formWithErrors, session, event))),
formData: Session => SessionRepository.update(sessionId, formData).map { err: LastError =>
Redirect(controllers.routes.Sessions.details(eventId, sessionId))
})
}
res.flatMap(identity).recover {
case NoSuchElementException => Future(NotFound("Not found"))
}
}
}
object Sessions extends SilhouetteEnvironment {
def details(eventId: String, sessionId: String) = SecuredAction.async { implicit req =>
implicit val user = req.identity
withEvent(eventId) { event =>
withSession(sessionId) { session =>
Ok(backend.views.html.Events.Sessions.details(session, List(), 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: Session => SessionRepository.update(sessionId, formData).map { err: LastError =>
Redirect(controllers.routes.Sessions.details(eventId, sessionId))
})
}
}
}
def withEvent(eventId: String)(block: Event => Future[Result]): Future[Result] = {
EventRepository.getByUuid(eventId).flatMap {
case Some(event: Event) => block(event)
case None => Future(NotFound("Event not found"))
}
}
def withSession(sessionId: String)(block: Session => Future[Result]): Future[Result] = {
SessionRepository.getByUuid(sessionId).flatMap {
case Some(session: Session) => block(session)
case None => Future(NotFound("Session not found"))
}
}
}
@loicknuchel
Copy link
Author

@vil1 no shame for promoting good libs :)

Your solution is really elegant and not that nerdy (as some scala code could be...).

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