Added BalanceRecordRepository and associated logic.
This commit is contained in:
parent
2a47d93c97
commit
b6e1481805
|
@ -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);
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.andrewlalis.perfin.data;
|
||||||
|
|
||||||
|
import com.andrewlalis.perfin.model.BalanceRecord;
|
||||||
|
|
||||||
|
public interface BalanceRecordRepository extends AutoCloseable {
|
||||||
|
BalanceRecord findLatestByAccountId(long accountId);
|
||||||
|
}
|
|
@ -5,4 +5,9 @@ public interface DataSource {
|
||||||
default void useAccountRepository(ThrowableConsumer<AccountRepository> repoConsumer) {
|
default void useAccountRepository(ThrowableConsumer<AccountRepository> repoConsumer) {
|
||||||
DbUtil.useClosable(this::getAccountRepository, repoConsumer);
|
DbUtil.useClosable(this::getAccountRepository, repoConsumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BalanceRecordRepository getBalanceRecordRepository();
|
||||||
|
default void useBalanceRecordRepository(ThrowableConsumer<BalanceRecordRepository> repoConsumer) {
|
||||||
|
DbUtil.useClosable(this::getBalanceRecordRepository, repoConsumer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package com.andrewlalis.perfin.data.impl;
|
package com.andrewlalis.perfin.data.impl;
|
||||||
|
|
||||||
import com.andrewlalis.perfin.data.AccountRepository;
|
import com.andrewlalis.perfin.data.AccountRepository;
|
||||||
|
import com.andrewlalis.perfin.data.BalanceRecordRepository;
|
||||||
import com.andrewlalis.perfin.data.DataSource;
|
import com.andrewlalis.perfin.data.DataSource;
|
||||||
import com.andrewlalis.perfin.data.UncheckedSqlException;
|
import com.andrewlalis.perfin.data.UncheckedSqlException;
|
||||||
|
|
||||||
|
@ -31,4 +32,9 @@ public class JdbcDataSource implements DataSource {
|
||||||
public AccountRepository getAccountRepository() {
|
public AccountRepository getAccountRepository() {
|
||||||
return new JdbcAccountRepository(getConnection());
|
return new JdbcAccountRepository(getConnection());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BalanceRecordRepository getBalanceRecordRepository() {
|
||||||
|
return new JdbcBalanceRecordRepository(getConnection());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue