From 68276b8041ed5997edbbf7c4137fc791b4b29215 Mon Sep 17 00:00:00 2001 From: Steven van Beelen Date: Fri, 19 Mar 2021 11:23:46 +0100 Subject: [PATCH] Allow all operations on the Controller Add post mappings to allow single creation of an Order, influencing all aspects of an Order #BAEL-4767 --- .../baeldung/axon/gui/OrderRestEndpoint.java | 76 +++++++++++++++---- 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/axon/src/main/java/com/baeldung/axon/gui/OrderRestEndpoint.java b/axon/src/main/java/com/baeldung/axon/gui/OrderRestEndpoint.java index a9f34cc691..f9e1748cbe 100644 --- a/axon/src/main/java/com/baeldung/axon/gui/OrderRestEndpoint.java +++ b/axon/src/main/java/com/baeldung/axon/gui/OrderRestEndpoint.java @@ -1,20 +1,24 @@ 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.AddProductCommand; import com.baeldung.axon.coreapi.commands.ConfirmOrderCommand; +import com.baeldung.axon.coreapi.commands.DecrementProductCountCommand; +import com.baeldung.axon.coreapi.commands.IncrementProductCountCommand; 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; +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.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; @RestController public class OrderRestEndpoint { @@ -30,7 +34,8 @@ public class OrderRestEndpoint { @PostMapping("/ship-order") public void shipOrder() { String orderId = UUID.randomUUID().toString(); - commandGateway.send(new PlaceOrderCommand(orderId, "Deluxe Chair")); + commandGateway.send(new PlaceOrderCommand(orderId)); + commandGateway.send(new AddProductCommand(orderId, "Deluxe Chair")); commandGateway.send(new ConfirmOrderCommand(orderId)); commandGateway.send(new ShipOrderCommand(orderId)); } @@ -38,15 +43,54 @@ public class OrderRestEndpoint { @PostMapping("/ship-unconfirmed-order") public void shipUnconfirmedOrder() { String orderId = UUID.randomUUID().toString(); - commandGateway.send(new PlaceOrderCommand(orderId, "Deluxe Chair")); + commandGateway.send(new PlaceOrderCommand(orderId)); + commandGateway.send(new AddProductCommand(orderId, "Deluxe Chair")); // This throws an exception, as an Order cannot be shipped if it has not been confirmed yet. commandGateway.send(new ShipOrderCommand(orderId)); } - @GetMapping("/all-orders") - public List findAllOrderedProducts() { - return queryGateway.query(new FindAllOrderedProductsQuery(), ResponseTypes.multipleInstancesOf(OrderedProduct.class)) - .join(); + @PostMapping("/order") + public CompletableFuture placeOrder() { + return placeOrder(UUID.randomUUID().toString()); } + @PostMapping("/order/{order-id}") + public CompletableFuture placeOrder(@PathVariable("order-id") String orderId) { + return commandGateway.send(new PlaceOrderCommand(orderId)); + } + + @PostMapping("/order/{order-id}/product/{product-id}") + public CompletableFuture addProduct(@PathVariable("order-id") String orderId, + @PathVariable("product-id") String productId) { + return commandGateway.send(new AddProductCommand(orderId, productId)); + } + + @PostMapping("/order/{order-id}/product/{product-id}/increment") + public CompletableFuture incrementProduct(@PathVariable("order-id") String orderId, + @PathVariable("product-id") String productId) { + return commandGateway.send(new IncrementProductCountCommand(orderId, productId)); + } + + @PostMapping("/order/{order-id}/product/{product-id}/decrement") + public CompletableFuture decrementProduct(@PathVariable("order-id") String orderId, + @PathVariable("product-id") String productId) { + return commandGateway.send(new DecrementProductCountCommand(orderId, productId)); + } + + @PostMapping("/order/{order-id}/confirm") + public CompletableFuture confirmOrder(@PathVariable("order-id") String orderId) { + return commandGateway.send(new ConfirmOrderCommand(orderId)); + } + + @PostMapping("/order/{order-id}/ship") + public CompletableFuture shipOrder(@PathVariable("order-id") String orderId) { + return commandGateway.send(new ShipOrderCommand(orderId)); + } + + @GetMapping("/all-orders") + public CompletableFuture> findAllOrderedProducts() { + return queryGateway.query( + new FindAllOrderedProductsQuery(), ResponseTypes.multipleInstancesOf(OrderedProduct.class) + ); + } }