I am using ReactiveMongo with PlayFramework and have the following code in my DAO:
def find(authType: AuthType.Value, authAccountId: String) =
collection.find(
Json.obj(Fields.AuthType → authType,
Fields.AuthAccountId → authAccountId)).one[Credentials].recover(wrapLastError)
where Credentials
is defined as:
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)
In the service class that uses the DAO find
method, I have the following code:
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
}
Now my questions is as follows: What is the best way to handle a case where the DAO find
method returns no results? In the example above, adding a Case None
changes the return type of the method to Object. So if I were to use it in a controller or any other class, it adds complexity (e.g. mapping/case/transform).
Thanks in advance!