-
-
Save ShaneDelmore/0484a81e93fdb7d8bddc3e54c377a4c3 to your computer and use it in GitHub Desktop.
This file contains 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
import java.time.{Duration, Instant} | |
import cats.Monad | |
import cats.implicits._ | |
case class TokenLifetime(createdAt: Instant, expiresAt: Instant) | |
case class Notification(message: String) | |
case class AuthToken(value: String, lifetime: TokenLifetime) | |
case class Credentials(username: String, password: String) | |
case class User(username: String) | |
trait UserRepos[F[_]] { | |
implicit def F: Monad[F] | |
def userRepo: UserRepo | |
trait UserRepo { | |
def followers(userId: Long): F[List[User]] | |
def validatedTokenLifetime: F[TokenLifetime] | |
def loginUser(creds: Credentials, lifetime: TokenLifetime): F[AuthToken] | |
def findUser(token: AuthToken): F[User] | |
def getUserNotifications(user: User): F[List[Notification]] | |
} | |
} | |
class UserRepos0 extends UserRepos[Option] { | |
override val F = implicitly[Monad[Option]] | |
override val userRepo: UserRepo = new UserRepo0 {} | |
trait UserRepo0 extends UserRepo { | |
def followers(userId: Long): Option[List[User]] = Option(Nil) | |
def validatedTokenLifetime: Option[TokenLifetime] = ??? | |
def loginUser(creds: Credentials, lifetime: TokenLifetime): Option[AuthToken] = ??? | |
def findUser(token: AuthToken): Option[User] = ??? | |
def getUserNotifications(user: User): Option[List[Notification]] = ??? | |
} | |
} | |
object UserActions { | |
val repo = new UserRepos0 | |
import repo.userRepo._ | |
def login(creds: Credentials, ttl: Duration) = | |
for { | |
loginTtl <- validatedTokenLifetime | |
token <- loginUser(creds, loginTtl) | |
user <- findUser(token) | |
notifications <- getUserNotifications(user) | |
} yield (token, notifications) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment