Skip to content

Instantly share code, notes, and snippets.

@gbougeard
Created October 3, 2013 11:56
Show Gist options
  • Select an option

  • Save gbougeard/6808674 to your computer and use it in GitHub Desktop.

Select an option

Save gbougeard/6808674 to your computer and use it in GitHub Desktop.
case class Page[A](items: Seq[A], page: Int, offset: Long, total: Long) {
lazy val prev = Option(page - 1).filter(_ >= 0)
lazy val next = Option(page + 1).filter(_ => (offset + items.size) < total)
}
object Page {
implicit val writes: Writes[Page[A]] = (
(__ \ 'items).write[Seq[A]] ~
(__ \ 'page).write[Int] ~
(__ \ 'offset).write[Long] ~
(__ \ 'total).write[Long]
) (unlift(Page.unapply))
}
@mandubian

Copy link
Copy Markdown

Try

case class Page[A](items: Seq[A], page: Int, offset: Long, total: Long)
object Page {
  implicit def writes[A : Writes]: Writes[Page[A]] = (
    (__ \ 'items).write[Seq[A]] ~
    (__ \ 'page).write[Int] ~
    (__ \ 'offset).write[Long] ~
    (__ \ 'total).write[Long]
  ) (unlift(Page.unapply[A]))

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment