blob: 6d8aaf809a4847cbfb988c522ca443102b8434f5 (
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
24
|
package xyz.driver.restquery.rest.parsers
import xyz.driver.restquery.query.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))
}
}
}
|