From e208650524c9548e52d0083ba001b4d7f436dc35 Mon Sep 17 00:00:00 2001 From: Murilo Kakazu Date: Tue, 28 May 2024 01:14:22 -0300 Subject: [PATCH 1/2] feat: add route to list orders --- .../adapters/in/rest/OrderResource.java | 15 ++++++++++- .../usecases/order/ListOrdersUseCase.java | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java index 9a1147b..0807546 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java @@ -4,6 +4,7 @@ import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.domain.usecases.order.AddProductToOrderUseCase; import br.com.fiap.grupo30.fastfood.domain.usecases.order.GetOrderUseCase; +import br.com.fiap.grupo30.fastfood.domain.usecases.order.ListOrdersUseCase; import br.com.fiap.grupo30.fastfood.domain.usecases.order.RemoveProductFromOrderUseCase; import br.com.fiap.grupo30.fastfood.domain.usecases.order.StartNewOrderUseCase; import br.com.fiap.grupo30.fastfood.domain.usecases.order.SubmitOrderUseCase; @@ -25,6 +26,7 @@ public class OrderResource { private final RemoveProductFromOrderUseCase removeProductFromOrderUseCase; private final GetOrderUseCase getOrderUseCase; private final SubmitOrderUseCase submitOrderUseCase; + private final ListOrdersUseCase listAllOrdersUseCase; @Autowired public OrderResource( @@ -32,12 +34,23 @@ public OrderResource( AddProductToOrderUseCase addProductToOrderUseCase, RemoveProductFromOrderUseCase removeProductFromOrderUseCase, GetOrderUseCase getOrderUseCase, - SubmitOrderUseCase submitOrderUseCase) { + SubmitOrderUseCase submitOrderUseCase, + ListOrdersUseCase listAllOrdersUseCase) { this.startNewOrderUseCase = startNewOrderUseCase; this.addProductToOrderUseCase = addProductToOrderUseCase; this.removeProductFromOrderUseCase = removeProductFromOrderUseCase; this.getOrderUseCase = getOrderUseCase; this.submitOrderUseCase = submitOrderUseCase; + this.listAllOrdersUseCase = listAllOrdersUseCase; + } + + @GetMapping(value = "/") + @Operation( + summary = "List all orders", + description = "Return all orders") + public ResponseEntity listAllOrders() { + OrderDTO[] orders = this.listAllOrdersUseCase.execute(); + return ResponseEntity.ok().body(orders); } @GetMapping(value = "/{orderId}") diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java new file mode 100644 index 0000000..0d08bc8 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java @@ -0,0 +1,27 @@ +package br.com.fiap.grupo30.fastfood.domain.usecases.order; + +import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO; +import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderEntity; +import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.repositories.OrderRepository; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ListOrdersUseCase { + + private final OrderRepository orderRepository; + + @Autowired + public ListOrdersUseCase(OrderRepository orderRepository) { + this.orderRepository = orderRepository; + } + + public OrderDTO[] execute() { + List allOrders = this.orderRepository.findAll(); + + return allOrders.stream().map(order -> order.toDTO()).toArray(OrderDTO[]::new); + } +} From 868162d086fc82b12cbe723b6b6d75106d5ce6a1 Mon Sep 17 00:00:00 2001 From: Murilo Kakazu Date: Tue, 28 May 2024 01:15:00 -0300 Subject: [PATCH 2/2] style: code linting --- .../fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java | 4 +--- .../fastfood/domain/usecases/order/ListOrdersUseCase.java | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java index 0807546..0fd9673 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/OrderResource.java @@ -45,9 +45,7 @@ public OrderResource( } @GetMapping(value = "/") - @Operation( - summary = "List all orders", - description = "Return all orders") + @Operation(summary = "List all orders", description = "Return all orders") public ResponseEntity listAllOrders() { OrderDTO[] orders = this.listAllOrdersUseCase.execute(); return ResponseEntity.ok().body(orders); diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java index 0d08bc8..ea41fb4 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java @@ -3,9 +3,7 @@ import br.com.fiap.grupo30.fastfood.application.dto.OrderDTO; import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.entities.OrderEntity; import br.com.fiap.grupo30.fastfood.infrastructure.out.persistence.jpa.repositories.OrderRepository; - import java.util.List; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;