Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Last active August 29, 2015 14:17
Show Gist options
  • Save Jacoby6000/25316d3c660a51ca5317 to your computer and use it in GitHub Desktop.
Save Jacoby6000/25316d3c660a51ca5317 to your computer and use it in GitHub Desktop.
class QuerySettings(pageSize: Option[Int] = Some(25), pageNumber: Option[Long] = Some(0), sortBy: Option[String] = None, direction: Option[String] = None, allParams: Map[String, String]) {
sealed trait Direction
sealed object Asc extends Direction
sealed object Desc extends Direction
sealed object Invalid extends Direction
sealed trait Operator
sealed object Equal extends Operator
sealed object LessThan extends Operator
sealed object LessThanOrEqual extends Operator
sealed object GreaterThan extends Operator
sealed object GreaterThanOrEqual extends Operator
sealed object NotEqual extends Operator
case class Filter(column: String, operator: Operator)
val sortDirection = direction match {
case None => Desc
case Some("asc") => Asc
case Some("desc") => Desc
case _ => Invalid
}
require(sortDirection != Invalid, "Sort direction must be asc or desc.")
require(allParams.size < 100, "Why are you trying to give me so many parameters?")
val filters: Map[String, Filter] = allParams filter { x =>
x._1 != "pageSize" &&
x._1 != "pageNumber" &&
x._1 != "sortBy" &&
x._1 != "direction"} map { x =>
val column = x._1
val value = x._2
val parts = value.split(";~;") // Some Super uncommon string that hopefully people will never search for.
val filterValue = parts(0)
val filterOperator = if(parts.length >= 2) parts(1) else "eq"
(column, Filter(filterValue, filterOperator match {
case "eq" => Equal
case "lt" => LessThan
case "lte" => LessThanOrEqual
case "gt" => GreaterThan
case "gte" => GreaterThanOrEqual
case "ne" => NotEqual
}))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment