Skip to content

Instantly share code, notes, and snippets.

@AndreasKostler
Last active April 16, 2019 09:16
Show Gist options
  • Save AndreasKostler/191f5648826ac9e828b0061050f6a9dd to your computer and use it in GitHub Desktop.
Save AndreasKostler/191f5648826ac9e828b0061050f6a9dd to your computer and use it in GitHub Desktop.
Typed routing
import play.sbt.routes.RoutesKeys
...
RoutesKeys.routesImport += "model.SellId"
...
PUT /partner-experience/dealers/:sellId @controllers.DealerController.update(sellId: SellId)
PUT /partner-experience/dealers/:sellId/makes @controllers.DealerController.setMakes(sellId: SellId)
...
package model
import algebra.DeleteDealerErrors.SellIdError
import cats.MonadError
import play.api.libs.json._
import play.api.mvc.PathBindable
import scala.util.Try
final case class SellId(value: String)
object SellId {
def fromString(s: String): Option[SellId] = Try(s.toLong).toOption.map(_ => SellId(s))
...
implicit def pathBindable[T](implicit pb: PathBindable[String]) = new PathBindable[SellId] {
override def bind(key: String, value: String): Either[String, SellId] = {
val err = Left("Unable to bind a seller Id")
pb.bind(key, value) match {
case Right(id) => fromString(id).map(Right(_)).getOrElse(err)
case _ => err
}
}
override def unbind(key: String, id: SellId): String = id.value.toString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment