我的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
}
})
}
}
问题:这是&#39;模式&#39;从功能编程的角度来看还好吗?特别是,我在CredentialsService.scala
:
def checkEmailPassword(email: String, password: String) = credentialsDAO.find(AuthType.EmailPassword, email).map(_.get) //Needs review
我是Scala的新手,但我认为在上面的行中有更好的方法来处理_.get
?任何建议/想法都将受到高度赞赏。
提前致谢。