Skip to content

Commit

Permalink
Merge pull request #27 from lenguyenthanh/remove-one-paginator
Browse files Browse the repository at this point in the history
We have two paginators 😱
  • Loading branch information
ornicar authored Apr 10, 2024
2 parents a88a6d4 + b694bd2 commit 3dc62a6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 183 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ lazy val model: Project = Project("model", file("model"))
commonSettings,
name := "scalalib-model"
)
.dependsOn(core)
.dependsOn(core, playJson)

lazy val playJson: Project = Project("playJson", file("playJson"))
.settings(
Expand All @@ -49,7 +49,7 @@ lazy val playJson: Project = Project("playJson", file("playJson"))
"org.playframework" %% "play-json" % "3.0.2"
)
)
.dependsOn(core, model)
.dependsOn(core)

// functions that useful for lila & lila-ws
lazy val lila: Project = Project("lila", file("lila"))
Expand Down
24 changes: 24 additions & 0 deletions lila/src/main/scala/Json.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scalalib

import play.api.libs.json.{ Json as PlayJson, * }
import scalalib.json.Json.*
import scalalib.model.MaxPerPage
import scalalib.paginator.Paginator

object Json:
export scalalib.json.Json.{ *, given }
export scalalib.json.extensions.{ *, given }

given Writes[MaxPerPage] with
def writes(m: MaxPerPage) = JsNumber(m.value)

given paginatorWrite[A: Writes]: OWrites[Paginator[A]] = OWrites[Paginator[A]]: p =>
PlayJson.obj(
"currentPage" -> p.currentPage,
"maxPerPage" -> p.maxPerPage,
"currentPageResults" -> p.currentPageResults,
"nbResults" -> p.nbResults,
"previousPage" -> p.previousPage,
"nextPage" -> p.nextPage,
"nbPages" -> p.nbPages
)
132 changes: 0 additions & 132 deletions model/src/main/scala/paginator.scala

This file was deleted.

49 changes: 0 additions & 49 deletions playJson/src/main/scala/Json.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import scala.util.NotGiven
import alleycats.Zero

import scalalib.newtypes.SameRuntime
import scalalib.model.*
import scalalib.time.toMillis

object Json:
Expand Down Expand Up @@ -80,51 +79,3 @@ object Json:
)

given Writes[Instant] = writeAs(_.toMillis)

// given Writes[chess.Color] = writeAs(_.name)
//
// given Reads[Uci] = Reads
// .of[String]
// .flatMapResult: str =>
// JsResult.fromTry(Uci(str).toTry(s"Invalid UCI: $str"))
// given Writes[Uci] = writeAs(_.uci)
//
// given Reads[LilaOpeningFamily] = Reads[LilaOpeningFamily]: f =>
// f.get[String]("key")
// .flatMap(LilaOpeningFamily.find)
// .fold[JsResult[LilaOpeningFamily]](JsError(Nil))(JsSuccess(_))
//
// given NoJsonHandler[chess.Square] with {}
//
// given OWrites[Crazyhouse.Pocket] = OWrites: p =>
// JsObject:
// p.flatMap((role, nb) => Option.when(nb > 0)(role.name -> JsNumber(nb)))
//
// given OWrites[chess.variant.Crazyhouse.Data] = OWrites: v =>
// PlayJson.obj("pockets" -> v.pockets.all)
//

given Writes[MaxPerPage] with
def writes(m: MaxPerPage) = JsNumber(m.value)

import scalalib.paginator.Paginator
given paginatorWrite[A: Writes]: OWrites[Paginator[A]] = OWrites[Paginator[A]]: p =>
PlayJson.obj(
"currentPage" -> p.currentPage,
"maxPerPage" -> p.maxPerPage,
"currentPageResults" -> p.currentPageResults,
"nbResults" -> p.nbResults,
"previousPage" -> p.previousPage,
"nextPage" -> p.nextPage,
"nbPages" -> p.nbPages
)

// import lila.core.LightUser
// given lightUserWrites: OWrites[LightUser] = OWrites(lightUser.write)
// object lightUser:
// def write(u: LightUser): JsObject = writeNoId(u) + ("id" -> JsString(u.id.value))
// def writeNoId(u: LightUser): JsObject = PlayJson
// .obj("name" -> u.name)
// .add("title", u.title)
// .add("flair", u.flair)
// .add("patron", u.isPatron)

0 comments on commit 3dc62a6

Please sign in to comment.