Last active
December 17, 2015 02:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET /someUrl controllers.SomeController.get(startDate: org.joda.time.LocalDate ?= null, endDate: org.joda.time.LocalDate ?= null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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