Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Server extends IOApp {
- type F[A] = IO[A]
- override def run(args: List[String]): IO[ExitCode] =
- for {
- _ <- {
- val app = {
- val xa = Transactor.fromDriverManager[F](
- "org.postgresql.Driver", "jdbc:postgresql://localhost:5432/postgres", "postgres", "")
- val betRepo = new AsyncBetRepository[F](xa)
- //...
- }
- BlazeServerBuilder[F]
- .bindHttp(8080, "localhost")
- .withHttpApp(app)
- .serve
- .compile
- .drain
- }
- } yield ExitCode.Error
- }
- class AsyncBetRepository[F[_] : Async ](val xa: Transactor[F]) extends BetRepository[F] {
- import BetSQL._
- override def create(bet: Bet): F[Bet] =
- insert(bet).withUniqueGeneratedKeys[UUID]("id").map(betId => bet.copy(betId = betId.some)).transact(xa)
- }
- private object BetSQL {
- def insert(bet: Bet): Update0 =
- sql"""
- INSERT INTO bet (home, away, stake, betType) VALUES (${bet.home}, ${bet.away}, ${bet.stake}, ${bet.betType})
- """.update
- }
- case class Bet(
- betId: Option[UUID],
- home: String,
- away: String,
- stake: BigDecimal,
- betType: Int)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement