From 9eeeb25aa7dfaa5c4b516d28fd9f716934a0eb44 Mon Sep 17 00:00:00 2001 From: Valerii Fedorovych Date: Wed, 3 Jan 2024 12:54:36 +0200 Subject: [PATCH] realized function to get order items by order id --- .../bookstore/controller/OrderController.java | 7 +++++-- .../order/item/OrderItemRepository.java | 15 ++++++++++++++ .../bookstore/service/order/OrderService.java | 1 + .../service/order/OrderServiceImpl.java | 4 ++++ .../service/order/item/OrderItemService.java | 10 ++++++++++ .../order/item/OrderItemServiceImpl.java | 20 +++++++++++++++++++ 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/app/bookstore/repository/order/item/OrderItemRepository.java create mode 100644 src/main/java/com/app/bookstore/service/order/item/OrderItemService.java create mode 100644 src/main/java/com/app/bookstore/service/order/item/OrderItemServiceImpl.java diff --git a/src/main/java/com/app/bookstore/controller/OrderController.java b/src/main/java/com/app/bookstore/controller/OrderController.java index fe3f282..7ecfcd7 100644 --- a/src/main/java/com/app/bookstore/controller/OrderController.java +++ b/src/main/java/com/app/bookstore/controller/OrderController.java @@ -4,8 +4,10 @@ import com.app.bookstore.dto.order.OrderResponseDto; import com.app.bookstore.dto.order.UpdateOrderRequestDto; import com.app.bookstore.model.Order; +import com.app.bookstore.model.OrderItem; import com.app.bookstore.model.User; import com.app.bookstore.service.order.OrderService; +import com.app.bookstore.service.order.item.OrderItemService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import java.util.List; @@ -26,6 +28,7 @@ @Tag(name = "Order management", description = "Endpoints for managing orders") public class OrderController { private final OrderService orderService; + private final OrderItemService orderItemService; @PreAuthorize("hasRole('ROLE_USER')") @PostMapping @@ -57,8 +60,8 @@ public OrderResponseDto updateStatus( summary = "Get all items by order id", description = "Retrieve all OrderItems for a specific order" ) - public Object getAllById(@PathVariable Long id) { - return null; + public List getAllById(@PathVariable Long id) { + return orderItemService.getAllById(id); } @PreAuthorize("hasRole('ROLE_USER')") diff --git a/src/main/java/com/app/bookstore/repository/order/item/OrderItemRepository.java b/src/main/java/com/app/bookstore/repository/order/item/OrderItemRepository.java new file mode 100644 index 0000000..ecee952 --- /dev/null +++ b/src/main/java/com/app/bookstore/repository/order/item/OrderItemRepository.java @@ -0,0 +1,15 @@ +package com.app.bookstore.repository.order.item; + +import com.app.bookstore.model.OrderItem; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +public interface OrderItemRepository extends JpaRepository { + @Query("SELECT oi FROM OrderItem oi " + + "JOIN FETCH oi.order " + + "JOIN FETCH oi.book " + + "WHERE oi.order.id = :id") + List findAllById(Long id); +} diff --git a/src/main/java/com/app/bookstore/service/order/OrderService.java b/src/main/java/com/app/bookstore/service/order/OrderService.java index 6335beb..745769a 100644 --- a/src/main/java/com/app/bookstore/service/order/OrderService.java +++ b/src/main/java/com/app/bookstore/service/order/OrderService.java @@ -4,6 +4,7 @@ import com.app.bookstore.dto.order.OrderResponseDto; import com.app.bookstore.dto.order.UpdateOrderRequestDto; import com.app.bookstore.model.Order; + import java.util.List; public interface OrderService { diff --git a/src/main/java/com/app/bookstore/service/order/OrderServiceImpl.java b/src/main/java/com/app/bookstore/service/order/OrderServiceImpl.java index e963856..339f0d0 100644 --- a/src/main/java/com/app/bookstore/service/order/OrderServiceImpl.java +++ b/src/main/java/com/app/bookstore/service/order/OrderServiceImpl.java @@ -6,8 +6,11 @@ import com.app.bookstore.exception.EntityNotFoundException; import com.app.bookstore.mapper.OrderMapper; import com.app.bookstore.model.Order; +import com.app.bookstore.model.OrderItem; import com.app.bookstore.repository.order.OrderRepository; import java.util.List; + +import com.app.bookstore.repository.order.item.OrderItemRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -15,6 +18,7 @@ @RequiredArgsConstructor public class OrderServiceImpl implements OrderService { private final OrderRepository orderRepository; + private final OrderItemRepository orderItemRepository; private final OrderMapper orderMapper; @Override diff --git a/src/main/java/com/app/bookstore/service/order/item/OrderItemService.java b/src/main/java/com/app/bookstore/service/order/item/OrderItemService.java new file mode 100644 index 0000000..17f473b --- /dev/null +++ b/src/main/java/com/app/bookstore/service/order/item/OrderItemService.java @@ -0,0 +1,10 @@ +package com.app.bookstore.service.order.item; + +import com.app.bookstore.model.OrderItem; + +import java.util.List; + +public interface OrderItemService { + + List getAllById(Long id); +} diff --git a/src/main/java/com/app/bookstore/service/order/item/OrderItemServiceImpl.java b/src/main/java/com/app/bookstore/service/order/item/OrderItemServiceImpl.java new file mode 100644 index 0000000..4d1bd9d --- /dev/null +++ b/src/main/java/com/app/bookstore/service/order/item/OrderItemServiceImpl.java @@ -0,0 +1,20 @@ +package com.app.bookstore.service.order.item; + +import com.app.bookstore.model.OrderItem; +import com.app.bookstore.repository.order.OrderRepository; +import com.app.bookstore.repository.order.item.OrderItemRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@RequiredArgsConstructor +public class OrderItemServiceImpl implements OrderItemService { + private final OrderItemRepository orderItemRepository; + + @Override + public List getAllById(Long id) { + return orderItemRepository.findAllById(id); + } +}