Last active
April 16, 2019 09:16
-
-
Save AndreasKostler/191f5648826ac9e828b0061050f6a9dd to your computer and use it in GitHub Desktop.
Typed routing
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 play.sbt.routes.RoutesKeys | |
... | |
RoutesKeys.routesImport += "model.SellId" |
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
... | |
PUT /partner-experience/dealers/:sellId @controllers.DealerController.update(sellId: SellId) | |
PUT /partner-experience/dealers/:sellId/makes @controllers.DealerController.setMakes(sellId: SellId) | |
... |
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 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