Fortune.scala 912 B

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