From 9db6f8f2c2527f64c5d55ac98882328ed18962b3 Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Wed, 11 Apr 2018 23:32:48 -0500 Subject: [PATCH] server: Add the PaginatedResult model --- .../app/com/xsn/explorer/models/base/Count.scala | 10 ++++++++++ .../explorer/models/base/PaginatedResult.scala | 15 +++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 server/app/com/xsn/explorer/models/base/Count.scala create mode 100644 server/app/com/xsn/explorer/models/base/PaginatedResult.scala diff --git a/server/app/com/xsn/explorer/models/base/Count.scala b/server/app/com/xsn/explorer/models/base/Count.scala new file mode 100644 index 0000000..06a3819 --- /dev/null +++ b/server/app/com/xsn/explorer/models/base/Count.scala @@ -0,0 +1,10 @@ +package com.xsn.explorer.models.base + +import play.api.libs.json.{JsNumber, Writes} + +case class Count(int: Int) extends AnyVal + +object Count { + + implicit val writes: Writes[Count] = Writes[Count] { count => JsNumber(count.int) } +} diff --git a/server/app/com/xsn/explorer/models/base/PaginatedResult.scala b/server/app/com/xsn/explorer/models/base/PaginatedResult.scala new file mode 100644 index 0000000..2a1f49d --- /dev/null +++ b/server/app/com/xsn/explorer/models/base/PaginatedResult.scala @@ -0,0 +1,15 @@ +package com.xsn.explorer.models.base + +import play.api.libs.json.{Json, OWrites, Writes} + +case class PaginatedResult[T](offset: Offset, limit: Limit, total: Count, data: List[T]) +object PaginatedResult { + implicit def writes[T](implicit writesT: Writes[T]): Writes[PaginatedResult[T]] = OWrites[PaginatedResult[T]] { result => + Json.obj( + "offset" -> result.offset, + "limit" -> result.limit, + "total" -> result.total, + "data" -> result.data + ) + } +}