Created
February 19, 2020 14:39
-
-
Save choplin/151c32f2eb0c508d263c137f3d4e22fc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
case class User(id: Int, name: String, item: Seq[Item]) | |
case class Item(id: Int, name: String) | |
trait UserRepository[F[_]] { | |
def find(id: Int): OptionT[F, User] | |
def store(user: User): F[Unit] | |
} | |
trait ItemRepository[F[_]] { | |
def find(id: Int): OptionT[F, Item] | |
def store(item: Item): F[Unit] | |
} | |
trait UserService[F[_]] { | |
val ur = bind[UserRepository[F]] | |
val ir = bind[ItemRepository[F]] | |
def addItemToUser(userId: Int, itemId: Int): OptionT[F, Unit] = { | |
for { | |
user <- ur.find(userId) | |
item <- ir.find(itemId) | |
newUser = user.copy(item = user.item ++ Seq(item)) | |
_ <- ur.store(newUser) | |
} yield () | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment