From c42965230e9138da70ee9f8e13a28bc5c54117fc Mon Sep 17 00:00:00 2001 From: abidknashtech Date: Tue, 5 Dec 2023 16:42:18 +0530 Subject: [PATCH] added order-status endpoint --- .../query/FindOrderQuery.java | 2 +- .../query/FindOrdersByUserQuery.java | 12 ++++++++++++ .../query/OrderQueriesHandler.java | 13 +++++-------- .../repository/OrderRepository.java | 3 +++ .../restapi/OrdersCommandController.java | 18 ++++++++++++++---- 5 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 order-service/src/main/java/com.nashtech.order/query/FindOrdersByUserQuery.java diff --git a/order-service/src/main/java/com.nashtech.order/query/FindOrderQuery.java b/order-service/src/main/java/com.nashtech.order/query/FindOrderQuery.java index 3ca4bdf2..65210fc0 100644 --- a/order-service/src/main/java/com.nashtech.order/query/FindOrderQuery.java +++ b/order-service/src/main/java/com.nashtech.order/query/FindOrderQuery.java @@ -9,4 +9,4 @@ @Data public class FindOrderQuery { String orderId; -} \ No newline at end of file +} diff --git a/order-service/src/main/java/com.nashtech.order/query/FindOrdersByUserQuery.java b/order-service/src/main/java/com.nashtech.order/query/FindOrdersByUserQuery.java new file mode 100644 index 00000000..7e11aa26 --- /dev/null +++ b/order-service/src/main/java/com.nashtech.order/query/FindOrdersByUserQuery.java @@ -0,0 +1,12 @@ +package com.nashtech.order.query; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@AllArgsConstructor +@NoArgsConstructor +@Data +public class FindOrdersByUserQuery { + String userId; +} \ No newline at end of file diff --git a/order-service/src/main/java/com.nashtech.order/query/OrderQueriesHandler.java b/order-service/src/main/java/com.nashtech.order/query/OrderQueriesHandler.java index 5536f2b8..dd7c0703 100644 --- a/order-service/src/main/java/com.nashtech.order/query/OrderQueriesHandler.java +++ b/order-service/src/main/java/com.nashtech.order/query/OrderQueriesHandler.java @@ -1,26 +1,23 @@ package com.nashtech.order.query; -import com.nashtech.common.utils.OrderStatus; import com.nashtech.order.repository.OrderRepository; -import com.nashtech.order.restapi.response.OrderSummary; +import com.nashtech.order.repository.entity.OrderEntity; import org.axonframework.queryhandling.QueryHandler; import org.springframework.stereotype.Component; +import java.util.List; @Component public class OrderQueriesHandler { - OrderRepository ordersRepository; + private final OrderRepository ordersRepository; public OrderQueriesHandler(OrderRepository ordersRepository) { this.ordersRepository = ordersRepository; } @QueryHandler - public OrderSummary findOrder(FindOrderQuery findOrderQuery) { - return OrderSummary.builder() - .orderId(findOrderQuery.getOrderId()) - .orderStatus(OrderStatus.ORDER_PLACED.toString()) - .build(); + public List findOrders(FindOrdersByUserQuery findOrdersByUserQuery) { + return ordersRepository.findByUserId(findOrdersByUserQuery.getUserId()); } } diff --git a/order-service/src/main/java/com.nashtech.order/repository/OrderRepository.java b/order-service/src/main/java/com.nashtech.order/repository/OrderRepository.java index a4ae6f0d..98fa6492 100644 --- a/order-service/src/main/java/com.nashtech.order/repository/OrderRepository.java +++ b/order-service/src/main/java/com.nashtech.order/repository/OrderRepository.java @@ -3,6 +3,9 @@ import com.nashtech.order.repository.entity.OrderEntity; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface OrderRepository extends JpaRepository { + List findByUserId(String userId); OrderEntity findByOrderId(String orderId); } diff --git a/order-service/src/main/java/com.nashtech.order/restapi/OrdersCommandController.java b/order-service/src/main/java/com.nashtech.order/restapi/OrdersCommandController.java index 484a5d93..f5a6af09 100644 --- a/order-service/src/main/java/com.nashtech.order/restapi/OrdersCommandController.java +++ b/order-service/src/main/java/com.nashtech.order/restapi/OrdersCommandController.java @@ -3,24 +3,29 @@ import com.nashtech.common.utils.OrderStatus; import com.nashtech.order.commands.CreateOrderCommand; -import com.nashtech.order.query.FindOrderQuery; +import com.nashtech.order.query.FindOrdersByUserQuery; +import com.nashtech.order.repository.entity.OrderEntity; import com.nashtech.order.restapi.request.OrderCreateRequest; import com.nashtech.order.restapi.response.OrderSummary; +import lombok.extern.slf4j.Slf4j; import org.axonframework.commandhandling.gateway.CommandGateway; import org.axonframework.messaging.responsetypes.ResponseTypes; import org.axonframework.queryhandling.QueryGateway; -import org.axonframework.queryhandling.SubscriptionQueryResult; import org.springframework.beans.factory.annotation.Autowired; +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.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; - import javax.validation.Valid; +import java.util.List; import java.util.UUID; +import java.util.concurrent.ExecutionException; @RestController @RequestMapping("/orders") +@Slf4j public class OrdersCommandController { private final CommandGateway commandGateway; @@ -35,7 +40,7 @@ public OrdersCommandController(CommandGateway commandGateway, QueryGateway query @PostMapping("/create") public OrderSummary createOrder(@Valid @RequestBody OrderCreateRequest orderRequest) { String orderId = UUID.randomUUID().toString(); - + log.info("Order {} created request ", orderId); CreateOrderCommand createOrderCommand = CreateOrderCommand.builder() .productId(orderRequest.getProductId()).userId(orderRequest.getUserId()) .quantity(orderRequest.getQuantity()).orderId(orderId) @@ -57,4 +62,9 @@ public OrderSummary createOrder(@Valid @RequestBody OrderCreateRequest orderRequ .build(); } + @GetMapping("/{userId}") + public List getOrdersByUser(@PathVariable String userId) throws ExecutionException, InterruptedException { + log.info("Get orders status by userId: {}", userId); + return queryGateway.query(new FindOrdersByUserQuery(userId), ResponseTypes.multipleInstancesOf(OrderEntity.class)).get(); + } } \ No newline at end of file