Skip to content

Instantly share code, notes, and snippets.

softberries

Block or report user

Report or block softberries

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View play-fp2-5.scala
private lazy val personService: PersonService[IO] = wire[PersonService[IO]]
private lazy val messagesActionBuilder = new DefaultMessagesActionBuilderImpl(playBodyParsers.defaultBodyParser, messagesApi)
private lazy val messagesControllerComponents = DefaultMessagesControllerComponents(
messagesActionBuilder,
defaultActionBuilder,
playBodyParsers,
messagesApi,
langs,
fileMimeTypes,
ec)
View play-fp2-4.scala
class AppLoader extends ApplicationLoader {
def load(context: Context) = {
val appComponents = new AppComponents(context)
appComponents.applicationEvolutions //run the evolutions
appComponents.application
}
}
View play-fp2-3.scala
override def get(name: String): F[Option[Person]] = {
RepoUtil.fromFuture(db.run {
people.filter(_.name === name).result.headOption
})
}
override def create(name: String, age: Int): F[Person] = {
RepoUtil.fromFuture(db.run {
(people.map(p => (p.name, p.age))
returning people.map(_.id)
View play-fp-12.scala
object RepoUtil {
def fromFuture[IO[A], A](f: => Future[A])(implicit ec: ExecutionContext): cats.effect.IO[A] =
IO.delay(f) >>= (
f => IO.async[A] { cb =>
f.onComplete {
case Success(a) => cb(Right(a))
case Failure(ex) => cb(Left(ex))
}
})
}
View play-fp2-2.scala
package services
import cats.MonadError
import models.Person
import repositories.PersonRepository
import cats.implicits._
class PersonService[F[_]](personRepository: PersonRepository[F])(
implicit monadError: MonadError[F, String]
) {
View play-fp2-1.scala
package repositories
import models.Person
trait PersonRepository[F[_]] {
def get(name: String): F[Option[Person]]
def create(name: String, age: Int): F[Person]
def list(): F[Seq[Person]]
}
View play-fp-11.scala
IO.pure(Ok(views.html.index(errorForm)))
View play-fp-10.scala
def getPersons = Action.asyncF { implicit request =>
repo.list().map { people =>
Ok(Json.toJson(people))
}
}
View play-fp-9.scala
object IOHttp {
implicit class ActionBuilderOps[+R[_], B](ab: ActionBuilder[R, B]) {
import cats.effect.implicits._
def asyncF[F[_] : Effect](cb: R[B] => F[Result]): Action[B] = ab.async { c =>
cb(c).toIO.unsafeToFuture()
}
}
View play-fp-8.scala
def getPersons = Action.async { implicit request =>
repo.list().map { people =>
Ok(Json.toJson(people))
}.unsafeToFuture()
}
You can’t perform that action at this time.