我是 Scala 的新手,甚至我想要实现的目标对于 Java 来说都太简单了,我对 Scala 感到困惑。
我想要的是得到一个User
然后Permission
使用另一个查询并根据他Role
和他的个人来填充他Permissions
。
直到知道我有以下代码:
/**
* Finds a user by its loginInfo.
*
* @param loginInfo The loginInfo of the user to find.
* @return The found user or None if no user for the given login info could be found.
*/
def find(loginInfo: LoginInfo): Future[Option[models.admin.User]] = {
val userQuery = for {
dbLoginInfo <- loginInfoQuery(loginInfo)
dbUserLoginInfo <- Userlogininfo.filter(_.logininfoid === dbLoginInfo.id)
dbUser <- User.filter(_.userid === dbUserLoginInfo.userid)
user <- dbUser match {
case u =>
val permissionQuery = for {
dbUserPermission <- Userpermission.filter(_.userid === u.userid)
dbPermission <- Permission.filter(_.id === dbUserPermission.permissionid)
} yield dbPermission
val rolePermissionQuery = for {
dbUserRole <- Userrole.filter(_.userid === u.userid)
dbRole <- Role.filter(_.id === dbUserRole.roleid)
dbRolePermission <- Rolepermission.filter(_.roleid === dbRole.id)
dbPermission <- Permission.filter(_.id === dbRolePermission.permissionid)
} yield dbPermission
val unionPermissionQuery = permissionQuery union rolePermissionQuery
db.run(unionPermissionQuery.result).map(_.map(_.name).toList).map { permission =>
models.admin.User(
UUID.fromString(u.userid.toString),
u.firstname.toString,
u.lastname.toString,
u.jobtitle.toString,
loginInfo,
u.email.toString,
false,
Some(permission),
false)
}
case None => None
}
} yield user
db.run(userQuery.result.headOption)
}
我收到以下错误:
pattern type is incompatible with expected type;
[error] found : None.type
[error] required: UserDAOImpl.this.User
[error] case None => None
[error] ^
[error] play/modules/admin/app/models/daos/impl/UserDAOImpl.scala:36: value map is not a member of Object
[error] user <- dbUser match {
[error] ^
[error] play/modules/admin/app/models/daos/impl/UserDAOImpl.scala:34: type mismatch;
[error] found : UserDAOImpl.this.Userlogininfo => slick.lifted.Query[Nothing,Nothing,Seq]
[error] required: UserDAOImpl.this.Userlogininfo => slick.lifted.Query[Nothing,T,Seq]
[error] dbUserLoginInfo <- Userlogininfo.filter(_.logininfoid === dbLoginInfo.id)
[error] ^
[error] play/modules/admin/app/models/daos/impl/UserDAOImpl.scala:33: type mismatch;
[error] found : UserDAOImpl.this.Logininfo => slick.lifted.Query[Nothing,Nothing,Seq]
[error] required: UserDAOImpl.this.Logininfo => slick.lifted.Query[Nothing,T,Seq]
[error] dbLoginInfo <- loginInfoQuery(loginInfo)
[error] ^
[error] play/modules/admin/app/models/daos/impl/UserDAOImpl.scala:69: value headOption is not a member of UserDAOImpl.this.driver.DriverAction[Seq[Nothing],UserDAOImpl.this.driver.api.NoStream,slick.dbio.Effect.Read]
[error] db.run(userQuery.result.headOption)
[error] ^
[error] 5 errors found