📌 ???

🔍 Controller

@GetMapping("/user/order")
public ResponseEntity<List<ResponseOrder>> read_order(HttpServletRequest request){
    List<ResponseOrder> responseOrderList = orderService.read_order((User) request.getAttribute("user"));

    return (responseOrderList != null) ?
            ResponseEntity.status(HttpStatus.OK).body(responseOrderList) :
            ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}

OrderServiceread_order함수를 호출하고 결과에 따라 200 또는 400의 status code를 반환

🔍 Service

@Override
@Transactional
public List<ResponseOrder> read_order(User user) {
    User userPersisted = entityManager.find(User.class, user.getId());
    if(userPersisted == null){
        return null;
    }

    List<Order> orderList = userPersisted.getOrderList();
    if (orderList == null) {
        return null;
    }

    List<ResponseOrder> result = new ArrayList<>();

    for(Order order : orderList) {
        List<OrderProduct> orderProductList = order.getOrderProductList();

        List<ReadOrderQuery> readOrderQueryList = orderProductRepository.findResponseOrder(orderProductList);
        if (readOrderQueryList == null){
            continue;
        }

        result.addAll(readOrderQueryList
                .stream().map(readOrderQuery -> ResponseOrder
                        .builder()
                        .order(order)
                        .readOrderQuery(readOrderQuery)
                        .build()).toList());
    }

    return result;
}

파라미터로 받아온 User는 준영속 객체이므로 User 의 프록시 객체를 참조하면 Lazyinitializationexception 가 발생 → EntityManager로 영속화시킴

UserOrder는 1대N 양방향 매핑 관계임

OrderOrderProduct 또한 1대N 양방향 매핑 관계임

해당 UserOrderList를 사용해 Order와 연관된 OrderProduct를 조회

🔍 Repository

@Override
public List<ReadOrderQuery> findResponseOrder(List<OrderProduct> orderProductList) {
    BooleanExpression status = eqOrderIDList(orderProductList.stream().map(OrderProduct::getId).toList());

    if(status == null){
        return null;
    }

    return queryFactory.select(
            Projections.fields(ReadOrderQuery.class,
                    orderProduct.product.id.as("product_id"),
                    orderProduct.count,
                    orderProduct.product.price,
                    orderProduct.product.name,
                    orderProduct.size,
                    orderProduct.product.imgKey,
                    new CaseBuilder()
                            .when(orderProduct.product.stock.gt(0))
                            .then(true)
                            .otherwise(false).as("stock_zero")
            ))
            .from(orderProduct)
            .innerJoin(orderProduct.product, product)
            .where(status)
            .fetch();
}

OrderProduct의 필요한 부분만 DTO로 만들어 조회

CaseBuilderProductstock이 0이하 일때 true, 아니라면 false를 담게함