I would like to find some object (Fight) in the DB and based on it's presence return this particular object or create a new object in DB and return the newly created object. I implemented the following function:
def findOrCreateFight(firstBoxer: BoxersRow, secondBoxer: BoxersRow, eventDate: java.sql.Date): Future[FightsRow] = {
for {
fight <- findByBoxersAndDate(firstBoxer, secondBoxer, eventDate)
} yield {
fight match {
case Some(f) => f
case None => createAndFindFight(firstBoxer, secondBoxer, eventDate)
}
}
}
findByBoxersAndDate function returns Future[Option[FightsRow]] object and createAndFindFight function returns Future[FightsRow]. Now the compiler shows an error in a line with createAndFindFight function:
type mismatch; found : scala.concurrent.Future[models.Tables.FightsRow] required: models.Tables.FightsRow
OK, so I need to get the completed result of this Future in the 'case None'. I thought about onComplete function but it returns Unit, not a desired FightsRow object. Any suggestion how to fix my function to have a best scala-able effect? :)
Best Regards