Skip to content

Commit

Permalink
Update Query API
Browse files Browse the repository at this point in the history
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
  • Loading branch information
smcvb committed Mar 19, 2021
1 parent 37106a3 commit 452d369
Showing 1 changed file with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
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;
}

public String getOrderId() {
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;
}
Expand All @@ -35,29 +54,28 @@ public void setOrderShipped() {
}

@Override
public int hashCode() {
return Objects.hash(orderId, product, orderStatus);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
if (o == null || getClass() != o.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);
OrderedProduct that = (OrderedProduct) o;
return Objects.equals(orderId, that.orderId) && Objects.equals(products, that.products)
&& orderStatus == that.orderStatus;
}

@Override
public int hashCode() {
return Objects.hash(orderId, products, orderStatus);
}

@Override
public String toString() {
return "OrderedProduct{" +
"orderId='" + orderId + '\'' +
", product='" + product + '\'' +
", products=" + products +
", orderStatus=" + orderStatus +
'}';
}
Expand Down

0 comments on commit 452d369

Please sign in to comment.