BAEL-2435 Introduce Query message logic

Introduce a FindAllOrderedProductsQuery query message, which is
published on the QueryGateway through the OrderRestEndpoint and handled
by the OrderedProductsEventHandler
This commit is contained in:
Steven van Beelen 2018-12-27 10:39:42 +01:00
parent 84b6dc1dbb
commit 769af6759e
3 changed files with 30 additions and 2 deletions

View File

@ -0,0 +1,5 @@
package com.baeldung.axon.coreapi.queries;
public class FindAllOrderedProductsQuery {
}

View File

@ -1,14 +1,20 @@
package com.baeldung.axon.gui;
import java.util.List;
import java.util.UUID;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.messaging.responsetypes.ResponseTypes;
import org.axonframework.queryhandling.QueryGateway;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.axon.coreapi.commands.ConfirmOrderCommand;
import com.baeldung.axon.coreapi.commands.PlaceOrderCommand;
import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
import com.baeldung.axon.coreapi.queries.OrderedProduct;
@RestController
public class OrderRestEndpoint {
@ -16,9 +22,11 @@ public class OrderRestEndpoint {
private static final String DEFAULT_PRODUCT = "Deluxe Chair";
private final CommandGateway commandGateway;
private final QueryGateway queryGateway;
public OrderRestEndpoint(CommandGateway commandGateway) {
public OrderRestEndpoint(CommandGateway commandGateway, QueryGateway queryGateway) {
this.commandGateway = commandGateway;
this.queryGateway = queryGateway;
}
@PostMapping("/ship-order")
@ -37,4 +45,10 @@ public class OrderRestEndpoint {
commandGateway.send(new ShipOrderCommand(orderId));
}
@GetMapping("/all-orders")
public List<OrderedProduct> findAllOrderedProducts() {
return queryGateway.query(new FindAllOrderedProductsQuery(), ResponseTypes.multipleInstancesOf(OrderedProduct.class))
.join();
}
}

View File

@ -1,15 +1,19 @@
package com.baeldung.axon.querymodel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.axonframework.eventhandling.EventHandler;
import org.axonframework.queryhandling.QueryHandler;
import org.springframework.stereotype.Service;
import com.baeldung.axon.coreapi.queries.OrderedProduct;
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;
@Service
public class OrderedProductsEventHandler {
@ -39,4 +43,9 @@ public class OrderedProductsEventHandler {
});
}
@QueryHandler
public List<OrderedProduct> handle(FindAllOrderedProductsQuery query) {
return new ArrayList<>(orderedProducts.values());
}
}