Skip to content

Commit 02f8471

Browse files
FIX 2 : handling commands, type switch removal (#1064)
1 parent bbd99d2 commit 02f8471

File tree

13 files changed

+108
-61
lines changed

13 files changed

+108
-61
lines changed

Oracle_CQRS/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.4.5</version>
8+
<version>3.4.6</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<artifactId>Oracle_CQRS</artifactId>
@@ -14,8 +14,8 @@
1414
<java.version>21</java.version>
1515
<lombok.version>1.18.38</lombok.version>
1616
<springdoc-openapi.version>2.8.6</springdoc-openapi.version>
17-
<oracle-spring-boot-starter-ucp.version>25.1.0</oracle-spring-boot-starter-ucp.version>
18-
<oracle-spring-boot-starter-aqjms.version>25.1.0</oracle-spring-boot-starter-aqjms.version>
17+
<oracle-spring-boot-starter-ucp.version>25.2.0</oracle-spring-boot-starter-ucp.version>
18+
<oracle-spring-boot-starter-aqjms.version>25.2.0</oracle-spring-boot-starter-aqjms.version>
1919
</properties>
2020

2121
<dependencies>
Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.example.oracle.cqrs.command.aggregates;
22

33
import jakarta.transaction.Transactional;
4-
import lombok.*;
54

5+
import lombok.Getter;
66
import org.example.oracle.cqrs.command.commands.*;
77
import org.example.oracle.cqrs.common.events.*;
88
import org.example.oracle.cqrs.command.producers.EventProducer;
@@ -37,45 +37,8 @@ public AccountAggregate(EventProducer eventProducer, EventStoreRepository eventS
3737

3838
@JmsListener(destination = "${txeventq.queue.commands.name}", id = "sampleCommand")
3939
void handleCommand(BaseCommand command) {
40-
41-
BaseEvent event;
42-
43-
switch (command) {
44-
case CreateAccountCommand createAccountCommand -> {
45-
System.out.println("Handling create: " + command);
46-
if (createAccountCommand.getInitialBalance() < 0)
47-
throw new IllegalArgumentException("Initial balance is negative");
48-
49-
event = new AccountCreatedEvent(UUID.randomUUID().toString(), createAccountCommand.getInitialBalance(), createAccountCommand.getCurrency(), AccountStatus.CREATED, createAccountCommand.getAccountId());
50-
51-
}
52-
case DebitAccountCommand debitAccountCommand -> {
53-
System.out.println("Handling debit: " + command);
54-
if (debitAccountCommand.getAmount() < 0) throw new IllegalArgumentException("Amount is negative");
55-
56-
event = new AccountDebitedEvent(UUID.randomUUID().toString(), debitAccountCommand.getAccountId(), debitAccountCommand.getCurrency(), debitAccountCommand.getAmount());
57-
58-
59-
}
60-
case CreditAccountCommand creditAccountCommand -> {
61-
System.out.println("Handling debit: " + command);
62-
if (creditAccountCommand.getAmount() < 0) throw new IllegalArgumentException("Amount is negative");
63-
64-
event = new AccountCreditedEvent(UUID.randomUUID().toString(), creditAccountCommand.getCurrency(), creditAccountCommand.getAmount(), creditAccountCommand.getAccountId());
65-
66-
67-
}
68-
case UpdateAccountStatusCommand updateAccountStatusCommand -> {
69-
System.out.println("Handling debit: " + command);
70-
event = new AccountStatusUpdatedEvent(UUID.randomUUID().toString(), updateAccountStatusCommand.getAccountStatus(), updateAccountStatusCommand.getAccountId());
71-
72-
}
73-
74-
default -> throw new IllegalStateException("Unexpected value: " + command);
75-
}
76-
40+
BaseEvent event = command.createEvent();
7741
eventProducer.enqueue(event);
7842
eventStoreRepository.save(event);
79-
8043
}
8144
}

Oracle_CQRS/src/main/java/org/example/oracle/cqrs/command/commands/BaseCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import lombok.AllArgsConstructor;
44
import lombok.Getter;
55
import lombok.NoArgsConstructor;
6+
import org.example.oracle.cqrs.common.events.BaseEvent;
67

78

89
@AllArgsConstructor @NoArgsConstructor
9-
public class BaseCommand<T>{
10+
public abstract class BaseCommand<T>{
1011
@Getter
1112
private T id;
13+
public abstract BaseEvent createEvent();
1214

1315
}

Oracle_CQRS/src/main/java/org/example/oracle/cqrs/command/commands/CreateAccountCommand.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package org.example.oracle.cqrs.command.commands;
22

3-
import lombok.*;
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
import org.example.oracle.cqrs.common.enums.AccountStatus;
6+
import org.example.oracle.cqrs.common.events.AccountCreatedEvent;
7+
import org.example.oracle.cqrs.common.events.BaseEvent;
48

5-
@Getter @NoArgsConstructor
9+
import java.util.UUID;
10+
11+
@Getter
12+
@NoArgsConstructor
613
public class CreateAccountCommand extends BaseCommand<String> {
714
private double initialBalance;
815
private String currency;
@@ -14,4 +21,13 @@ public CreateAccountCommand(String id, double initialBalance, String currency, S
1421
this.currency = currency;
1522
this.accountId = accountId;
1623
}
24+
25+
@Override
26+
public BaseEvent createEvent() {
27+
System.out.println("Handling create: " + this);
28+
if (this.getInitialBalance() < 0)
29+
throw new IllegalArgumentException("Initial balance is negative");
30+
31+
return new AccountCreatedEvent(UUID.randomUUID().toString(), initialBalance, currency, AccountStatus.CREATED, accountId);
32+
}
1733
}

Oracle_CQRS/src/main/java/org/example/oracle/cqrs/command/commands/CreditAccountCommand.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
import lombok.Getter;
55
import lombok.NoArgsConstructor;
6+
import org.example.oracle.cqrs.common.events.AccountCreditedEvent;
7+
import org.example.oracle.cqrs.common.events.BaseEvent;
8+
9+
import java.util.UUID;
610

711
@Getter
812
@NoArgsConstructor
@@ -17,4 +21,13 @@ public CreditAccountCommand(String id, String accountId, double amount, String c
1721
this.amount = amount;
1822
this.currency = currency;
1923
}
24+
25+
@Override
26+
public BaseEvent createEvent() {
27+
System.out.println("Handling debit: " + this);
28+
if (amount < 0) throw new IllegalArgumentException("Amount is negative");
29+
30+
return new AccountCreditedEvent(UUID.randomUUID().toString(), currency, amount, accountId);
31+
32+
}
2033
}

Oracle_CQRS/src/main/java/org/example/oracle/cqrs/command/commands/DebitAccountCommand.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package org.example.oracle.cqrs.command.commands;
22

3-
import lombok.*;
3+
import lombok.Getter;
4+
import lombok.NoArgsConstructor;
5+
import org.example.oracle.cqrs.common.events.AccountDebitedEvent;
6+
import org.example.oracle.cqrs.common.events.BaseEvent;
7+
8+
import java.util.UUID;
49

510
@Getter
611
@NoArgsConstructor
@@ -16,4 +21,11 @@ public DebitAccountCommand(String id, String accountId, double amount, String cu
1621
this.currency = currency;
1722
}
1823

24+
@Override
25+
public BaseEvent createEvent() {
26+
System.out.println("Handling debit: " + this);
27+
if (amount < 0) throw new IllegalArgumentException("Amount is negative");
28+
return new AccountDebitedEvent(UUID.randomUUID().toString(), accountId, currency, amount);
29+
30+
}
1931
}

Oracle_CQRS/src/main/java/org/example/oracle/cqrs/command/commands/UpdateAccountStatusCommand.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import lombok.Getter;
44
import lombok.NoArgsConstructor;
55
import org.example.oracle.cqrs.common.enums.AccountStatus;
6+
import org.example.oracle.cqrs.common.events.AccountStatusUpdatedEvent;
7+
import org.example.oracle.cqrs.common.events.BaseEvent;
8+
9+
import java.util.UUID;
610

711
@Getter
812
@NoArgsConstructor
@@ -15,4 +19,10 @@ public UpdateAccountStatusCommand(String id, String accountId, AccountStatus acc
1519
this.accountId = accountId;
1620
this.accountStatus = accountStatus;
1721
}
22+
23+
@Override
24+
public BaseEvent createEvent() {
25+
System.out.println("Handling update account : " + this);
26+
return new AccountStatusUpdatedEvent(UUID.randomUUID().toString(), accountStatus, accountId);
27+
}
1828
}

Oracle_CQRS/src/main/java/org/example/oracle/cqrs/command/controlles/AccountCommandRest.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@
1313
import org.example.oracle.cqrs.common.Dtos.DebitAccountDTO;
1414
import org.example.oracle.cqrs.common.Dtos.UpdateAccountStatusDTO;
1515
import org.example.oracle.cqrs.common.events.BaseEvent;
16-
import org.springframework.http.HttpStatus;
16+
import org.springframework.beans.factory.annotation.Value;
1717
import org.springframework.http.ResponseEntity;
18-
import org.springframework.web.bind.annotation.*;
18+
import org.springframework.web.bind.annotation.ExceptionHandler;
19+
import org.springframework.web.bind.annotation.PutMapping;
20+
import org.springframework.web.bind.annotation.RequestBody;
21+
import org.springframework.web.bind.annotation.PostMapping;
22+
import org.springframework.web.bind.annotation.RequestMapping;
23+
import org.springframework.web.bind.annotation.RestController;
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.util.UriComponentsBuilder;
1926

27+
28+
import java.net.URI;
2029
import java.util.List;
2130
import java.util.UUID;
2231

@@ -26,38 +35,41 @@ public class AccountCommandRest {
2635

2736
private CommandsProducer commandsProducer;
2837
private EventStoreRepository eventStoreRepository;
38+
private String queryBsedUrl ;
2939

30-
AccountCommandRest(CommandsProducer commandsProducer, EventStoreRepository eventStoreRepository) {
40+
AccountCommandRest(CommandsProducer commandsProducer, EventStoreRepository eventStoreRepository, @Value("${query.base.url}") String queryBsedUrl) {
3141
this.commandsProducer = commandsProducer;
3242
this.eventStoreRepository = eventStoreRepository;
43+
this.queryBsedUrl = queryBsedUrl;
3344
}
3445

3546
@PostMapping("/create")
3647
public ResponseEntity createAccount(@Valid @RequestBody CreateAccountDTO request) {
3748
String accountId = UUID.randomUUID().toString();
3849
commandsProducer.enqueue(new CreateAccountCommand(UUID.randomUUID().toString(), request.getInitialBalance(), request.getCurrency(), accountId));
39-
40-
return ResponseEntity.status(HttpStatus.ACCEPTED).header("Location", "/api/queries/status/" + accountId).build();
50+
URI location = getGetQueryUri(accountId);
51+
return ResponseEntity.created(location).build();
4152
}
4253

4354
@PostMapping("/debit")
4455
public ResponseEntity debitAccount(@Valid @RequestBody DebitAccountDTO request) {
4556
commandsProducer.enqueue(new DebitAccountCommand(UUID.randomUUID().toString(), request.getAccountId(), request.getAmount(), request.getCurrency()));
46-
47-
return ResponseEntity.status(HttpStatus.ACCEPTED).header("Location", "/api/queries/" + request.getAccountId()).build();
57+
URI location = getGetQueryUri(request.getAccountId());
58+
return ResponseEntity.created(location).build();
4859
}
4960

5061
@PostMapping("/credit")
5162
public ResponseEntity creditAccount(@Valid @RequestBody CreditAccountDTO request) {
5263
commandsProducer.enqueue(new CreditAccountCommand(UUID.randomUUID().toString(), request.getAccountId(), request.getAmount(), request.getCurrency()));
53-
54-
return ResponseEntity.status(HttpStatus.ACCEPTED).header("Location", "/api/queries/" + request.getAccountId()).build();
64+
URI location = getGetQueryUri(request.getAccountId());
65+
return ResponseEntity.created(location).build();
5566
}
5667

5768
@PutMapping("/updateStatus")
5869
public ResponseEntity updateStatus(@Valid @RequestBody UpdateAccountStatusDTO request) {
5970
commandsProducer.enqueue(new UpdateAccountStatusCommand(UUID.randomUUID().toString(), request.getAccountId(), request.getAccountStatus()));
60-
return ResponseEntity.status(HttpStatus.ACCEPTED).header("Location", "/api/queries/status/" + request.getAccountId()).build();
71+
URI location = getGetQueryUri(request.getAccountId());
72+
return ResponseEntity.created(location).build();
6173
}
6274

6375

@@ -71,5 +83,13 @@ public String exceptionHandler(Exception exception) {
7183
return exception.getMessage();
7284
}
7385

86+
private URI getGetQueryUri(String accountId) {
87+
URI location = UriComponentsBuilder.fromHttpUrl(queryBsedUrl)
88+
.path("/" + accountId)
89+
.build()
90+
.toUri();
91+
return location;
92+
}
93+
7494

7595
}

Oracle_CQRS/src/main/java/org/example/oracle/cqrs/common/Dtos/CreateAccountDTO.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import jakarta.validation.constraints.Min;
44
import jakarta.validation.constraints.NotBlank;
5-
import lombok.*;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
import lombok.Setter;
69

710
@Getter
811
@Setter

Oracle_CQRS/src/main/java/org/example/oracle/cqrs/common/Dtos/UpdateAccountStatusDTO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package org.example.oracle.cqrs.common.Dtos;
33

44
import jakarta.validation.constraints.NotBlank;
5+
import jakarta.validation.constraints.NotNull;
56
import lombok.AllArgsConstructor;
67
import lombok.Data;
78
import lombok.NoArgsConstructor;
@@ -11,6 +12,6 @@
1112
public class UpdateAccountStatusDTO {
1213
@NotBlank
1314
private String accountId ;
14-
@NotBlank
15+
@NotNull
1516
private AccountStatus accountStatus ;
1617
}

0 commit comments

Comments
 (0)