Refactored account ordering according to latest account history updates.

This commit is contained in:
Andrew Lalis 2024-01-04 20:49:21 -05:00
parent e0e73cddae
commit ca85ab9893
4 changed files with 25 additions and 13 deletions

View File

@ -1,9 +1,8 @@
package com.andrewlalis.perfin.control;
import com.andrewlalis.javafx_scene_router.RouteSelectionListener;
import com.andrewlalis.perfin.data.pagination.PageRequest;
import com.andrewlalis.perfin.data.pagination.Sort;
import com.andrewlalis.perfin.data.util.CurrencyUtil;
import com.andrewlalis.perfin.model.Account;
import com.andrewlalis.perfin.model.MoneyValue;
import com.andrewlalis.perfin.model.Profile;
import com.andrewlalis.perfin.view.component.AccountTile;
@ -14,6 +13,8 @@ import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import java.util.List;
import static com.andrewlalis.perfin.PerfinApp.router;
public class AccountsViewController implements RouteSelectionListener {
@ -47,14 +48,14 @@ public class AccountsViewController implements RouteSelectionListener {
public void refreshAccounts() {
Profile.whenLoaded(profile -> {
Thread.ofVirtual().start(() -> {
profile.getDataSource().useAccountRepository(repo -> {
var page = repo.findAll(PageRequest.unpaged(Sort.asc("created_at")));
Platform.runLater(() -> {
accountsPane.getChildren().setAll(page.items().stream().map(AccountTile::new).toList());
});
});
});
Thread.ofVirtual().start(() -> profile.getDataSource().useAccountRepository(repo -> {
List<Account> accounts = repo.findAllOrderedByRecentHistory();
Platform.runLater(() -> accountsPane.getChildren()
.setAll(accounts.stream()
.map(AccountTile::new)
.toList()
));
}));
// Compute grand totals!
Thread.ofVirtual().start(() -> {
var totals = profile.getDataSource().getCombinedAccountBalances();

View File

@ -16,6 +16,7 @@ import java.util.Set;
public interface AccountRepository extends AutoCloseable {
long insert(AccountType type, String accountNumber, String name, Currency currency);
Page<Account> findAll(PageRequest pagination);
List<Account> findAllOrderedByRecentHistory();
List<Account> findAllByCurrency(Currency currency);
Optional<Account> findById(long id);
void updateName(long id, String name);

View File

@ -46,6 +46,19 @@ public record JdbcAccountRepository(Connection conn) implements AccountRepositor
return DbUtil.findAll(conn, "SELECT * FROM account WHERE NOT archived", pagination, JdbcAccountRepository::parseAccount);
}
@Override
public List<Account> findAllOrderedByRecentHistory() {
return DbUtil.findAll(
conn,
"""
SELECT DISTINCT ON (account.id) account.*, ahi.timestamp AS _
FROM account
LEFT OUTER JOIN account_history_item ahi ON ahi.account_id = account.id
ORDER BY ahi.timestamp DESC, account.created_at DESC""",
JdbcAccountRepository::parseAccount
);
}
@Override
public List<Account> findAllByCurrency(Currency currency) {
return DbUtil.findAll(

View File

@ -38,9 +38,6 @@ public class AccountTile extends BorderPane {
-fx-padding: 5px;
-fx-cursor: hand;
""");
final Color color = ACCOUNT_TYPE_COLORS.get(account.getType());
// var fill = new BackgroundFill(color, new CornerRadii(3.0), null);
// setBackground(new Background(fill));
setTop(getHeader(account));
setBottom(getFooter(account));