0

我有一个 Play 项目,其中包含一个用户模型。之前,我使用case class声明和object声明来访问我的应用程序中的模型。有用。

但现在我想声明一个 User 类,拥有多个实例;因为我知道当你有一个带有对象的模型时,它是一个单例,所以是一个实例。

我的项目是一个会有很多用户的网站。

当我声明我的班级时,例如:

class User(val username: String,  var firstname: String, var lastname: String, var company: String, var password: String){}

我的应用程序(Application.scala)未识别该模型

我怎样才能意识到我想要什么?

旧代码:

case class User(username: String, password: String, firstname: String, lastname: String, company: String)

object User {

val simple = {
  get[String]("username")  ~
  get[String]("password") ~
  get[String]("firstname") ~
  get[String]("lastname")  ~
  get[String]("company") map {
    case username~password~firstname~lastname~company =>
      User(username, password, firstname, lastname, company)
  }
}

def findAll(): Seq[User] = {
  DB.withConnection { implicit connection =>
    SQL("SELECT * FROM user").as(User.simple *)
  }
}

def create(user: User): Unit = {
  DB.withConnection { implicit connection =>
    SQL("INSERT INTO user(username, password, firstname, lastname, company) VALUES   ({username}, {password}, {firstname}, {lastname}, {company})").on(
      'username  -> user.username,
      'password  -> user.password,
      'firstname -> user.firstname,
      'lastname  -> user.lastname,
      'company   -> user.company
    ).executeUpdate()
  }
}

def getOne(username: String): Option[User] = {
  DB.withConnection { implicit connection =>
    val selectUser = SQL("SELECT username, password, firstname, lastname, company FROM user where username={username}")
                      .on('username -> "azuken")()
                      .collectFirst {
                        case Row(username: String, password: String, firstname: String, lastname: String, company: String) =>
                          User(username, password, firstname, lastname, company)
                        case Row(username:String, password:String, firstname: String) =>
                          User(username, password, "bobby", "lqsdast", "sdf")
                        case _ =>
                          User("username", "password", "firstname", "lastname", "company")
                      }
    selectUser
  }
} 

def authentication(username: String, password: String): Long = {
  DB.withConnection { implicit connection =>
    val firstRow = SQL("SELECT COUNT(*) AS NbRep FROM user WHERE username = {username} AND password = {password}").on(
      'username -> username,
      'password -> password
    ).apply().head
    firstRow[Long]("NbRep")
  }
} 

}

4

0 回答 0