World.scala 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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): Option[World] = DB.withSession { implicit session =>
  15. byId(id).firstOption
  16. }
  17. val updateQuery = Compiled{ (id: Column[Int]) => this.where(_.id === id) }
  18. def updateRandom(world: World) {
  19. DB.withSession { implicit session: Session =>
  20. updateQuery(world.id).update(world)
  21. }
  22. }
  23. }
  24. case class World(id: Int, randomNumber: Long)
  25. object World {
  26. implicit val toJson = new Writes[World] {
  27. def writes(w: World): JsValue = {
  28. Json.obj(
  29. "id" -> w.id,
  30. "randomNumber" -> w.randomNumber
  31. )
  32. }
  33. }
  34. }