0

我正在为我的 scala Play 使用 play-slick!虚拟休息 API。

所以,我必须从多个表中获取记录。但是,它们是相互依赖的,即

Table_1   Table_2
id1       id2
id2

要从 Table_2 中获取记录,我必须从 Table_1 中获取记录,然后使用 id2 从 Table_2 中获取。

我的控制器:

def getEntity(id : Long) = Action.async {
  table1DAO.findById(id) map { t1 =>
    t1 map { t1Entity =>
      table2DAO.findById(t1Entity.id2) map { t2 =>
        t2 map { t2Entity =>
          Ok(Json.toJson(CombiningClass(t1Entity, t2Entity)))
        } getOrElse { Ok(Json.toJson(t1Entity)) }
      }
    } getOrElse { NoContent }
  }
}

编译后,我得到:

[error] DummyController.scala:17: overloaded method value async with alternatives:
[error]   [A](bodyParser: play.api.mvc.BodyParser[A])(block: play.api.mvc.Request[A] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[A] <and>
[error]   (block: play.api.mvc.Request[play.api.mvc.AnyContent] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent] <and>
[error]   (block: => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent]
[error]  cannot be applied to (scala.concurrent.Future[Object])
[error]   def getEntity(id : Long) = Action.async {
[error]                                ^
[error] one error found

这是我的 DAO 方法:

def findById(id : Long): Future[Option[A]] = {
  db.run(tableQ.filter(_.id === id).result.headOption)
}

PS:我对功能范式和scala非常陌生,所以如果可以,请原谅我的无知。

4

1 回答 1

1

简单地解决您的问题:

def getEntity(id : Long) = Action.async {
  findById(id) flatMap {
    case Some(t1Entity) =>
      findById(t1Entity.id2) map { t2Opt =>
        t2Opt map { t2Entity =>
          Ok(Json.toJson(t1Entity, t2Entity))
        } getOrElse { Ok(Json.toJson(t1Entity)) }
      }
    case None => Future.successful(NoContent)
  }
}

这里的问题是你不能在 scala 中一起使用flatMap Option是一篇关于这个主题的精彩而简单的文章(使用自定义monad 实现作为解决方案)。长话短说,我会使用cat 库(甚至 scalaz 库)和功能。我稍微简化了你的代码。FutureFutureOOptionT

def getEntity(id : Long) = Action.async {
  (for {
    t1 <- daoFindById(id)
    t2 <- daoFindById(t1.id2)
  } yield (t1, t2)).map{
    result => Ok(Json.toJson(result))
  }.getOrElse(NoContent)
}

case class T(id2: Long)
def daoFindById(id : Long): OptionT[Future, T] = {
  OptionT[Future, T](db.run(tableQ.filter(_.id === id).result.headOption))
}

您现在可以轻松地flatMap越过这个OptionTmonad,而不必关心您是否正在处理Optionor Future(因为在 scala 中的理解只是一种语法糖)。

于 2016-06-12T08:04:41.987 回答