Skip to content

Instantly share code, notes, and snippets.

@ahjohannessen
Forked from MarkRBM/amm.sc
Created September 21, 2018 15:33
Show Gist options
  • Save ahjohannessen/06d6cc853752429b6de01a66714d8f1d to your computer and use it in GitHub Desktop.
Save ahjohannessen/06d6cc853752429b6de01a66714d8f1d to your computer and use it in GitHub Desktop.
Dynamic Transactors
interp.configureCompiler(_.settings.YpartialUnification.value = true)
import $ivy.`org.tpolecat::doobie-core:0.5.3`
import cats._
import cats.effect._
import cats.implicits._
import doobie._
import doobie.implicits._
case class Customer(id: String)
case class DBInfo(address: String, un: String, pw: String)
trait DBInfoProvider[F[_]] {
def getDBInfo(c: Customer): F[DBInfo]
}
case class User(name: String)
trait Users[F[_]] {
def getUsers(c: Customer): F[List[User]]
def saveUsers(newList: List[User])(c: Customer): F[Unit]
}
object TransactorProvider {
def getTransactorForDB(db: DBInfo): IO[Transactor[IO]] = ???
}
final class UsersDoobieImplementation(DBP: DBInfoProvider[IO]) extends Users[IO] {
import TransactorProvider._
def getUsers(c: Customer): IO[List[User]] = for {
dbInfo <- DBP.getDBInfo(c)
t <- getTransactorForDB(dbInfo)
users <- getAllUsers.transact(t)
} yield users
def saveUsers(newList: List[User])(c: Customer): IO[Unit] = for {
dbInfo <- DBP.getDBInfo(c)
t <- getTransactorForDB(dbInfo)
_ <- saveAllUsers(newList).transact(t)
} yield ()
private def getAllUsers: ConnectionIO[List[User]] = ???
private def saveAllUsers(nw: List[User]): ConnectionIO[Unit] = ???
}
final class SomeBusinessLogig[F[_]](U: Users[F])(implicit F: Monad[F]) {
def manipiulateUsersForCustomer(c: Customer): F[Unit] = for {
users <- U.getUsers(c)
manipulatedUsers = users.tail
_ <- U.saveUsers(manipulatedUsers)(c)
} yield ()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment