我刚开始玩框架游戏。我正在开发一个基于play的应用程序,在这个应用程序中,我希望从DB中获取一个对象列表,其中将包含一些选定的字段。为了获取我使用的所有字段,我使用以下代码:
case class Mail(txID: String,
timeStamp: Long,
toUserID: String,
mailContent: String,
mailTemplateFileName: String,
fromID: String,
toID: String
)def getLogFromIDFuture(userID: String): Future[Option[List[Mail]]] = cache.getOrElseUpdate[Option[List[Mail]]](userID) {
val resultingUsers = db.run(mailsData.filter(x => x.toUserID === userID).result)
val res = Await.result(resultingUsers, Duration.Inf)
res.map(t => t) match {
case t if t.nonEmpty =>
Future(Some(t.toList))
case _ => Future(None)
}
}因此,我的问题是如何只获取timeStamp、toUserID、mailContent、fromID、toID字段作为像UserMessage(timeStamp: Long, toUserID: String, mailContent: String, fromID: String, toID: String)这样的对象列表。我试着搜索这个,但没有得到任何令人信服的答案。
发布于 2019-12-02 14:14:51
就像我在评论中说的,你可以这样做:
def getLogFromIDFuture(userID: String): Future[Option[List[UserMessage]]] = cache.getOrElseUpdate[Option[List[Mail]]](userID) {
val resultingUsers = db.run(mailsData.filter(x => x.toUserID === userID).map(entry =>(entry.timeStamp, entry.toUserID, entry.mailContent, entry.fromID, entry.toID))
.result)// here you have the tuple of things
// add the mapping of the tuple to the UserMessage
val res = Await.result(resultingUsers, Duration.Inf)
res.map(t => t) match {
case t if t.nonEmpty =>
Future(Some(t.toList))
case _ => Future(None)
}
}你可以摆脱那个Await.result
resultingUsers.map( match {
case t if t.nonEmpty =>
Some(t.toList)
case _ => None
}
)希望能帮上忙。
https://stackoverflow.com/questions/59135897
复制相似问题