World.scala 935 B

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 extends Table[World]("World") {
  8. def id = column[Int]("id", O.PrimaryKey)
  9. def randomNumber = column[Long]("randomNumber")
  10. def * = id ~ randomNumber <> (World.apply _, World.unapply _)
  11. val byId = createFinderBy(_.id)
  12. def findById(id: Int): Option[World] = DB.withSession { implicit session =>
  13. byId(id).firstOption
  14. }
  15. def updateRandom(world: World) {
  16. DB.withSession { implicit session: Session =>
  17. this.where(_.id === world.id.bind).update(world)
  18. }
  19. }
  20. }
  21. case class World(id: Int, randomNumber: Long)
  22. object World {
  23. implicit val toJson = new Writes[World] {
  24. def writes(w: World): JsValue = {
  25. Json.obj(
  26. "id" -> w.id,
  27. "randomNumber" -> w.randomNumber
  28. )
  29. }
  30. }
  31. }