World.scala 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package models
  2. import play.api.Play.current
  3. import play.api.db.slick.Config.driver.simple._
  4. import play.api.db.slick.DB
  5. import play.api.libs.json._
  6. import play.api.libs.functional.syntax._
  7. class Worlds(tag: Tag) extends Table[World](tag, "World") {
  8. def id = column[Int]("id", O.PrimaryKey)
  9. def randomNumber = column[Long]("randomNumber")
  10. def * = (id, randomNumber) <> ((World.apply _).tupled, World.unapply _)
  11. }
  12. class WorldsTableQuery extends TableQuery(new Worlds(_)){
  13. val byId = this.findBy(_.id)
  14. def findById(id: Int)(implicit session: Session): World = {
  15. byId(id).first
  16. }
  17. val updateQuery = Compiled{ (id: Column[Int]) => this.filter(_.id === id) }
  18. def updateRandom(world: World)(implicit session: Session) {
  19. updateQuery(world.id).update(world)
  20. }
  21. }
  22. case class World(id: Int, randomNumber: Long)
  23. object World {
  24. implicit val toJson = new Writes[World] {
  25. def writes(w: World): JsValue = {
  26. Json.obj(
  27. "id" -> w.id,
  28. "randomNumber" -> w.randomNumber
  29. )
  30. }
  31. }
  32. }