Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.01 KB | None | 0 0
  1. case class UserInfo(account: String, password: String)
  2.  
  3. trait UserService {
  4.     def getUserInfo(account: String): UserInfo
  5. }
  6.  
  7. class UserServiceComponent extends UserService {
  8.   def getUserInfo(account: String): UserInfo = UserInfo(account, "123")
  9. }
  10.  
  11. case class LoginEnv(userName: String, password: String, service: UserService)
  12.  
  13. object LoginFacade {
  14.   val login = Reader[LoginEnv, Boolean] { loginEnvironment =>
  15.       loginEnvironment.service.getUserInfo(loginEnvironment.userName).password == loginEnvironment.password
  16.   }
  17. }
  18.  
  19. object HomeController {
  20.   val autenticarUsuario = LoginFacade.login.tapWith { (loginEnv, isAuthorized) =>
  21.     if (isAuthorized) {
  22.       s"Olá, ${loginEnv.userName}"
  23.     } else {
  24.       "Erro de login"
  25.     }
  26.   }
  27. }
  28.  
  29. object ReaderMonadInjection extends App {
  30.   val userService = new UserServiceComponent()
  31.   val loginEnvironment = new LoginEnv("Rodrigo", "123", userService)
  32.  
  33.   val loginMessage = HomeController.autenticarUsuario.run(loginEnvironment)
  34.  
  35.   println(loginMessage)
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement