Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. @Singleton
  2. class UserDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
  3. private val dbConfig = dbConfigProvider.get[JdbcProfile]
  4.  
  5. import dbConfig._
  6. import profile.api._
  7.  
  8. private class UserTable(tag: Tag) extends Table[User](tag, "dc_user") {
  9. def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  10.  
  11. def username = column[String]("username")
  12.  
  13. def email = column[String]("email")
  14.  
  15. def password = column[String]("password")
  16.  
  17. def firstName = column[String]("first_name")
  18.  
  19. def lastName = column[String]("last_name")
  20.  
  21. def createdAt = column[Timestamp]("created_at", SqlType("timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP"))
  22.  
  23. def * = (id, username, email, password, firstName, lastName, createdAt) <> ((User.apply _).tupled, User.unapply)
  24. }
  25.  
  26. private val users = TableQuery[UserTable]
  27.  
  28. def add(user: User): Future[Int] = db.run {
  29. users += user
  30. }
  31.  
  32. def list(page: Int = 0, pageSize: Int = 10, orderBy:Int = 1, filter:String = "%"): Future[Seq[User]] = db.run (
  33. users.filter(_.firstName === filter).drop(page * pageSize).take(pageSize).result
  34. )
  35.  
  36. def update(user: User): Future[Int] = db.run {
  37. users.update(user)
  38. }
  39.  
  40. def delete(id: Long): Future[Unit] = db.run {
  41. users.filter(_.id === id).delete.map(_ => ())
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement