gpt4 book ai didi

scala - Slick 3.1 - 将 Slick 表格行对象转换为案例类

转载 作者:行者123 更新时间:2023-12-05 04:14:48 28 4
gpt4 key购买 nike

我使用 Slick 3.1 代码生成器来创建默认对象和特征 Tables.scala

下面的方法有效,但我想隐式或显式地将 UserRow 和 PasswordsRow 转换为 User 和 UserPassword。

工作方式:

override def getUser(email: String): Future[Option[(Tables.UsersRow, Tables.PasswordsRow)]] = db.run {
(for {
user <- users if user.email === email
password <- passwords if password.id === user.id
} yield (user, password)).result.headOption

所需的方法:

  override def getUser(email: String): Future[Option[(User, UserPassword)]] = db.run {
(for {
user <- users if user.email === email
password <- passwords if password.id === user.id
} yield (user, password)).result.headOption
}

用户.scala

  package model

import com.wordnik.swagger.annotations.{ ApiModel, ApiModelProperty }
import slick.jdbc.GetResult
import spray.json.DefaultJsonProtocol

import scala.annotation.meta.field

case class User(
id: Int,
email: String,
name: Option[String] = None,
surname: Option[String] = None,
passwordId: Option[Int] = None
)

object User extends DefaultJsonProtocol{
implicit val getUserResult = GetResult(r => User(r.<<, r.<<, r.<<, r.<<, r.<<))

implicit val userFormat = jsonFormat5(User.apply)
}

用户密码.scala

package model

import com.github.t3hnar.bcrypt.{Password, generateSalt}
import slick.jdbc.GetResult


case class UserPassword(id: Int, hashedPassword: Option[String], salt: String = generateSalt) {
def passwordMatches(password: String): Boolean = hashedPassword.contains(password.bcrypt(salt))
}

object UserPassword {

implicit val getUserPasswordResult = GetResult(r => UserPassword(r.<<, r.<<, r.<<))

def newWithPassword(password: String) = {
val salt = generateSalt
new UserPassword(0, Some(password.bcrypt(salt)), salt)
}
}

最佳答案

也许是这样的?

val futureUserRowAndPwdRow = getUser(email)
val futureUser: Future[Option[(User, UserPassword)]] = futureUserRowAndPwdRow map {
maybeUserRow => maybeUserRow map {
case (userRow, pwdRow) => (User(userRow.whatever....), UserPassword(..))
}
}

关于scala - Slick 3.1 - 将 Slick 表格行对象转换为案例类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34153541/

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