Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active May 18, 2020 08:07
Show Gist options
  • Save adamw/8d291057f52db93ee2d9e9a4a164c65f to your computer and use it in GitHub Desktop.
Save adamw/8d291057f52db93ee2d9e9a4a164c65f to your computer and use it in GitHub Desktop.
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
// should fail with Error if user not found
def authorize(authToken: AuthToken): Future[User] = ???
def findPetForUser(user: User, id: UUID): Future[Option[Pet]] = ???
def addPetToUser(user: User, pet: Pet): Future[Unit] = ???
val getPetWithLogic = getPet.serverLogicRecoverErrors {
case (authToken, id) =>
authorize(authToken).flatMap { user =>
findPetForUser(user, id).flatMap {
case Some(pet) => Future.successful(pet)
case None => Future.failed(Error("Not found", StatusCode.NotFound))
}
}
}
val addPetWithLogic = addPet.serverLogicRecoverErrors {
case (authToken, pet) =>
authorize(authToken).flatMap { user =>
addPetToUser(user, pet)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment