Created
January 1, 2017 21:35
-
-
Save mikegehard/5534225603e736f61dd1d5d317847c3b 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
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
commented
Jan 1, 2017
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