blob: 2b4547b39f4ef2684b7cfb234d87ae17dbfcb9ca (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package xyz.driver.restquery.http.parsers
import xyz.driver.restquery.domain.Pagination
import scala.util._
object PaginationParser {
def parse(query: Seq[(String, String)]): Try[Pagination] = {
val IntString = """(\d+)""".r
def validate(field: String, default: Int) = query.collectFirst { case (`field`, size) => size } match {
case Some(IntString(x)) if x.toInt > 0 => x.toInt
case Some(IntString(x)) => throw new ParseQueryArgException((field, s"must greater than zero (found $x)"))
case Some(str) => throw new ParseQueryArgException((field, s"must be an integer (found $str)"))
case None => default
}
Try {
Pagination(validate("pageSize", Pagination.Default.pageSize),
validate("pageNumber", Pagination.Default.pageNumber))
}
}
}
|