Skip to content

Commit 8a9a827

Browse files
authored
Update data type handling and field mappings in JPA queries (#108)
* Update data type handling and field mappings in JPA queries * Fix inconsistent transaction data and improve error handling * Update expected values in TransactionProviderJpaIT tests
1 parent 4ab2a6c commit 8a9a827

File tree

6 files changed

+13
-14
lines changed

6 files changed

+13
-14
lines changed

domain/src/main/java/com/jongsoft/finance/domain/transaction/Transaction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,14 @@ public Account computeFrom() {
269269
return transactions
270270
.first(FROM_PREDICATE)
271271
.map(Part::getAccount)
272-
.get();
272+
.getOrThrow(() -> new IllegalStateException("Transaction has no from account."));
273273
}
274274

275275
public Account computeTo() {
276276
return transactions
277277
.first(TO_PREDICATE)
278278
.map(Part::getAccount)
279-
.get();
279+
.getOrThrow(() -> new IllegalStateException("Transaction has no to account."));
280280
}
281281

282282
public Account computeCounter(Account account) {

jpa-repository/src/main/java/com/jongsoft/finance/jpa/rule/CreateRuleGroupHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public void handle(CreateRuleGroupCommand command) {
3232
var currentMax = entityManager.from(RuleGroupJpa.class)
3333
.fieldEq("user.username", authenticationFacade.authenticated())
3434
.fieldEq("archived", false)
35-
.projectSingleValue(Long.class, "max(sort)");
35+
.projectSingleValue(Integer.class, "max(sort)");
3636

3737
var jpaEntity = RuleGroupJpa.builder()
3838
.name(command.name())
3939
.user(entityManager.currentUser())
40-
.sort(currentMax.map(Integer.class::cast).getOrSupply(() -> 1))
40+
.sort(currentMax.getOrSupply(() -> 1))
4141
.build();
4242

4343
entityManager.persist(jpaEntity);

jpa-repository/src/main/java/com/jongsoft/finance/jpa/rule/TransactionRuleProviderJpa.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public void save(TransactionRule rule) {
8484
sortOrder = entityManager.from(RuleJpa.class)
8585
.fieldEq("user.username", authenticationFacade.authenticated())
8686
.fieldEq("group.name", rule.getGroup())
87-
.projectSingleValue(Long.class, "max(sort)")
88-
.map(Long::intValue)
87+
.projectSingleValue(Integer.class, "max(sort)")
8988
.getOrSupply(() -> 1);
9089
}
9190

@@ -161,8 +160,8 @@ private UserAccountJpa activeUser() {
161160

162161
private RuleGroupJpa group(String group) {
163162
return entityManager.from(RuleGroupJpa.class)
164-
.fieldEq("username", authenticationFacade.authenticated())
165-
.fieldEq("group", group)
163+
.fieldEq("user.username", authenticationFacade.authenticated())
164+
.fieldEq("name", group)
166165
.singleResult()
167166
.getOrSupply(() -> {
168167
EventBus.getBus().send(new CreateRuleGroupCommand(group));

jpa-repository/src/test/java/com/jongsoft/finance/jpa/transaction/TransactionProviderJpaIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ void lookup_currency() {
6565
.currency("EUR");
6666

6767
var check = transactionProvider.lookup(filter);
68-
Assertions.assertThat(check.content()).hasSize(2);
68+
Assertions.assertThat(check.content()).hasSize(3);
6969
}
7070

7171
@Test
7272
void daily() {
7373
var check = transactionProvider.daily(filterFactory.transaction().ownAccounts());
7474
Assertions.assertThat(check).hasSize(2);
7575
Assertions.assertThat(check).containsOnly(
76-
new DailySummaryImpl(LocalDate.of(2019, 1, 1), BigDecimal.valueOf(20.2D)),
76+
new DailySummaryImpl(LocalDate.of(2019, 1, 1), BigDecimal.valueOf(30.2D)),
7777
new DailySummaryImpl(LocalDate.of(2019, 1, 2), BigDecimal.valueOf(20.2D)));
7878
}
7979

@@ -82,15 +82,15 @@ void monthly() {
8282
var check = transactionProvider.monthly(filterFactory.transaction().ownAccounts());
8383
Assertions.assertThat(check).hasSize(1);
8484
Assertions.assertThat(check).containsOnly(
85-
new DailySummaryImpl(LocalDate.of(2019, 1, 1), BigDecimal.valueOf(40.4D)));
85+
new DailySummaryImpl(LocalDate.of(2019, 1, 1), BigDecimal.valueOf(50.4D)));
8686
}
8787

8888
@Test
8989
void balance() {
9090
var check = transactionProvider.balance(filterFactory.transaction().ownAccounts());
9191

9292
Assertions.assertThat(check.isPresent()).isTrue();
93-
Assertions.assertThat(check.get()).isEqualByComparingTo(BigDecimal.valueOf(40.4));
93+
Assertions.assertThat(check.get()).isEqualByComparingTo(BigDecimal.valueOf(50.4));
9494
}
9595

9696
@Test

jpa-repository/src/test/resources/sql/base-setup.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
insert into currency (id, name, symbol, code, decimal_places, enabled, archived)
3-
values (1, 'Euro', '?', 'EUR', default, default, default),
3+
values (1, 'Euro', '?', 'EUR', default, true, false),
44
(2, 'US Dollar', '$', 'USD', default, default, default),
55
(3, 'British Pound', '', 'GBP', default, default, default);
66

jpa-repository/src/test/resources/sql/transaction/transaction-provider.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ values (1, '2019-01-01', '2019-01-01', null, 1, 1, 20.2),
2727
(4, '2019-01-02', '2019-01-05', '2019-01-05', 2, 4, -20.2),
2828
(5, '2019-01-05', '2019-01-05', null, 2, 4, -10.2),
2929
(6, '2019-01-05', '2019-01-05', null, 2, 4, -10.0),
30-
(7, '2019-01-05', '2019-01-05', null, 3, 4, -10.2),
30+
(7, '2019-01-05', '2019-01-05', null, 3, 3, 10.0),
3131
(8, '2019-01-05', '2019-01-05', null, 3, 4, -10.0);
3232

3333
insert into transaction_tag (tag_id, transaction_id)

0 commit comments

Comments
 (0)