我正在使用带有 PlayFramework 的 ReactiveMongo,并且在我的 DAO 中有以下代码:
def find(authType: AuthType.Value, authAccountId: String) =
collection.find(
Json.obj(Fields.AuthType → authType,
Fields.AuthAccountId → authAccountId)).one[Credentials].recover(wrapLastError)
其中Credentials
定义为:
case class Credentials(
authType: AuthType.Value,
accountId: EntityId,
authAccountId: String,
passwordHash: Option[String],
authToken: Option[String] = None,
expirationTime: Option[DateTime] = None,
id: EntityId = Entity.nextId)
在使用 DAO 方法的服务类中find
,我有以下代码:
def checkEmailPassword(email: String, password: String) =
credentialsDAO.find(AuthType.EmailPassword, email).map {
case Some(c: Credentials) if c.passwordHash.exists(ph ⇒ BCrypt.check(password, ph)) ⇒
CheckResult(c.accountId)
case None => Unit
}
现在我的问题如下:处理 DAOfind
方法没有返回结果的情况的最佳方法是什么?在上面的示例中,添加 aCase None
会将方法的返回类型更改为 Object。因此,如果我要在控制器或任何其他类中使用它,它会增加复杂性(例如映射/案例/转换)。
提前致谢!