11package com .baeldung .axon .gui ;
22
3- import java .util .List ;
4- import java .util .UUID ;
5-
3+ import com .baeldung .axon .coreapi .commands .AddProductCommand ;
4+ import com .baeldung .axon .coreapi .commands .ConfirmOrderCommand ;
5+ import com .baeldung .axon .coreapi .commands .DecrementProductCountCommand ;
6+ import com .baeldung .axon .coreapi .commands .IncrementProductCountCommand ;
7+ import com .baeldung .axon .coreapi .commands .PlaceOrderCommand ;
8+ import com .baeldung .axon .coreapi .commands .ShipOrderCommand ;
9+ import com .baeldung .axon .coreapi .queries .FindAllOrderedProductsQuery ;
10+ import com .baeldung .axon .coreapi .queries .OrderedProduct ;
611import org .axonframework .commandhandling .gateway .CommandGateway ;
712import org .axonframework .messaging .responsetypes .ResponseTypes ;
813import org .axonframework .queryhandling .QueryGateway ;
914import org .springframework .web .bind .annotation .GetMapping ;
15+ import org .springframework .web .bind .annotation .PathVariable ;
1016import org .springframework .web .bind .annotation .PostMapping ;
1117import org .springframework .web .bind .annotation .RestController ;
1218
13- import com .baeldung .axon .coreapi .commands .ConfirmOrderCommand ;
14- import com .baeldung .axon .coreapi .commands .PlaceOrderCommand ;
15- import com .baeldung .axon .coreapi .commands .ShipOrderCommand ;
16- import com .baeldung .axon .coreapi .queries .FindAllOrderedProductsQuery ;
17- import com .baeldung .axon .coreapi .queries .OrderedProduct ;
19+ import java .util .List ;
20+ import java .util .UUID ;
21+ import java .util .concurrent .CompletableFuture ;
1822
1923@ RestController
2024public class OrderRestEndpoint {
@@ -30,23 +34,63 @@ public OrderRestEndpoint(CommandGateway commandGateway, QueryGateway queryGatewa
3034 @ PostMapping ("/ship-order" )
3135 public void shipOrder () {
3236 String orderId = UUID .randomUUID ().toString ();
33- commandGateway .send (new PlaceOrderCommand (orderId , "Deluxe Chair" ));
37+ commandGateway .send (new PlaceOrderCommand (orderId ));
38+ commandGateway .send (new AddProductCommand (orderId , "Deluxe Chair" ));
3439 commandGateway .send (new ConfirmOrderCommand (orderId ));
3540 commandGateway .send (new ShipOrderCommand (orderId ));
3641 }
3742
3843 @ PostMapping ("/ship-unconfirmed-order" )
3944 public void shipUnconfirmedOrder () {
4045 String orderId = UUID .randomUUID ().toString ();
41- commandGateway .send (new PlaceOrderCommand (orderId , "Deluxe Chair" ));
46+ commandGateway .send (new PlaceOrderCommand (orderId ));
47+ commandGateway .send (new AddProductCommand (orderId , "Deluxe Chair" ));
4248 // This throws an exception, as an Order cannot be shipped if it has not been confirmed yet.
4349 commandGateway .send (new ShipOrderCommand (orderId ));
4450 }
4551
46- @ GetMapping ("/all-orders" )
47- public List <OrderedProduct > findAllOrderedProducts () {
48- return queryGateway .query (new FindAllOrderedProductsQuery (), ResponseTypes .multipleInstancesOf (OrderedProduct .class ))
49- .join ();
52+ @ PostMapping ("/order" )
53+ public CompletableFuture <String > placeOrder () {
54+ return placeOrder (UUID .randomUUID ().toString ());
55+ }
56+
57+ @ PostMapping ("/order/{order-id}" )
58+ public CompletableFuture <String > placeOrder (@ PathVariable ("order-id" ) String orderId ) {
59+ return commandGateway .send (new PlaceOrderCommand (orderId ));
60+ }
61+
62+ @ PostMapping ("/order/{order-id}/product/{product-id}" )
63+ public CompletableFuture <Void > addProduct (@ PathVariable ("order-id" ) String orderId ,
64+ @ PathVariable ("product-id" ) String productId ) {
65+ return commandGateway .send (new AddProductCommand (orderId , productId ));
66+ }
67+
68+ @ PostMapping ("/order/{order-id}/product/{product-id}/increment" )
69+ public CompletableFuture <Void > incrementProduct (@ PathVariable ("order-id" ) String orderId ,
70+ @ PathVariable ("product-id" ) String productId ) {
71+ return commandGateway .send (new IncrementProductCountCommand (orderId , productId ));
5072 }
5173
74+ @ PostMapping ("/order/{order-id}/product/{product-id}/decrement" )
75+ public CompletableFuture <Void > decrementProduct (@ PathVariable ("order-id" ) String orderId ,
76+ @ PathVariable ("product-id" ) String productId ) {
77+ return commandGateway .send (new DecrementProductCountCommand (orderId , productId ));
78+ }
79+
80+ @ PostMapping ("/order/{order-id}/confirm" )
81+ public CompletableFuture <Void > confirmOrder (@ PathVariable ("order-id" ) String orderId ) {
82+ return commandGateway .send (new ConfirmOrderCommand (orderId ));
83+ }
84+
85+ @ PostMapping ("/order/{order-id}/ship" )
86+ public CompletableFuture <Void > shipOrder (@ PathVariable ("order-id" ) String orderId ) {
87+ return commandGateway .send (new ShipOrderCommand (orderId ));
88+ }
89+
90+ @ GetMapping ("/all-orders" )
91+ public CompletableFuture <List <OrderedProduct >> findAllOrderedProducts () {
92+ return queryGateway .query (
93+ new FindAllOrderedProductsQuery (), ResponseTypes .multipleInstancesOf (OrderedProduct .class )
94+ );
95+ }
5296}
0 commit comments