From 10c0f2302e9f2afeb70858c7adcd9a24397f76f7 Mon Sep 17 00:00:00 2001 From: Steven van Beelen Date: Fri, 19 Mar 2021 10:55:15 +0100 Subject: [PATCH] Update Command API - Adjust PlaceOrderCommand to only construct the order - Introduce AddProductCommand to be able to add several products to an order - Introduce IncrementProductCountCommand to increase the number of product instances for a given Order - Introduce DecrementProductCountCommand to increase the number of product instances for a given Order #BAEL-4767 --- .../coreapi/commands/AddProductCommand.java | 50 +++++++++++++++++++ .../DecrementProductCountCommand.java | 50 +++++++++++++++++++ .../IncrementProductCountCommand.java | 50 +++++++++++++++++++ .../coreapi/commands/PlaceOrderCommand.java | 36 ++++++------- 4 files changed, 164 insertions(+), 22 deletions(-) create mode 100644 axon/src/main/java/com/baeldung/axon/coreapi/commands/AddProductCommand.java create mode 100644 axon/src/main/java/com/baeldung/axon/coreapi/commands/DecrementProductCountCommand.java create mode 100644 axon/src/main/java/com/baeldung/axon/coreapi/commands/IncrementProductCountCommand.java diff --git a/axon/src/main/java/com/baeldung/axon/coreapi/commands/AddProductCommand.java b/axon/src/main/java/com/baeldung/axon/coreapi/commands/AddProductCommand.java new file mode 100644 index 0000000000..28736aaadc --- /dev/null +++ b/axon/src/main/java/com/baeldung/axon/coreapi/commands/AddProductCommand.java @@ -0,0 +1,50 @@ +package com.baeldung.axon.coreapi.commands; + +import org.axonframework.modelling.command.TargetAggregateIdentifier; + +import java.util.Objects; + +public class AddProductCommand { + + @TargetAggregateIdentifier + private final String orderId; + private final String productId; + + public AddProductCommand(String orderId, String productId) { + this.orderId = orderId; + this.productId = productId; + } + + public String getOrderId() { + return orderId; + } + + public String getProductId() { + return productId; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AddProductCommand that = (AddProductCommand) o; + return Objects.equals(orderId, that.orderId) && Objects.equals(productId, that.productId); + } + + @Override + public int hashCode() { + return Objects.hash(orderId, productId); + } + + @Override + public String toString() { + return "AddProductCommand{" + + "orderId='" + orderId + '\'' + + ", productId='" + productId + '\'' + + '}'; + } +} diff --git a/axon/src/main/java/com/baeldung/axon/coreapi/commands/DecrementProductCountCommand.java b/axon/src/main/java/com/baeldung/axon/coreapi/commands/DecrementProductCountCommand.java new file mode 100644 index 0000000000..f6f4db00fc --- /dev/null +++ b/axon/src/main/java/com/baeldung/axon/coreapi/commands/DecrementProductCountCommand.java @@ -0,0 +1,50 @@ +package com.baeldung.axon.coreapi.commands; + +import org.axonframework.modelling.command.TargetAggregateIdentifier; + +import java.util.Objects; + +public class DecrementProductCountCommand { + + @TargetAggregateIdentifier + private final String orderId; + private final String productId; + + public DecrementProductCountCommand(String orderId, String productId) { + this.orderId = orderId; + this.productId = productId; + } + + public String getOrderId() { + return orderId; + } + + public String getProductId() { + return productId; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + DecrementProductCountCommand that = (DecrementProductCountCommand) o; + return Objects.equals(orderId, that.orderId) && Objects.equals(productId, that.productId); + } + + @Override + public int hashCode() { + return Objects.hash(orderId, productId); + } + + @Override + public String toString() { + return "DecrementProductCountCommand{" + + "orderId='" + orderId + '\'' + + ", productId='" + productId + '\'' + + '}'; + } +} diff --git a/axon/src/main/java/com/baeldung/axon/coreapi/commands/IncrementProductCountCommand.java b/axon/src/main/java/com/baeldung/axon/coreapi/commands/IncrementProductCountCommand.java new file mode 100644 index 0000000000..548faabe37 --- /dev/null +++ b/axon/src/main/java/com/baeldung/axon/coreapi/commands/IncrementProductCountCommand.java @@ -0,0 +1,50 @@ +package com.baeldung.axon.coreapi.commands; + +import org.axonframework.modelling.command.TargetAggregateIdentifier; + +import java.util.Objects; + +public class IncrementProductCountCommand { + + @TargetAggregateIdentifier + private final String orderId; + private final String productId; + + public IncrementProductCountCommand(String orderId, String productId) { + this.orderId = orderId; + this.productId = productId; + } + + public String getOrderId() { + return orderId; + } + + public String getProductId() { + return productId; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + IncrementProductCountCommand that = (IncrementProductCountCommand) o; + return Objects.equals(orderId, that.orderId) && Objects.equals(productId, that.productId); + } + + @Override + public int hashCode() { + return Objects.hash(orderId, productId); + } + + @Override + public String toString() { + return "IncrementProductCountCommand{" + + "orderId='" + orderId + '\'' + + ", productId='" + productId + '\'' + + '}'; + } +} diff --git a/axon/src/main/java/com/baeldung/axon/coreapi/commands/PlaceOrderCommand.java b/axon/src/main/java/com/baeldung/axon/coreapi/commands/PlaceOrderCommand.java index c70d503050..b631272366 100644 --- a/axon/src/main/java/com/baeldung/axon/coreapi/commands/PlaceOrderCommand.java +++ b/axon/src/main/java/com/baeldung/axon/coreapi/commands/PlaceOrderCommand.java @@ -1,51 +1,43 @@ package com.baeldung.axon.coreapi.commands; -import java.util.Objects; - import org.axonframework.modelling.command.TargetAggregateIdentifier; +import java.util.Objects; + public class PlaceOrderCommand { @TargetAggregateIdentifier private final String orderId; - private final String product; - public PlaceOrderCommand(String orderId, String product) { + public PlaceOrderCommand(String orderId) { this.orderId = orderId; - this.product = product; } public String getOrderId() { return orderId; } - public String getProduct() { - return product; + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + PlaceOrderCommand that = (PlaceOrderCommand) o; + return Objects.equals(orderId, that.orderId); } @Override public int hashCode() { - return Objects.hash(orderId, product); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - final PlaceOrderCommand other = (PlaceOrderCommand) obj; - return Objects.equals(this.orderId, other.orderId) - && Objects.equals(this.product, other.product); + return Objects.hash(orderId); } @Override public String toString() { return "PlaceOrderCommand{" + "orderId='" + orderId + '\'' + - ", product='" + product + '\'' + '}'; } } \ No newline at end of file