我是 Scala 的新手,目前正在尝试使用 play 框架。
这是我写的工作代码:
def authenticate = Action (BodyParsers.parse.json) { req =>
req.body.validate[AuthenticationForm].map {form =>
UserRepository.findByCredentials(form).map { user =>
user.apiKeys.find(_.deviceId == form.deviceId).map { apiKey =>
Ok(Json.toJson(apiKey))
}.getOrElse({
// HOW DO I TRANSFORM THIS INTO MORE BEAUTIFUL CODE
val createdApiKey = ApiKeyRepository.create(new ApiKey(form.deviceId, form.deviceId))
val userToWithNewApiKey = user.copy(apiKeys = user.apiKeys.:+(createdApiKey))
UserRepository.update(userToWithNewApiKey)
Ok(Json.toJson(createdApiKey))
})
}.getOrElse {
Unauthorized
}
}.getOrElse {
BadRequest
}
}
嗯,这看起来不太好。我们能做得更好吗?我还不能。但我看到了这个 stackoverflow 帖子:https ://stackoverflow.com/a/24085333/3038183看起来很不错:)
现在我想知道如何转换我的代码,所以它看起来像给定的例子。当然我已经尝试过了,但我无法让它编译,也不知道如何处理注释后的代码(“我如何将它转换成更漂亮的代码”)。就我而言,我使用的是 play.api.mvc.Result 而不是上面链接中给出的“失败”。那么我的 Either[play.api.mvc.Result, ?WhatHere?] 应该是什么类型?
此致
编辑:我接受了特拉维斯的回答。非常感谢。
对于任何对这里感兴趣的人来说,我可以编写更好看的代码,这要感谢 Travis:
def getApiKey(user: User, deviceId: String) : ApiKey = {
user.apiKeys.find(_.deviceId == deviceId).getOrElse {
val createdApiKey =
ApiKeyRepository.create(new ApiKey(deviceId, deviceId))
val userToWithNewApiKey =
user.copy(apiKeys = user.apiKeys.:+(createdApiKey))
UserRepository.update(userToWithNewApiKey)
createdApiKey
}
}
def authenticate = Action (BodyParsers.parse.json) { req =>
(for {
form <- req.body.validate[AuthenticationForm].asOpt.toRight(BadRequest).right
user <- UserRepository.findByCredentials(form).toRight(Unauthorized).right
} yield {
Ok(Json.toJson(getApiKey(user, form.deviceId)))
}).merge
}