Skip to content

Instantly share code, notes, and snippets.

@bouil
Last active December 17, 2015 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bouil/5535993 to your computer and use it in GitHub Desktop.
Save bouil/5535993 to your computer and use it in GitHub Desktop.
QueryStringBindable #playframewok #scala Require a "play clean" to import the object in routes. Check target/scala-2.10/src_managed/main/routes_routing.scala to see if the import is present.
import sbt._
import Keys._
import Path._
import play.Project._
object ApplicationBuild extends Build {
val appName = "scala-test"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
jdbc,
"mysql" % "mysql-connector-java" % "5.1.18",
"com.typesafe.slick" % "slick_2.10" % "1.0.0",
"joda-time" % "joda-time" % "2.0"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
resolvers += "Local Maven Repository" at "file://" + Path.userHome.absolutePath + "/.m2/repository",
routesImport += "services.QueryBinders.localDateBinder"
)
}
GET /someUrl controllers.SomeController.get(startDate: org.joda.time.LocalDate ?= null, endDate: org.joda.time.LocalDate ?= null)
package services
import org.joda.time.LocalDate
import play.api.mvc.QueryStringBindable
import org.joda.time.format.{DateTimeFormat, DateTimeFormatter}
object QueryBinders extends {
implicit def localDateBinder: QueryStringBindable[LocalDate] = new QueryStringBindable[LocalDate]{
val dateTimeFormatter: DateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, LocalDate]] = {
val dateString: Option[Seq[String]] = params.get(key)
try {
Some(Right(dateTimeFormatter.parseLocalDate(dateString.get.head)))
} catch {
case e: IllegalArgumentException => Option(Left(dateString.get.head))
}
}
def unbind(key: String, value: LocalDate): String = {
dateTimeFormatter.print(value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment