0

我的 Playframework 项目中有以下 DAO、服务对象和控制器模式:

CredentialsDAO.scala

def find(authType: AuthType.Value, authAccountId: String) =
    collection.find(
      Json.obj(Fields.AuthType → authType,
        Fields.AuthAccountId → authAccountId)).one[Credentials].recover(wrapLastError)

CredentialsService.scala

def checkEmailPassword(email: String, password: String) = credentialsDAO.find(AuthType.EmailPassword, email).map(_.get) //Needs review

在我的控制器中:

def auth = Action.async(parse.json) { request =>
    {

      val authRequest = request.body.validate[AuthRequest]
      authRequest.fold(
        errors => Future(BadRequest),
        auth => {
          //First verify username and password
          val authRequestResult = for {
            validCredential <- credentialsManager.checkEmailPassword(auth.email, auth.password)
            validPassword <- BCrypt.checkFuture(auth.password, validCredential.passwordHash)
            validAccount <- accountManager.getAccount(validCredential)
            session <- sessionManager.createSession(validAccount.id, validAccount.roles)

            touchedSession <- sessionManager.touchSession(session.id)

          } yield AuthResponse(session.id, session.token, validAccount.id, validAccount.roles)

          authRequestResult map {
            case res: AuthResponse => Ok(Json.toJson(res))
            case _ => NotFound

          }
        })
    }
  }

问题:从函数式编程的角度来看,这种“模式”可以吗?特别是,我对以下行感到困扰CredentialsService.scala

def checkEmailPassword(email: String, password: String) = credentialsDAO.find(AuthType.EmailPassword, email).map(_.get) //Needs review

我是 Scala 的新手,但我认为_.get在上面的行中有更好的处理方法?任何建议/想法将不胜感激。

提前致谢。

4

0 回答 0