Skip to content

Commit

Permalink
fixed issue [#14]
Browse files Browse the repository at this point in the history
  • Loading branch information
pitzzahh committed Aug 12, 2022
1 parent 179bb90 commit 9413d68
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main/java/io/github/pitzzahh/atm/dao/AtmDAOImplementation.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.pitzzahh.atm.dao;

import java.sql.SQLException;
import java.util.*;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.function.Function;
Expand Down Expand Up @@ -69,14 +69,20 @@ public Supplier<Map<String, Client>> getAllClients() {
* <p>T - a {@code String} containing the account number to be search from the database.</p>
* <p>R - a {@code Optional<Client>} containing the result if the client is found or not.</p>
* @return a {@code Optional<Client>} object.
* @throws IllegalArgumentException if the account number does not belong to any client.
* @see Function
* @see Optional
* @see Client
*/
@Override
public Function<String, Optional<Client>> getClientByAccountNumber() {
public Function<String, Client> getClientByAccountNumber() throws IllegalArgumentException {
final var QUERY = "SELECT * FROM clients WHERE account_number = ?";
return an -> Optional.ofNullable(db.queryForObject(QUERY, new ClientMapper(), SecurityUtil.encrypt(an)));
return an -> {
try {
return db.queryForObject(QUERY, new ClientMapper(), SecurityUtil.encrypt(an));
} catch (RuntimeException ignored) {
throw new IllegalArgumentException(String.format("CLIENT WITH ACCOUNT NUMBER: %s DOES NOT EXIST", an));
}
};
}

/**
Expand Down Expand Up @@ -299,15 +305,16 @@ public Function<String, Integer> getLoanCount() {
* Function that approves a loan request.
* The function takes a {@code Loan} object containing the loan information to be approved.
* @return a {@code Status} of the query wether {@link Status#SUCCESS} or {@link Status#ERROR}.
* @throws IllegalArgumentException if the account number does not belong to any client, thus the message cannot be found.
* @see BiFunction
* @see Loan
* @see Status
*/
@Override
public BiFunction<Loan, Client, Status> approveLoan() {
public BiFunction<Loan, Client, Status> approveLoan() throws IllegalArgumentException {
final var QUERY = "UPDATE loans SET pending = ? WHERE loan_number = ? AND account_number = ?";
return (loan, c) -> {
var client = getClientByAccountNumber().apply(c.accountNumber()).get();
var client = getClientByAccountNumber().apply(c.accountNumber());
var status = updateClientSavingsByAccountNumber().apply(client.accountNumber(), client.savings() + loan.amount());
return status == SUCCESS ? db.update(
QUERY,
Expand Down

0 comments on commit 9413d68

Please sign in to comment.