Fortune.scala 971 B

12345678910111213141516171819202122232425262728293031323334
  1. package models
  2. import javax.inject.{Inject, Singleton}
  3. import play.api.db.slick.{HasDatabaseConfigProvider, DatabaseConfigProvider}
  4. import play.db.NamedDatabase
  5. import slick.driver.JdbcProfile
  6. import scala.concurrent.Future
  7. case class Fortune(id: Long, message: String)
  8. @Singleton()
  9. class FortunesDAO @Inject()(@NamedDatabase("hello_world") protected val dbConfigProvider: DatabaseConfigProvider) extends HasDatabaseConfigProvider[JdbcProfile] {
  10. import driver.api._
  11. private val Fortunes = TableQuery[FortunesTable]
  12. def getAll(): Future[Seq[Fortune]] = db.run(Fortunes.result)
  13. def findById(id: Long): Future[Option[Fortune]] = {
  14. db.run(Fortunes.filter(_.id === id).result.headOption)
  15. }
  16. private class FortunesTable(tag: Tag) extends Table[Fortune](tag, Some("hello_world"), "Fortune") {
  17. def id = column[Long]("id", O.PrimaryKey)
  18. def message = column[String]("message")
  19. def * = (id, message) <>(Fortune.tupled, Fortune.unapply)
  20. }
  21. }