diff options
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuicommon/parsers')
5 files changed, 64 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuicommon/parsers/DimensionsParser.scala b/src/main/scala/xyz/driver/pdsuicommon/parsers/DimensionsParser.scala index fb5a6b9..29f2363 100644 --- a/src/main/scala/xyz/driver/pdsuicommon/parsers/DimensionsParser.scala +++ b/src/main/scala/xyz/driver/pdsuicommon/parsers/DimensionsParser.scala @@ -8,6 +8,12 @@ class Dimensions(private val xs: Set[String] = Set.empty) { object DimensionsParser { + @deprecated("play-akka transition", "0") + def tryParse(query: Map[String, Seq[String]]): Try[Dimensions] = + tryParse(query.toSeq.flatMap{ case (key, values) => + values.map(value => key -> value) + }) + def tryParse(query: Seq[(String, String)]): Try[Dimensions] = { query.collect { case ("dimensions", value) => value } match { case Nil => Success(new Dimensions()) diff --git a/src/main/scala/xyz/driver/pdsuicommon/parsers/ListRequestParser.scala b/src/main/scala/xyz/driver/pdsuicommon/parsers/ListRequestParser.scala new file mode 100644 index 0000000..0356784 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuicommon/parsers/ListRequestParser.scala @@ -0,0 +1,20 @@ +package xyz.driver.pdsuicommon.parsers + +import xyz.driver.pdsuicommon.db.{Pagination, SearchFilterExpr, Sorting} +import play.api.mvc._ + +import scala.util.Try + +final case class ListRequestParameters(filter: SearchFilterExpr, sorting: Sorting, pagination: Pagination) + +class ListRequestParser(validSortingFields: Set[String]) { + + def tryParse(request: Request[AnyContent]): Try[ListRequestParameters] = { + for { + queryFilters <- SearchFilterParser.parse(request.queryString) + sorting <- SortingParser.parse(validSortingFields, request.queryString) + pagination <- PaginationParser.parse(request.queryString) + } yield ListRequestParameters(queryFilters, sorting, pagination) + } + +} diff --git a/src/main/scala/xyz/driver/pdsuicommon/parsers/PagiationParser.scala b/src/main/scala/xyz/driver/pdsuicommon/parsers/PagiationParser.scala new file mode 100644 index 0000000..3381542 --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuicommon/parsers/PagiationParser.scala @@ -0,0 +1,26 @@ +package xyz.driver.pdsuicommon.parsers + +import xyz.driver.pdsuicommon.db._ +import scala.util._ + +object PaginationParser { + + @deprecated("play-akka transition", "0") + def parse(query: Map[String, Seq[String]]): Try[Pagination] = + parse(query.toSeq.flatMap{ case (key, values) => + values.map(value => key -> value) + }) + + def parse(query: Seq[(String, String)]): Try[Pagination] = { + val IntString = """\d+""".r + def validate(field: String) = query.collectFirst{case (`field`, size) => size} match { + case Some(IntString(x)) => x.toInt + case Some(str) => throw new ParseQueryArgException((field, s"must be an integer (found $str)")) + case None => throw new ParseQueryArgException((field, "must be defined")) + } + + Try { + Pagination(validate("pageSize"), validate("pageNumber")) + } + } +} diff --git a/src/main/scala/xyz/driver/pdsuicommon/parsers/SearchFilterParser.scala b/src/main/scala/xyz/driver/pdsuicommon/parsers/SearchFilterParser.scala index 58b80ce..061f2ef 100644 --- a/src/main/scala/xyz/driver/pdsuicommon/parsers/SearchFilterParser.scala +++ b/src/main/scala/xyz/driver/pdsuicommon/parsers/SearchFilterParser.scala @@ -108,6 +108,12 @@ object SearchFilterParser { private val atomParser: Parser[SearchFilterExpr.Atom] = P(binaryAtomParser | nAryAtomParser) + @deprecated("play-akka transition", "0") + def parse(query: Map[String, Seq[String]]): Try[SearchFilterExpr] = + parse(query.toSeq.flatMap{ case (key, values) => + values.map(value => key -> value) + }) + def parse(query: Seq[(String, String)]): Try[SearchFilterExpr] = Try { query.toList.collect { case ("filters", value) => value } match { case Nil => SearchFilterExpr.Empty diff --git a/src/main/scala/xyz/driver/pdsuicommon/parsers/SortingParser.scala b/src/main/scala/xyz/driver/pdsuicommon/parsers/SortingParser.scala index 375dee2..cc6ade3 100644 --- a/src/main/scala/xyz/driver/pdsuicommon/parsers/SortingParser.scala +++ b/src/main/scala/xyz/driver/pdsuicommon/parsers/SortingParser.scala @@ -30,6 +30,12 @@ object SortingParser { } } + @deprecated("play-akka transition", "0") + def parse(validDimensions: Set[String], query: Map[String, Seq[String]]): Try[Sorting] = + parse(validDimensions, query.toSeq.flatMap{ case (key, values) => + values.map(value => key -> value) + }) + def parse(validDimensions: Set[String], query: Seq[(String, String)]): Try[Sorting] = Try { query.toList.collect { case ("sort", value) => value } match { case Nil => Sorting.Sequential(Seq.empty) |