Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,23 @@ List<Long> findPaymentIdsByUserAndStatus(
@Param("limit") int limit
);

@Query(value = """
SELECT p.id FROM payments p
WHERE p.user_id = :userId
AND p.status = :status
ORDER BY p.cancelled_at DESC
LIMIT :limit OFFSET :offset
""", nativeQuery = true)
List<Long> findCancelledPaymentIdsByUserAndStatus(
@Param("userId") Long userId,
@Param("status") String status,
@Param("offset") int offset,
@Param("limit") int limit
);

@Query("""
SELECT p FROM Payment p
LEFT JOIN FETCH p.lesson
LEFT JOIN FETCH p.userCoupon
WHERE p.id IN :ids
""")
List<Payment> findAllWithAssociationsByIds(@Param("ids") List<Long> ids);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.threestar.trainus.domain.payment.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.threestar.trainus.domain.payment.entity.TossPayment;

public interface TossPaymentRepository extends JpaRepository<TossPayment, Long> {

Optional<TossPayment> findByOrderId(String orderId);

@Query("""
SELECT t FROM TossPayment t
WHERE t.orderId IN :orderIds
""")
List<TossPayment> findAllByOrderIds(@Param("orderIds") List<String> orderIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,20 @@ public PaymentSuccessHistoryPageDto viewAllSuccessTransaction(Long userId, int p
.map(map::get)
.toList();

List<String> orderIds = allSuccessPayments.stream()
.map(Payment::getOrderId)
.toList();

Map<String, TossPayment> tossPaymentMap = tossPaymentRepository.findAllByOrderIds(orderIds)
.stream()
.collect(Collectors.toMap(TossPayment::getOrderId, t -> t));

List<PaymentSuccessHistoryResponseDto> dtoList = allSuccessPayments.stream()
.map(payment -> {
TossPayment tossPayment = tossPaymentRepository.findByOrderId(payment.getOrderId())
.orElseThrow(() -> new BusinessException(ErrorCode.INVALID_PAYMENT));
TossPayment tossPayment = tossPaymentMap.get(payment.getOrderId());
if (tossPayment == null) {
throw new BusinessException(ErrorCode.INVALID_PAYMENT);
}
return PaymentMapper.toPaymentSuccessHistoryResponseDto(payment, tossPayment);
})
.toList();
Expand All @@ -219,7 +229,7 @@ public PaymentSuccessHistoryPageDto viewAllSuccessTransaction(Long userId, int p

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

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

List<String> orderIds = allFailurePayments.stream()
.map(Payment::getOrderId)
.toList();

Map<String, TossPayment> tossPaymentMap = tossPaymentRepository.findAllByOrderIds(orderIds)
.stream()
.collect(Collectors.toMap(TossPayment::getOrderId, t -> t));

List<PaymentCancelHistoryResponseDto> dtoList = allFailurePayments.stream()
.map(payment -> {
TossPayment tossPayment = tossPaymentRepository.findByOrderId(payment.getOrderId())
.orElseThrow(() -> new BusinessException(ErrorCode.INVALID_PAYMENT));
TossPayment tossPayment = tossPaymentMap.get(payment.getOrderId());
if (tossPayment == null) {
throw new BusinessException(ErrorCode.INVALID_PAYMENT);
}
return PaymentMapper.toPaymentFailureHistoryResponseDto(payment, tossPayment);
})
.toList();
Expand Down