Skip to content

Commit

Permalink
realized function to get order items by order id
Browse files Browse the repository at this point in the history
  • Loading branch information
fedorovychh committed Jan 3, 2024
1 parent bed87ee commit 9eeeb25
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<OrderItem> getAllById(@PathVariable Long id) {
return orderItemService.getAllById(id);
}

@PreAuthorize("hasRole('ROLE_USER')")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<OrderItem, Long> {
@Query("SELECT oi FROM OrderItem oi "
+ "JOIN FETCH oi.order "
+ "JOIN FETCH oi.book "
+ "WHERE oi.order.id = :id")
List<OrderItem> findAllById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
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;

@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {
private final OrderRepository orderRepository;
private final OrderItemRepository orderItemRepository;
private final OrderMapper orderMapper;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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<OrderItem> getAllById(Long id);
}
Original file line number Diff line number Diff line change
@@ -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<OrderItem> getAllById(Long id) {
return orderItemRepository.findAllById(id);
}
}

0 comments on commit 9eeeb25

Please sign in to comment.