Skip to content

Commit 84b6dc1

Browse files
committed
BAEL-2435 Switch domain around
Change the domain from 'Messages' to 'Order' as that domain is better suited for an example project like this. The Query Model would be better suited to revolve around the OrderedProducts rather than just printing out a line of text. To that end, add a OrderedProduct model with the OrderStatus, which is updated in the (renamed) OrderedProductsEventHandler upon all the events
1 parent eab6dfe commit 84b6dc1

4 files changed

Lines changed: 104 additions & 21 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.axon.coreapi.queries;
2+
3+
public enum OrderStatus {
4+
5+
PLACED, CONFIRMED, SHIPPED
6+
7+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.axon.coreapi.queries;
2+
3+
import java.util.Objects;
4+
5+
public class OrderedProduct {
6+
7+
private final String orderId;
8+
private final String product;
9+
private OrderStatus orderStatus;
10+
11+
public OrderedProduct(String orderId, String product) {
12+
this.orderId = orderId;
13+
this.product = product;
14+
orderStatus = OrderStatus.PLACED;
15+
}
16+
17+
public String getOrderId() {
18+
return orderId;
19+
}
20+
21+
public String getProduct() {
22+
return product;
23+
}
24+
25+
public OrderStatus getOrderStatus() {
26+
return orderStatus;
27+
}
28+
29+
public void setOrderConfirmed() {
30+
this.orderStatus = OrderStatus.CONFIRMED;
31+
}
32+
33+
public void setOrderShipped() {
34+
this.orderStatus = OrderStatus.SHIPPED;
35+
}
36+
37+
@Override
38+
public int hashCode() {
39+
return Objects.hash(orderId, product, orderStatus);
40+
}
41+
42+
@Override
43+
public boolean equals(Object obj) {
44+
if (this == obj) {
45+
return true;
46+
}
47+
if (obj == null || getClass() != obj.getClass()) {
48+
return false;
49+
}
50+
final OrderedProduct other = (OrderedProduct) obj;
51+
return Objects.equals(this.orderId, other.orderId)
52+
&& Objects.equals(this.product, other.product)
53+
&& Objects.equals(this.orderStatus, other.orderStatus);
54+
}
55+
}

axon/src/main/java/com/baeldung/axon/querymodel/MessagesEventHandler.java

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.axon.querymodel;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.axonframework.eventhandling.EventHandler;
7+
import org.springframework.stereotype.Service;
8+
9+
import com.baeldung.axon.coreapi.queries.OrderedProduct;
10+
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
11+
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
12+
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
13+
14+
@Service
15+
public class OrderedProductsEventHandler {
16+
17+
private final Map<String, OrderedProduct> orderedProducts = new HashMap<>();
18+
19+
@EventHandler
20+
public void on(OrderPlacedEvent event) {
21+
String orderId = event.getOrderId();
22+
orderedProducts.put(orderId, new OrderedProduct(orderId, event.getProduct()));
23+
24+
}
25+
26+
@EventHandler
27+
public void on(OrderShippedEvent event) {
28+
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
29+
orderedProduct.setOrderShipped();
30+
return orderedProduct;
31+
});
32+
}
33+
34+
@EventHandler
35+
public void on(OrderConfirmedEvent event) {
36+
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
37+
orderedProduct.setOrderConfirmed();
38+
return orderedProduct;
39+
});
40+
}
41+
42+
}

0 commit comments

Comments
 (0)