0

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!

MojoJojo
  • 3,897
  • 4
  • 28
  • 54
  • Why would like to success with `Unit` if there is no matching credential in the DB. Doesn't seem fine to me. I suggest you add the explicit return type to the `def checkEmailPassword` to specify what you want. – cchantep Jun 07 '16 at 20:22
  • The rule of thumb up in Scala is to add return type annotation to at least public methods signature. It decreases compilation time and API becomes clearer for the code reader. As @cchantep already said - think about what do you want from your service class API. And think about what `None` case really means for your application. Is it a normal behavior and you want return some value or is a failure case and you want to throw an exception, for instance. – yahor Jun 08 '16 at 01:09

0 Answers0