Update Projection
Add event handlers to update the products map of the OrderedProduct model #BAEL-4767
This commit is contained in:
parent
886368a4c3
commit
41d47f8aa6
|
@ -1,20 +1,23 @@
|
||||||
package com.baeldung.axon.querymodel;
|
package com.baeldung.axon.querymodel;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
|
||||||
import java.util.HashMap;
|
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
|
||||||
import java.util.List;
|
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
|
||||||
import java.util.Map;
|
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.OrderedProduct;
|
||||||
import org.axonframework.config.ProcessingGroup;
|
import org.axonframework.config.ProcessingGroup;
|
||||||
import org.axonframework.eventhandling.EventHandler;
|
import org.axonframework.eventhandling.EventHandler;
|
||||||
import org.axonframework.queryhandling.QueryHandler;
|
import org.axonframework.queryhandling.QueryHandler;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
|
import java.util.ArrayList;
|
||||||
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
|
import java.util.HashMap;
|
||||||
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
|
import java.util.List;
|
||||||
import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
|
import java.util.Map;
|
||||||
import com.baeldung.axon.coreapi.queries.OrderedProduct;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ProcessingGroup("ordered-products")
|
@ProcessingGroup("ordered-products")
|
||||||
|
@ -25,7 +28,39 @@ public class OrderedProductsEventHandler {
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void on(OrderPlacedEvent event) {
|
public void on(OrderPlacedEvent event) {
|
||||||
String orderId = event.getOrderId();
|
String orderId = event.getOrderId();
|
||||||
orderedProducts.put(orderId, new OrderedProduct(orderId, event.getProduct()));
|
orderedProducts.put(orderId, new OrderedProduct(orderId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void on(ProductAddedEvent event) {
|
||||||
|
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
|
||||||
|
orderedProduct.addProduct(event.getProductId());
|
||||||
|
return orderedProduct;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void on(ProductCountIncrementedEvent event) {
|
||||||
|
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
|
||||||
|
orderedProduct.incrementProductInstance(event.getProductId());
|
||||||
|
return orderedProduct;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void on(ProductCountDecrementedEvent event) {
|
||||||
|
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
|
||||||
|
orderedProduct.decrementProductInstance(event.getProductId());
|
||||||
|
return orderedProduct;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void on(ProductRemovedEvent event) {
|
||||||
|
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
|
||||||
|
orderedProduct.removeProduct(event.getProductId());
|
||||||
|
return orderedProduct;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
|
@ -48,5 +83,4 @@ public class OrderedProductsEventHandler {
|
||||||
public List<OrderedProduct> handle(FindAllOrderedProductsQuery query) {
|
public List<OrderedProduct> handle(FindAllOrderedProductsQuery query) {
|
||||||
return new ArrayList<>(orderedProducts.values());
|
return new ArrayList<>(orderedProducts.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue