Skip to content

Commit 76aaf3b

Browse files
authored
feat(be): 진행중/완료 상태인 주문 조회 API 추가 (#282)
1 parent 093b0ac commit 76aaf3b

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

backend/src/main/java/com/deliveranything/domain/order/controller/CustomerOrderController.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.swagger.v3.oas.annotations.Operation;
1515
import io.swagger.v3.oas.annotations.tags.Tag;
1616
import jakarta.validation.Valid;
17+
import java.util.List;
1718
import lombok.RequiredArgsConstructor;
1819
import org.springframework.http.ResponseEntity;
1920
import org.springframework.security.access.prepost.PreAuthorize;
@@ -69,6 +70,29 @@ public ResponseEntity<ApiResponse<OrderResponse>> get(
6970
customerOrderService.getCustomerOrder(orderId, securityUser.getId())));
7071
}
7172

73+
@GetMapping("/in-progress")
74+
@Operation(summary = "진행중인 주문 조회", description = "소비자가 진행중인 주문 내역을 요청한 경우")
75+
@PreAuthorize("@profileSecurity.isCustomer(#securityUser)")
76+
public ResponseEntity<ApiResponse<List<OrderResponse>>> getInProgressOrders(
77+
@AuthenticationPrincipal SecurityUser securityUser
78+
) {
79+
return ResponseEntity.ok().body(ApiResponse.success("진행중인 소비자 주문 조회 성공",
80+
customerOrderService.getProgressingOrders(securityUser.getId())));
81+
}
82+
83+
@GetMapping("/completed")
84+
@Operation(summary = "배달 완료된 주문 조회", description = "소비자가 배달 완료된 주문 내역을 요청한 경우")
85+
@PreAuthorize("@profileSecurity.isCustomer(#securityUser)")
86+
public ResponseEntity<ApiResponse<CursorPageResponse<OrderResponse>>> getCompletedOrders(
87+
@AuthenticationPrincipal SecurityUser securityUser,
88+
@RequestParam(required = false) String nextPageToken,
89+
@RequestParam(defaultValue = "10") int size
90+
) {
91+
return ResponseEntity.ok().body(ApiResponse.success("배달 완료된 소비자 주문 조회 성공",
92+
customerOrderService.getCompletedOrdersByCursor(securityUser.getId(), nextPageToken,
93+
size)));
94+
}
95+
7296
@PostMapping("/{merchantUid}/pay")
7397
@Operation(summary = "주문 결제", description = "소비자가 생성한 주문의 결제 시도")
7498
@PreAuthorize("@profileSecurity.isCustomer(#securityUser)")

backend/src/main/java/com/deliveranything/domain/order/repository/OrderRepository.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,14 @@ public interface OrderRepository extends JpaRepository<Order, Long> {
3737
ORDER BY o.createdAt ASC
3838
""")
3939
List<Order> findOrdersWithStoreByStoreIdAndStatuses(Long storeId, List<OrderStatus> statuses);
40+
41+
@Query("""
42+
SELECT o
43+
FROM Order o
44+
JOIN FETCH o.store s
45+
WHERE s.id = :customerId AND o.status IN :statuses
46+
ORDER BY o.createdAt DESC
47+
""")
48+
List<Order> findOrdersWithStoreByCustomerIdAndStatuses(Long customerId,
49+
List<OrderStatus> statuses);
4050
}

backend/src/main/java/com/deliveranything/domain/order/repository/OrderRepositoryCustom.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ public List<Order> findOrdersWithStoreByStoreId(Long storeId, List<OrderStatus>
4949
.fetch();
5050
}
5151

52+
public List<Order> findOrdersWithStoreByCustomerId(Long customerId, List<OrderStatus> statuses,
53+
LocalDateTime lastCreatedAt, Long lastOrderId, int size) {
54+
QOrder order = QOrder.order;
55+
QStore store = QStore.store;
56+
57+
return queryFactory.selectFrom(order)
58+
.join(order.store, store).fetchJoin()
59+
.where(
60+
order.customer.id.eq(customerId),
61+
statusIn(statuses),
62+
storeCursorCondition(lastCreatedAt, lastOrderId)
63+
)
64+
.orderBy(order.createdAt.desc(), order.id.desc())
65+
.limit(size)
66+
.fetch();
67+
}
68+
5269
private BooleanExpression statusIn(List<OrderStatus> statuses) {
5370
return statuses != null && !statuses.isEmpty() ? QOrder.order.status.in(statuses) : null;
5471
}

backend/src/main/java/com/deliveranything/domain/order/service/CustomerOrderService.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.deliveranything.domain.order.dto.OrderResponse;
66
import com.deliveranything.domain.order.entity.Order;
77
import com.deliveranything.domain.order.entity.OrderItem;
8+
import com.deliveranything.domain.order.enums.OrderStatus;
89
import com.deliveranything.domain.order.event.OrderCreatedEvent;
910
import com.deliveranything.domain.order.repository.OrderRepository;
1011
import com.deliveranything.domain.order.repository.OrderRepositoryCustom;
@@ -14,7 +15,9 @@
1415
import com.deliveranything.global.common.CursorPageResponse;
1516
import com.deliveranything.global.exception.CustomException;
1617
import com.deliveranything.global.exception.ErrorCode;
18+
import com.deliveranything.global.util.CursorUtil;
1719
import com.deliveranything.global.util.PointUtil;
20+
import java.time.LocalDateTime;
1821
import java.util.List;
1922
import lombok.RequiredArgsConstructor;
2023
import org.springframework.context.ApplicationEventPublisher;
@@ -95,4 +98,51 @@ public OrderResponse getCustomerOrder(Long orderId, Long customerId) {
9598
orderRepository.findOrderWithStoreByIdAndCustomerId(orderId, customerId)
9699
.orElseThrow(() -> new CustomException(ErrorCode.CUSTOMER_ORDER_NOT_FOUND)));
97100
}
101+
102+
@Transactional(readOnly = true)
103+
public List<OrderResponse> getProgressingOrders(Long customerId) {
104+
return orderRepository.findOrdersWithStoreByCustomerIdAndStatuses(customerId, List.of(
105+
OrderStatus.PENDING, OrderStatus.PREPARING, OrderStatus.RIDER_ASSIGNED,
106+
OrderStatus.DELIVERING)).stream()
107+
.map(OrderResponse::from)
108+
.toList();
109+
}
110+
111+
@Transactional(readOnly = true)
112+
public CursorPageResponse<OrderResponse> getCompletedOrdersByCursor(
113+
Long customerId,
114+
String nextPageToken,
115+
int size
116+
) {
117+
LocalDateTime lastCreatedAt = null;
118+
Long lastOrderId = null;
119+
String[] decodedParts = CursorUtil.decode(nextPageToken);
120+
121+
if (decodedParts != null && decodedParts.length == 2) {
122+
try {
123+
lastCreatedAt = LocalDateTime.parse(decodedParts[0]);
124+
lastOrderId = Long.parseLong(decodedParts[1]);
125+
} catch (NumberFormatException e) {
126+
lastCreatedAt = null;
127+
lastOrderId = null;
128+
}
129+
}
130+
131+
List<Order> cursorOrders = orderRepositoryCustom.findOrdersWithStoreByCustomerId(customerId,
132+
List.of(OrderStatus.COMPLETED), lastCreatedAt, lastOrderId, size + 1);
133+
134+
List<OrderResponse> cursorResponses = cursorOrders.stream()
135+
.limit(size)
136+
.map(OrderResponse::from)
137+
.toList();
138+
139+
boolean hasNext = cursorOrders.size() > size;
140+
OrderResponse lastResponse = cursorResponses.getLast();
141+
142+
return new CursorPageResponse<>(
143+
cursorResponses,
144+
hasNext ? CursorUtil.encode(lastResponse.createdAt(), lastResponse.id()) : null,
145+
hasNext
146+
);
147+
}
98148
}

0 commit comments

Comments
 (0)