如何使用过滤器获取布尔值 - Scala Slick
How to use Filter to get Boolean - Scala Slick
我有账户表:
import models.{Account, Category}
import play.api.Play
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfig}
import slick.driver.JdbcProfile
import slick.driver.PostgresDriver.api._
import slick.lifted.Tag
import play.api.libs.json.{JsValue, Writes, Json}
object AccountTable extends AccountTable
trait AccountTable extends HasDatabaseConfig[JdbcProfile]{
val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
class Accounts(tag: Tag) extends Table[Account](tag, "account") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def login = column[String]("login")
def password = column[String]("password")
def * = (id, login, password) <> ((Account.apply _).tupled, Account.unapply)
}
val accounts = TableQuery[Accounts]
implicit val accountFormat = Json.format[Account]
def auth(login: String, password: String): Boolean = {
db.run(findByLogin(login, password).result.headOption)
}
private def findByLogin(login: String, password: String) = accounts.filter(x => (x.password === password && x.login === login))
}
我尝试开发auth
方法。我真的不明白,如何完成这个方法。我尝试了不同的方法,但总是得到不同的错误。
选项 1:
def auth(login: String, password: String): Future[Boolean] = {
val action = findByLogin(login, password).result.headOption
.map(authOpt => authOpt.getOrElse(false))
db.run(action)
}
选项 2:
def auth(login: String, password: String): Boolean = {
val action = findByLogin(login, password).result.headOption
.map(authOpt => authOpt.getOrElse(false))
Await.result(db.run(action), 5 seconds)
}
上面的代码未经测试,但您应该了解 slick 查询背后的想法。如果在数据库中没有找到结果,我假设 auth 应该 return false
。
最后一句话:我强烈建议使用选项 1 并使用 Future
,因为 Await.result
会阻止线程执行。
我有账户表:
import models.{Account, Category}
import play.api.Play
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfig}
import slick.driver.JdbcProfile
import slick.driver.PostgresDriver.api._
import slick.lifted.Tag
import play.api.libs.json.{JsValue, Writes, Json}
object AccountTable extends AccountTable
trait AccountTable extends HasDatabaseConfig[JdbcProfile]{
val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
class Accounts(tag: Tag) extends Table[Account](tag, "account") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def login = column[String]("login")
def password = column[String]("password")
def * = (id, login, password) <> ((Account.apply _).tupled, Account.unapply)
}
val accounts = TableQuery[Accounts]
implicit val accountFormat = Json.format[Account]
def auth(login: String, password: String): Boolean = {
db.run(findByLogin(login, password).result.headOption)
}
private def findByLogin(login: String, password: String) = accounts.filter(x => (x.password === password && x.login === login))
}
我尝试开发auth
方法。我真的不明白,如何完成这个方法。我尝试了不同的方法,但总是得到不同的错误。
选项 1:
def auth(login: String, password: String): Future[Boolean] = {
val action = findByLogin(login, password).result.headOption
.map(authOpt => authOpt.getOrElse(false))
db.run(action)
}
选项 2:
def auth(login: String, password: String): Boolean = {
val action = findByLogin(login, password).result.headOption
.map(authOpt => authOpt.getOrElse(false))
Await.result(db.run(action), 5 seconds)
}
上面的代码未经测试,但您应该了解 slick 查询背后的想法。如果在数据库中没有找到结果,我假设 auth 应该 return false
。
最后一句话:我强烈建议使用选项 1 并使用 Future
,因为 Await.result
会阻止线程执行。