Skip to content

Commit

Permalink
Rename API in line with adjusted approach
Browse files Browse the repository at this point in the history
Change PlaceOrderCommand/OrderPlacedEvent to
CreateOrderCommand/OrderCreatedEvent. Placed suggests it is already
placed, meaning products cannot be changed. However with the new
OrderLine member and its operations it's no longer correct to state the
order placed upon creation. Furthermore, the OrderedProduct is no longer
 a single product, but contains several. Renaming this to Order is more
 in line with what this query model resembles

#BAEL-4767
  • Loading branch information
smcvb committed Mar 19, 2021
1 parent 449fcf5 commit 213f6f0
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.baeldung.axon.coreapi.commands.AddProductCommand;
import com.baeldung.axon.coreapi.commands.ConfirmOrderCommand;
import com.baeldung.axon.coreapi.commands.PlaceOrderCommand;
import com.baeldung.axon.coreapi.commands.CreateOrderCommand;
import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
import com.baeldung.axon.coreapi.events.OrderCreatedEvent;
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
import com.baeldung.axon.coreapi.events.ProductAddedEvent;
import com.baeldung.axon.coreapi.events.ProductRemovedEvent;
Expand Down Expand Up @@ -34,8 +34,8 @@ public class OrderAggregate {
private Map<String, OrderLine> orderLines;

@CommandHandler
public OrderAggregate(PlaceOrderCommand command) {
apply(new OrderPlacedEvent(command.getOrderId()));
public OrderAggregate(CreateOrderCommand command) {
apply(new OrderCreatedEvent(command.getOrderId()));
}

@CommandHandler
Expand Down Expand Up @@ -70,7 +70,7 @@ public void handle(ShipOrderCommand command) {
}

@EventSourcingHandler
public void on(OrderPlacedEvent event) {
public void on(OrderCreatedEvent event) {
this.orderId = event.getOrderId();
this.orderConfirmed = false;
this.orderLines = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import java.util.Objects;

public class PlaceOrderCommand {
public class CreateOrderCommand {

@TargetAggregateIdentifier
private final String orderId;

public PlaceOrderCommand(String orderId) {
public CreateOrderCommand(String orderId) {
this.orderId = orderId;
}

Expand All @@ -25,7 +25,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
PlaceOrderCommand that = (PlaceOrderCommand) o;
CreateOrderCommand that = (CreateOrderCommand) o;
return Objects.equals(orderId, that.orderId);
}

Expand All @@ -36,7 +36,7 @@ public int hashCode() {

@Override
public String toString() {
return "PlaceOrderCommand{" +
return "CreateOrderCommand{" +
"orderId='" + orderId + '\'' +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.util.Objects;

public class OrderPlacedEvent {
public class OrderCreatedEvent {

private final String orderId;

public OrderPlacedEvent(String orderId) {
public OrderCreatedEvent(String orderId) {
this.orderId = orderId;
}

Expand All @@ -22,7 +22,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
OrderPlacedEvent that = (OrderPlacedEvent) o;
OrderCreatedEvent that = (OrderCreatedEvent) o;
return Objects.equals(orderId, that.orderId);
}

Expand All @@ -33,7 +33,7 @@ public int hashCode() {

@Override
public String toString() {
return "OrderPlacedEvent{" +
return "OrderCreatedEvent{" +
"orderId='" + orderId + '\'' +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import java.util.Map;
import java.util.Objects;

public class OrderedProduct {
public class Order {

private final String orderId;
private final Map<String, Integer> products;
private OrderStatus orderStatus;

public OrderedProduct(String orderId) {
public Order(String orderId) {
this.orderId = orderId;
this.products = new HashMap<>();
orderStatus = OrderStatus.PLACED;
orderStatus = OrderStatus.CREATED;
}

public String getOrderId() {
Expand Down Expand Up @@ -61,8 +61,9 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
OrderedProduct that = (OrderedProduct) o;
return Objects.equals(orderId, that.orderId) && Objects.equals(products, that.products)
Order that = (Order) o;
return Objects.equals(orderId, that.orderId)
&& Objects.equals(products, that.products)
&& orderStatus == that.orderStatus;
}

Expand All @@ -73,7 +74,7 @@ public int hashCode() {

@Override
public String toString() {
return "OrderedProduct{" +
return "Order{" +
"orderId='" + orderId + '\'' +
", products=" + products +
", orderStatus=" + orderStatus +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

public enum OrderStatus {

PLACED, CONFIRMED, SHIPPED

CREATED, CONFIRMED, SHIPPED
}
22 changes: 10 additions & 12 deletions axon/src/main/java/com/baeldung/axon/gui/OrderRestEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.baeldung.axon.coreapi.commands.AddProductCommand;
import com.baeldung.axon.coreapi.commands.ConfirmOrderCommand;
import com.baeldung.axon.coreapi.commands.CreateOrderCommand;
import com.baeldung.axon.coreapi.commands.DecrementProductCountCommand;
import com.baeldung.axon.coreapi.commands.IncrementProductCountCommand;
import com.baeldung.axon.coreapi.commands.PlaceOrderCommand;
import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
import com.baeldung.axon.coreapi.queries.OrderedProduct;
import com.baeldung.axon.coreapi.queries.Order;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.messaging.responsetypes.ResponseTypes;
import org.axonframework.queryhandling.QueryGateway;
Expand All @@ -34,7 +34,7 @@ public OrderRestEndpoint(CommandGateway commandGateway, QueryGateway queryGatewa
@PostMapping("/ship-order")
public void shipOrder() {
String orderId = UUID.randomUUID().toString();
commandGateway.send(new PlaceOrderCommand(orderId));
commandGateway.send(new CreateOrderCommand(orderId));
commandGateway.send(new AddProductCommand(orderId, "Deluxe Chair"));
commandGateway.send(new ConfirmOrderCommand(orderId));
commandGateway.send(new ShipOrderCommand(orderId));
Expand All @@ -43,20 +43,20 @@ public void shipOrder() {
@PostMapping("/ship-unconfirmed-order")
public void shipUnconfirmedOrder() {
String orderId = UUID.randomUUID().toString();
commandGateway.send(new PlaceOrderCommand(orderId));
commandGateway.send(new CreateOrderCommand(orderId));
commandGateway.send(new AddProductCommand(orderId, "Deluxe Chair"));
// This throws an exception, as an Order cannot be shipped if it has not been confirmed yet.
commandGateway.send(new ShipOrderCommand(orderId));
}

@PostMapping("/order")
public CompletableFuture<String> placeOrder() {
return placeOrder(UUID.randomUUID().toString());
public CompletableFuture<String> createOrder() {
return createOrder(UUID.randomUUID().toString());
}

@PostMapping("/order/{order-id}")
public CompletableFuture<String> placeOrder(@PathVariable("order-id") String orderId) {
return commandGateway.send(new PlaceOrderCommand(orderId));
public CompletableFuture<String> createOrder(@PathVariable("order-id") String orderId) {
return commandGateway.send(new CreateOrderCommand(orderId));
}

@PostMapping("/order/{order-id}/product/{product-id}")
Expand Down Expand Up @@ -88,9 +88,7 @@ public CompletableFuture<Void> shipOrder(@PathVariable("order-id") String orderI
}

@GetMapping("/all-orders")
public CompletableFuture<List<OrderedProduct>> findAllOrderedProducts() {
return queryGateway.query(
new FindAllOrderedProductsQuery(), ResponseTypes.multipleInstancesOf(OrderedProduct.class)
);
public CompletableFuture<List<Order>> findAllOrders() {
return queryGateway.query(new FindAllOrderedProductsQuery(), ResponseTypes.multipleInstancesOf(Order.class));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.baeldung.axon.querymodel;

import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
import com.baeldung.axon.coreapi.events.OrderCreatedEvent;
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
import com.baeldung.axon.coreapi.events.ProductAddedEvent;
import com.baeldung.axon.coreapi.events.ProductCountDecrementedEvent;
import com.baeldung.axon.coreapi.events.ProductCountIncrementedEvent;
import com.baeldung.axon.coreapi.events.ProductRemovedEvent;
import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
import com.baeldung.axon.coreapi.queries.Order;
import org.axonframework.config.ProcessingGroup;
import org.axonframework.eventhandling.EventHandler;
import org.axonframework.queryhandling.QueryHandler;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@ProcessingGroup("orders")
public class OrdersEventHandler {

private final Map<String, Order> orders = new HashMap<>();

@EventHandler
public void on(OrderCreatedEvent event) {
String orderId = event.getOrderId();
orders.put(orderId, new Order(orderId));
}

@EventHandler
public void on(ProductAddedEvent event) {
orders.computeIfPresent(event.getOrderId(), (orderId, order) -> {
order.addProduct(event.getProductId());
return order;
});
}

@EventHandler
public void on(ProductCountIncrementedEvent event) {
orders.computeIfPresent(event.getOrderId(), (orderId, order) -> {
order.incrementProductInstance(event.getProductId());
return order;
});
}

@EventHandler
public void on(ProductCountDecrementedEvent event) {
orders.computeIfPresent(event.getOrderId(), (orderId, order) -> {
order.decrementProductInstance(event.getProductId());
return order;
});
}

@EventHandler
public void on(ProductRemovedEvent event) {
orders.computeIfPresent(event.getOrderId(), (orderId, order) -> {
order.removeProduct(event.getProductId());
return order;
});
}

@EventHandler
public void on(OrderConfirmedEvent event) {
orders.computeIfPresent(event.getOrderId(), (orderId, order) -> {
order.setOrderConfirmed();
return order;
});
}

@EventHandler
public void on(OrderShippedEvent event) {
orders.computeIfPresent(event.getOrderId(), (orderId, order) -> {
order.setOrderShipped();
return order;
});
}

@QueryHandler
public List<Order> handle(FindAllOrderedProductsQuery query) {
return new ArrayList<>(orders.values());
}
}
Loading

0 comments on commit 213f6f0

Please sign in to comment.