Update Query API

Change the OrderedProduct model such that it contains a map of products
having the count as its value, instead of a single product.

#BAEL-4767
This commit is contained in:
Steven van Beelen 2021-03-19 11:01:19 +01:00
parent a9812052df
commit 2b2aff5ceb
1 changed files with 37 additions and 19 deletions

View File

@ -1,16 +1,18 @@
package com.baeldung.axon.coreapi.queries;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class OrderedProduct {
private final String orderId;
private final String product;
private final Map<String, Integer> products;
private OrderStatus orderStatus;
public OrderedProduct(String orderId, String product) {
public OrderedProduct(String orderId) {
this.orderId = orderId;
this.product = product;
this.products = new HashMap<>();
orderStatus = OrderStatus.PLACED;
}
@ -18,14 +20,31 @@ public class OrderedProduct {
return orderId;
}
public String getProduct() {
return product;
public Map<String, Integer> getProducts() {
return products;
}
public OrderStatus getOrderStatus() {
return orderStatus;
}
public void addProduct(String productId) {
products.putIfAbsent(productId, 1);
}
public void incrementProductInstance(String productId) {
products.computeIfPresent(productId, (id, count) -> ++count);
}
public void decrementProductInstance(String productId) {
products.computeIfPresent(productId, (id, count) -> --count);
}
public void removeProduct(String productId) {
products.remove(productId);
}
public void setOrderConfirmed() {
this.orderStatus = OrderStatus.CONFIRMED;
}
@ -35,29 +54,28 @@ public class OrderedProduct {
}
@Override
public int hashCode() {
return Objects.hash(orderId, product, orderStatus);
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
OrderedProduct that = (OrderedProduct) o;
return Objects.equals(orderId, that.orderId) && Objects.equals(products, that.products)
&& orderStatus == that.orderStatus;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final OrderedProduct other = (OrderedProduct) obj;
return Objects.equals(this.orderId, other.orderId)
&& Objects.equals(this.product, other.product)
&& Objects.equals(this.orderStatus, other.orderStatus);
public int hashCode() {
return Objects.hash(orderId, products, orderStatus);
}
@Override
public String toString() {
return "OrderedProduct{" +
"orderId='" + orderId + '\'' +
", product='" + product + '\'' +
", products=" + products +
", orderStatus=" + orderStatus +
'}';
}