Skip to content

Commit d719ad9

Browse files
authored
Refactor/171/toss query fix (#173)
* refactor: 쿼리 관련 리팩토링 * feat: 서비스 추가
1 parent f475dc5 commit d719ad9

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/main/java/com/threestar/trainus/domain/payment/service/PaymentService.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,20 @@ public PaymentSuccessHistoryPageDto viewAllSuccessTransaction(Long userId, int p
203203
.map(map::get)
204204
.toList();
205205

206+
List<String> orderIds = allSuccessPayments.stream()
207+
.map(Payment::getOrderId)
208+
.toList();
209+
210+
Map<String, TossPayment> tossPaymentMap = tossPaymentRepository.findAllByOrderIds(orderIds)
211+
.stream()
212+
.collect(Collectors.toMap(TossPayment::getOrderId, t -> t));
213+
206214
List<PaymentSuccessHistoryResponseDto> dtoList = allSuccessPayments.stream()
207215
.map(payment -> {
208-
TossPayment tossPayment = tossPaymentRepository.findByOrderId(payment.getOrderId())
209-
.orElseThrow(() -> new BusinessException(ErrorCode.INVALID_PAYMENT));
216+
TossPayment tossPayment = tossPaymentMap.get(payment.getOrderId());
217+
if (tossPayment == null) {
218+
throw new BusinessException(ErrorCode.INVALID_PAYMENT);
219+
}
210220
return PaymentMapper.toPaymentSuccessHistoryResponseDto(payment, tossPayment);
211221
})
212222
.toList();
@@ -219,7 +229,7 @@ public PaymentSuccessHistoryPageDto viewAllSuccessTransaction(Long userId, int p
219229

220230
@Transactional(readOnly = true)
221231
public PaymentCancelHistoryPageDto viewAllFailureTransaction(Long userId, int page, int pageSize) {
222-
List<Long> paymentIds = paymentRepository.findPaymentIdsByUserAndStatus(userId,
232+
List<Long> paymentIds = paymentRepository.findCancelledPaymentIdsByUserAndStatus(userId,
223233
PaymentStatus.CANCELED.name(), (page - 1) * pageSize, pageSize);
224234

225235
if (paymentIds.isEmpty()) {
@@ -233,10 +243,20 @@ public PaymentCancelHistoryPageDto viewAllFailureTransaction(Long userId, int pa
233243
.map(map::get)
234244
.toList();
235245

246+
List<String> orderIds = allFailurePayments.stream()
247+
.map(Payment::getOrderId)
248+
.toList();
249+
250+
Map<String, TossPayment> tossPaymentMap = tossPaymentRepository.findAllByOrderIds(orderIds)
251+
.stream()
252+
.collect(Collectors.toMap(TossPayment::getOrderId, t -> t));
253+
236254
List<PaymentCancelHistoryResponseDto> dtoList = allFailurePayments.stream()
237255
.map(payment -> {
238-
TossPayment tossPayment = tossPaymentRepository.findByOrderId(payment.getOrderId())
239-
.orElseThrow(() -> new BusinessException(ErrorCode.INVALID_PAYMENT));
256+
TossPayment tossPayment = tossPaymentMap.get(payment.getOrderId());
257+
if (tossPayment == null) {
258+
throw new BusinessException(ErrorCode.INVALID_PAYMENT);
259+
}
240260
return PaymentMapper.toPaymentFailureHistoryResponseDto(payment, tossPayment);
241261
})
242262
.toList();

0 commit comments

Comments
 (0)