Added BalanceRecordRepository and associated logic.

This commit is contained in:
Andrew Lalis 2023-12-26 14:02:01 -05:00
parent 2a47d93c97
commit b6e1481805
5 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package com.andrewlalis.perfin.data;
import com.andrewlalis.perfin.model.AccountEntry;
import java.util.List;
public interface AccountEntryRepository extends AutoCloseable {
List<AccountEntry> findAllByAccountId(long accountId);
}

View File

@ -0,0 +1,7 @@
package com.andrewlalis.perfin.data;
import com.andrewlalis.perfin.model.BalanceRecord;
public interface BalanceRecordRepository extends AutoCloseable {
BalanceRecord findLatestByAccountId(long accountId);
}

View File

@ -5,4 +5,9 @@ public interface DataSource {
default void useAccountRepository(ThrowableConsumer<AccountRepository> repoConsumer) {
DbUtil.useClosable(this::getAccountRepository, repoConsumer);
}
BalanceRecordRepository getBalanceRecordRepository();
default void useBalanceRecordRepository(ThrowableConsumer<BalanceRecordRepository> repoConsumer) {
DbUtil.useClosable(this::getBalanceRecordRepository, repoConsumer);
}
}

View File

@ -0,0 +1,38 @@
package com.andrewlalis.perfin.data.impl;
import com.andrewlalis.perfin.data.BalanceRecordRepository;
import com.andrewlalis.perfin.data.DbUtil;
import com.andrewlalis.perfin.model.BalanceRecord;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Currency;
import java.util.List;
public record JdbcBalanceRecordRepository(Connection conn) implements BalanceRecordRepository {
@Override
public BalanceRecord findLatestByAccountId(long accountId) {
return DbUtil.findOne(
conn,
"SELECT * FROM balance_record WHERE account_id = ? ORDER BY timestamp DESC LIMIT 1",
List.of(accountId),
JdbcBalanceRecordRepository::parse
).orElse(null);
}
@Override
public void close() throws Exception {
conn.close();
}
private static BalanceRecord parse(ResultSet rs) throws SQLException {
return new BalanceRecord(
rs.getLong("id"),
DbUtil.utcLDTFromTimestamp(rs.getTimestamp("timestamp")),
rs.getLong("account_id"),
rs.getBigDecimal("balance"),
Currency.getInstance(rs.getString("currency"))
);
}
}

View File

@ -1,6 +1,7 @@
package com.andrewlalis.perfin.data.impl;
import com.andrewlalis.perfin.data.AccountRepository;
import com.andrewlalis.perfin.data.BalanceRecordRepository;
import com.andrewlalis.perfin.data.DataSource;
import com.andrewlalis.perfin.data.UncheckedSqlException;
@ -31,4 +32,9 @@ public class JdbcDataSource implements DataSource {
public AccountRepository getAccountRepository() {
return new JdbcAccountRepository(getConnection());
}
@Override
public BalanceRecordRepository getBalanceRecordRepository() {
return new JdbcBalanceRecordRepository(getConnection());
}
}