gpt4 book ai didi

Scala 异步/回调代码重写

转载 作者:行者123 更新时间:2023-12-02 19:55:18 25 4
gpt4 key购买 nike

应该通过检查用户、用户是否处于事件状态以及更新上次登录日期时间之后的简单代码。

  def authenticate() = Action.async { implicit request => 
loginForm.bindFromRequest.fold(
errors => Future.successful(BadRequest(views.html.logon(errors))),
usersData =>{
val cursor = this.collection.find(BSONDocument("name" -> usersData._1)).one[Account].map(_.filter(p=>p.password == hashedPass(usersData._2, usersData._1)))
cursor.flatMap(p => p match {
case None => Future.successful(BadRequest(views.html.logon(loginForm.withGlobalError("user/pass incorect!!!"))))
case Some(user) => {
if(!user.active)
Future.successful(BadRequest(views.html.logon(loginForm.withGlobalError("inactive!!!"))))
else collection.update(BSONDocument("_id" -> user.id),
BSONDocument("$set" ->
BSONDocument("lastLogin" -> BSONDateTime(new org.joda.time.DateTime().getMillis()))))
.flatMap(x => gotoLoginSucceeded(user.id.stringify))

}
})
})
}

如何将其重写为更少的 flatMap/map 意大利面?

另一种解决方案

def authenticate() = AsyncStack { implicit request => 
loginForm.bindFromRequest.fold(
errors => Future.successful(BadRequest(views.html.logon(errors))),
usersData =>{
for{
user <- this.collection.find(BSONDocument("name" -> usersData._1)).one[Account].map(_.filter(p=>p.password == hashedPass(usersData._2, usersData._1)))
update <- {
lazy val update = collection.update(BSONDocument("_id" -> user.get.id),
BSONDocument("$set" ->
BSONDocument("lastLogin" -> BSONDateTime(new org.joda.time.DateTime().getMillis()))))
update
}
result <- {
lazy val result = gotoLoginSucceeded(user.get.id.stringify)
result
}
} yield
if(user.isEmpty) BadRequest(views.html.logon(loginForm.withGlobalError("login\pass mismatch")))
else if(!user.get.active) BadRequest(views.html.logon(loginForm.withGlobalError("inactive")))
else if(update.err.isEmpty) result
else InternalServerError(views.html.logon(loginForm.withGlobalError("server error")))
})

}

最佳答案

我可能会将代码重构为如下所示:

def authenticate() = Action.async { implicit request => 
loginForm.bindFromRequest.fold(
hasErrors = displayFormWithErrors,
success = loginUser)
}

private def displayFormWithErrors[T](errors:Form[T]) =
Future.successful(BadRequest(views.html.logon(errors)))

private def loginUser(userData:(String, String)) = {
val (username, password) = userData

findUser(username, password)
.flatMap {
case None =>
showLoginFormWithError("user/pass incorect!!!")
case Some(user) if (!user.active) =>
showLoginFormWithError("inactive!!!")
case Some(user) =>
updateUserAndRedirect(user)
}
}

private def findUser(username:String, password:String) =
this.collection
.find(BSONDocument("name" -> username))
.one[Account]
.map(_.filter(_.password == hashedPass(password, username)))

private def showLoginFormWithError(error:String) =
Future.successful(BadRequest(
views.html.logon(loginForm.withGlobalError(error))))

private def updateUserAndRedirect(user:Account) =
updateLastLogin(user)
.flatMap(_ => gotoLoginSucceeded(user.id.stringify))

private def updateLastLogin(user:Account) =
collection
.update(BSONDocument("_id" -> user.id),
BSONDocument("$set" ->
BSONDocument("lastLogin" ->
BSONDateTime(new JodaDateTime().getMillis()))))

关于Scala 异步/回调代码重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21709112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com