Skip to content

Instantly share code, notes, and snippets.

@mikegehard
Created January 1, 2017 21:35
Show Gist options
  • Save mikegehard/5534225603e736f61dd1d5d317847c3b to your computer and use it in GitHub Desktop.
Save mikegehard/5534225603e736f61dd1d5d317847c3b to your computer and use it in GitHub Desktop.
def apply(petRepository: PetRepository, contestResultRepository: ContestResultRepository, rules: Rules)
(pet1Id: UUID, pet2Id: UUID)
(implicit ec: ExecutionContext): Future[Either[String, UUID]] = {
val id = java.util.UUID.randomUUID
for {
pet1: Option[Pet] <- petRepository.find(pet1Id)
pet2: Option[Pet] <- petRepository.find(pet2Id)
// this is messy, try to change it to a more functional style at some point
} yield if (pet1.isDefined && pet2.isDefined) {
// Next step is to do this vai an Akka actor so you don't tie up the response because the rules can take a long time to resolve
contestResultRepository.create(id, rules(pet1.get, pet2.get))
Right(id)
} else {
// Make this better so you know what pet wasn't found
Left("One of the pets was not found.")
}
}
@jdegoes
Copy link

jdegoes commented Jan 1, 2017

def apply(petRepository: PetRepository, contestResultRepository: ContestResultRepository, rules: Rules)
  (pet1Id: UUID, pet2Id: UUID)
  (implicit ec: ExecutionContext): Future[Either[String, UUID]] =
    for {
      uuid    <- Future(java.util.UUID.randomUUID)
      pet1Opt <- petRepository.find(pet1Id)
      pet2Opt <- petRepository.find(pet2Id)
      rez     <- (for {
                   pet1 <- pet1Opt
                   pet2 <- pet2Opt
                 } yield Future({contestResultRepository.create(uuid, rules(pet1, pet2)); Right(uuid)})).getOrElse(Future(Left("One of the pets was not found.")))
    } yield rez

@mikegehard
Copy link
Author

def apply(petRepository: PetRepository, contestResultRepository: ContestResultRepository, rules: Rules)(pet1Id: UUID, pet2Id: UUID)(implicit ec: ExecutionContext): Future[Either[String, UUID]] = {
    val result = for {
      pet1 <- OptionT(petRepository.find(pet1Id))
      pet2 <- OptionT(petRepository.find(pet2Id))
    } yield contestResultRepository.create(java.util.UUID.randomUUID, rules(pet1, pet2))

    result.fold(Left("One pet not found"): Either[String, UUID]) { id => Right(id) }
  }

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