You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.6 KiB

package hs.fulda.de.ci.exam.project;
import java.util.Date;
public class AccountService {
private final AccountRepository accountRepository;
private final PasswordEncoder passwordEncoder;
public AccountService(AccountRepository accountRepository, PasswordEncoder passwordEncoder) {
this.accountRepository = accountRepository;
this.passwordEncoder = passwordEncoder;
}
public boolean isValidAccount(String id, String password){
Account account = accountRepository.findById(id);
return isEnabledAccount(account) && isValidPassword(account, password);
}
private boolean isEnabledAccount(Account account) {
return account!= null && account.isEnabled();
}
private boolean isValidPassword(Account account, String password) {
String encodedPassword = passwordEncoder.encode(password);
return encodedPassword.equals(account.getPasswordHash());
}
public void validateAccount(Account account){
account.validateAccountStatus();
account.validateId();
account.validatePassword();
}
public void checkIfAccountAlreadyExist(Account account){
if(accountRepository.checkIfAccountAlreadyExist(account)){
throw new RuntimeException("Account Already Exists");
}
}
public boolean createAccount(String id, String password, Account.AccountStatus accountStatus){
Account account = new Account(id, password, accountStatus);
validateAccount(account);
checkIfAccountAlreadyExist(account);
accountRepository.save(account);
return true;
}
}