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..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 @@ -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,21 @@ 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..ea41fb4 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/domain/usecases/order/ListOrdersUseCase.java @@ -0,0 +1,25 @@ +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); + } +}