content: package kr.megaptera.makaobank.services;
  
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.stereotype.Service;
  
  import kr.megaptera.makaobank.exceptions.LoginFailed;
  import kr.megaptera.makaobank.models.Account;
  import kr.megaptera.makaobank.models.AccountNumber;
  import kr.megaptera.makaobank.repositories.AccountRepository;
  
  @Service
  public class LoginService {
      private final AccountRepository accountRepository;
  
      private final PasswordEncoder passwordEncoder;
  
      public LoginService(AccountRepository accountRepository,
                          PasswordEncoder passwordEncoder) {
          this.accountRepository = accountRepository;
          this.passwordEncoder = passwordEncoder;
      }
  
      public Account login(AccountNumber accountNumber, String password) {
          Account account = accountRepository.findByAccountNumber(accountNumber)
                  .orElseThrow(() -> new LoginFailed());
  
          if (!account.authenticate(password, passwordEncoder)) {
              throw new LoginFailed();
          }
  
          return account;
      }
  }
  