From b6e14818057a8b9d3e7bddbe0433c37396f17451 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 26 Dec 2023 14:02:01 -0500 Subject: [PATCH] Added BalanceRecordRepository and associated logic. --- .../perfin/data/AccountEntryRepository.java | 9 +++++ .../perfin/data/BalanceRecordRepository.java | 7 ++++ .../andrewlalis/perfin/data/DataSource.java | 5 +++ .../impl/JdbcBalanceRecordRepository.java | 38 +++++++++++++++++++ .../perfin/data/impl/JdbcDataSource.java | 6 +++ 5 files changed, 65 insertions(+) create mode 100644 src/main/java/com/andrewlalis/perfin/data/AccountEntryRepository.java create mode 100644 src/main/java/com/andrewlalis/perfin/data/BalanceRecordRepository.java create mode 100644 src/main/java/com/andrewlalis/perfin/data/impl/JdbcBalanceRecordRepository.java diff --git a/src/main/java/com/andrewlalis/perfin/data/AccountEntryRepository.java b/src/main/java/com/andrewlalis/perfin/data/AccountEntryRepository.java new file mode 100644 index 0000000..18fc8d9 --- /dev/null +++ b/src/main/java/com/andrewlalis/perfin/data/AccountEntryRepository.java @@ -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 findAllByAccountId(long accountId); +} diff --git a/src/main/java/com/andrewlalis/perfin/data/BalanceRecordRepository.java b/src/main/java/com/andrewlalis/perfin/data/BalanceRecordRepository.java new file mode 100644 index 0000000..c825bb8 --- /dev/null +++ b/src/main/java/com/andrewlalis/perfin/data/BalanceRecordRepository.java @@ -0,0 +1,7 @@ +package com.andrewlalis.perfin.data; + +import com.andrewlalis.perfin.model.BalanceRecord; + +public interface BalanceRecordRepository extends AutoCloseable { + BalanceRecord findLatestByAccountId(long accountId); +} diff --git a/src/main/java/com/andrewlalis/perfin/data/DataSource.java b/src/main/java/com/andrewlalis/perfin/data/DataSource.java index 5e5c5a3..262c57f 100644 --- a/src/main/java/com/andrewlalis/perfin/data/DataSource.java +++ b/src/main/java/com/andrewlalis/perfin/data/DataSource.java @@ -5,4 +5,9 @@ public interface DataSource { default void useAccountRepository(ThrowableConsumer repoConsumer) { DbUtil.useClosable(this::getAccountRepository, repoConsumer); } + + BalanceRecordRepository getBalanceRecordRepository(); + default void useBalanceRecordRepository(ThrowableConsumer repoConsumer) { + DbUtil.useClosable(this::getBalanceRecordRepository, repoConsumer); + } } diff --git a/src/main/java/com/andrewlalis/perfin/data/impl/JdbcBalanceRecordRepository.java b/src/main/java/com/andrewlalis/perfin/data/impl/JdbcBalanceRecordRepository.java new file mode 100644 index 0000000..e0d35ea --- /dev/null +++ b/src/main/java/com/andrewlalis/perfin/data/impl/JdbcBalanceRecordRepository.java @@ -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")) + ); + } +} diff --git a/src/main/java/com/andrewlalis/perfin/data/impl/JdbcDataSource.java b/src/main/java/com/andrewlalis/perfin/data/impl/JdbcDataSource.java index 9202fba..dcbf499 100644 --- a/src/main/java/com/andrewlalis/perfin/data/impl/JdbcDataSource.java +++ b/src/main/java/com/andrewlalis/perfin/data/impl/JdbcDataSource.java @@ -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()); + } }