Skip to content

Instantly share code, notes, and snippets.

@Algiras
Created February 10, 2020 12:48
Show Gist options
  • Save Algiras/3d4e0a06e7739c5d6c87f23757e0daf3 to your computer and use it in GitHub Desktop.
Save Algiras/3d4e0a06e7739c5d6c87f23757e0daf3 to your computer and use it in GitHub Desktop.
object UserStore {
def apply(user: UsernamePasswordCredentials, users: UsernamePasswordCredentials*): IO[UserStore] =
for {
userList <- (user +: users)
.map(u => UserStore.newUser(u.username, u.password))
.toList
.sequence
users <- Ref.of[IO, Map[UserId, User]](userList.map(u => u.id -> u).toMap)
} yield
new UserStore(
(id: UserId) => OptionT(users.get.map(_.get(id))),
usr => users.get.map(_.values.toList).flatMap(validateUser(usr)(_))
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment