From 41d47f8aa63ec643af7bced292f9bc356df89170 Mon Sep 17 00:00:00 2001 From: Steven van Beelen Date: Fri, 19 Mar 2021 11:08:42 +0100 Subject: [PATCH] Update Projection Add event handlers to update the products map of the OrderedProduct model #BAEL-4767 --- .../OrderedProductsEventHandler.java | 58 +++++++++++++++---- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/axon/src/main/java/com/baeldung/axon/querymodel/OrderedProductsEventHandler.java b/axon/src/main/java/com/baeldung/axon/querymodel/OrderedProductsEventHandler.java index a37f0111ed..3efd81fd37 100644 --- a/axon/src/main/java/com/baeldung/axon/querymodel/OrderedProductsEventHandler.java +++ b/axon/src/main/java/com/baeldung/axon/querymodel/OrderedProductsEventHandler.java @@ -1,20 +1,23 @@ package com.baeldung.axon.querymodel; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - +import com.baeldung.axon.coreapi.events.OrderConfirmedEvent; +import com.baeldung.axon.coreapi.events.OrderPlacedEvent; +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.OrderedProduct; import org.axonframework.config.ProcessingGroup; import org.axonframework.eventhandling.EventHandler; import org.axonframework.queryhandling.QueryHandler; import org.springframework.stereotype.Service; -import com.baeldung.axon.coreapi.events.OrderConfirmedEvent; -import com.baeldung.axon.coreapi.events.OrderPlacedEvent; -import com.baeldung.axon.coreapi.events.OrderShippedEvent; -import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery; -import com.baeldung.axon.coreapi.queries.OrderedProduct; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; @Service @ProcessingGroup("ordered-products") @@ -25,7 +28,39 @@ public class OrderedProductsEventHandler { @EventHandler public void on(OrderPlacedEvent event) { 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 @@ -48,5 +83,4 @@ public class OrderedProductsEventHandler { public List handle(FindAllOrderedProductsQuery query) { return new ArrayList<>(orderedProducts.values()); } - } \ No newline at end of file