Scala-Play-Slick-Demo/app/controllers/MonixHomeController.scala
Rohan Sircar cb9524eac7 Many changes
Rewrote slick db service using monix task instead of future
Added cats IO to controller to make use of above db service
Added action methods that support effect wrappers other than future
Added play-flyway to auto-manage migrations
Moved migration files to appropriate directory as a result of above
Added cors filter to work with separately hosted frontend
Made logger log at debug
2020-08-22 16:18:11 +05:30

35 lines
966 B
Scala

package controllers
import javax.inject.{Inject, Singleton}
import play.api.mvc._
import play.api.libs.json.Json
import com.example.services.TaskLibraryService
import util.IOHttp._
import com.typesafe.scalalogging.LazyLogging
import cats.effect.IO
import scala.concurrent.ExecutionContext
import util.Schedulers
@Singleton
class MonixHomeController @Inject() (
taskLibraryService: TaskLibraryService,
cc: ControllerComponents
)(schedulers: Schedulers, ec: ExecutionContext)
extends AbstractController(cc)
with LazyLogging {
private implicit val defaultScheduler = schedulers.dbScheduler
def authors(bookId: Long) = Action.asyncF {
for {
_ <- IO(logger.debug("Getting Authors"))
authors <- taskLibraryService.getAuthorsForBook(bookId).to[IO]
_ <- IO(logger.debug("Where am I now?"))
_ <- IO.shift(ec)
_ <- IO(logger.info("Got Authors"))
res <- IO.pure(Ok(Json.toJson(authors)))
} yield (res)
}
}