From 7ca1935c28518bfc004b054f0ad873a15320469c Mon Sep 17 00:00:00 2001 From: TaeyeonRoyce Date: Fri, 17 May 2024 12:55:24 +0900 Subject: [PATCH] =?UTF-8?q?enhancement:=20=EC=A3=BC=EB=AC=B8=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=20=EC=A1=B0=ED=9A=8C=EC=8B=9C=20=EB=B0=9C=EC=83=9D?= =?UTF-8?q?=ED=95=98=EB=8A=94=20N+1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../petqua/application/order/OrderService.kt | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/com/petqua/application/order/OrderService.kt b/src/main/kotlin/com/petqua/application/order/OrderService.kt index 17941644..8c68352c 100644 --- a/src/main/kotlin/com/petqua/application/order/OrderService.kt +++ b/src/main/kotlin/com/petqua/application/order/OrderService.kt @@ -18,6 +18,7 @@ import com.petqua.domain.order.OrderPayment import com.petqua.domain.order.OrderPaymentRepository import com.petqua.domain.order.OrderRepository import com.petqua.domain.order.OrderShippingAddress +import com.petqua.domain.order.OrderStatus import com.petqua.domain.order.ShippingAddress import com.petqua.domain.order.ShippingAddressRepository import com.petqua.domain.order.ShippingNumber @@ -155,19 +156,18 @@ class OrderService( } private fun orderProductResponsesFromOrders(orders: List): List { - val statusByOrderId = orders.map { orderPaymentRepository.findOrderStatusByOrderId(it.id) } - .associateBy { orderPayment -> orderPayment.orderId } - .mapValues { it.value.status } - return orders.map { OrderProductResponse(it, statusByOrderId.getOrThrow(it.id)) } + val orderStatusByOrderId = orderStatusByOrders(orders) + return orders.map { OrderProductResponse(it, orderStatusByOrderId.getOrThrow(it.id)) } } @Transactional(readOnly = true) fun readAll(query: OrderReadQuery): OrdersResponse { validateOrderReadQuery(query) val orders = orderRepository.findOrdersByMemberId(query.memberId, query.toOrderPaging()) - orders.forEach { it.validateOwner(query.memberId) } - val ordersByOrderNumber = orders.groupBy { it.orderNumber }.toMutableMap() - val orderDetails = ordersByOrderNumber.mapValues { orderDetailResponseFromOrders(it.value) } + val ordersByOrderNumber = orders.groupBy { it.orderNumber } + val orderDetails = ordersByOrderNumber.mapValues { + orderDetailResponseFromOrders(it.value) + } return OrdersResponse.of(orderDetails.values.toList(), query.limit) } @@ -183,11 +183,13 @@ class OrderService( } private fun orderDetailResponseFromOrders(orders: List): OrderDetailResponse { + val orderStatusByOrderId = orderStatusByOrders(orders) val representativeOrder = orders[0] val orderProductResponses = orders.map { - val orderStatus = orderPaymentRepository.findOrderStatusByOrderId(it.id) - OrderProductResponse(it, orderStatus.status) + val orderStatus = orderStatusByOrderId.getOrThrow(it.id) + OrderProductResponse(it, orderStatus) } + return OrderDetailResponse( orderNumber = representativeOrder.orderNumber.value, orderedAt = representativeOrder.createdAt, @@ -195,4 +197,10 @@ class OrderService( totalAmount = representativeOrder.totalAmount, ) } + + private fun orderStatusByOrders(orders: List): Map { + val orderIds = orders.map { it.id } + return orderPaymentRepository.findOrderStatusByOrderIds(orderIds) + .associateBy({ it.orderId }, { it.status }) + } }