From bbb3fe1a9de6790be14d6b39d07f522976d19067 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 31 Oct 2023 16:23:51 +0900 Subject: [PATCH] fix: modify pending-delete-method by pending-id --- .../vouchers/service/PendingVoucherService.java | 13 ++++++++----- .../vouchers/service/VoucherSaveService.java | 10 +++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/PendingVoucherService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/PendingVoucherService.java index 21ea98dd..c137bdb5 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/PendingVoucherService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/PendingVoucherService.java @@ -1,5 +1,7 @@ package org.swmaestro.repl.gifthub.vouchers.service; +import java.util.Optional; + import org.springframework.stereotype.Service; import org.swmaestro.repl.gifthub.auth.entity.User; import org.swmaestro.repl.gifthub.exception.BusinessException; @@ -14,20 +16,21 @@ public class PendingVoucherService { private final PendingVoucherRepository pendingVoucherRepository; - public void create(User member) { + public Long create(User member) { PendingVoucher pendingVoucher = PendingVoucher.builder() .user(member) .build(); pendingVoucherRepository.save(pendingVoucher); + return pendingVoucher.getId(); } - public void delete(User user) { - PendingVoucher pendingVoucher = pendingVoucherRepository.findByUserId(user.getId()); + public void delete(Long id) { + Optional pendingVoucher = pendingVoucherRepository.findById(id); //예외 처리 - if (pendingVoucher == null) { + if (pendingVoucher.isEmpty()) { throw new BusinessException("PendingVoucher가 존재하지 않습니다.", StatusEnum.NOT_FOUND); } - pendingVoucherRepository.delete(pendingVoucher); + pendingVoucherRepository.delete(pendingVoucher.get()); } public int count(Long memberId) { diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java index f93099b7..1f9d04b4 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java @@ -38,7 +38,7 @@ public class VoucherSaveService { private final PendingVoucherService pendingVoucherService; public void execute(OCRDto ocrDto, String username) throws IOException { - pendingVoucherService.create(userService.read(username)); + Long pendingId = pendingVoucherService.create(userService.read(username)); handleGptResponse(ocrDto, username) .flatMap(voucherSaveRequestDto -> handleSearchResponse(voucherSaveRequestDto, username)) .flatMap(voucherSaveRequestDto -> handleVoucherSaving(voucherSaveRequestDto, username)) @@ -46,6 +46,8 @@ public void execute(OCRDto ocrDto, String username) throws IOException { // onSuccess voucherSaveResponseDto -> { System.out.println("등록 성공"); + // 처리 완료 + pendingVoucherService.delete(pendingId); // 만료된 기프티콘을 등록할 경우 if (voucherService.read(voucherSaveResponseDto.getId()).getExpiresAt().isBefore(LocalDate.now())) { fcmNotificationService.sendNotification("기프티콘 등록 성공", "만료된 기프티콘을 등록했습니다.", username); @@ -58,12 +60,12 @@ public void execute(OCRDto ocrDto, String username) throws IOException { NotificationType.REGISTERED, "기프티콘 등록에 성공했습니다."); } - // 처리 완료 - pendingVoucherService.delete(userService.read(username)); }, // onError throwable -> { System.out.println("등록 실패"); + // 처리 완료 + pendingVoucherService.delete(pendingId); throwable.printStackTrace(); //Gpt 에러일 경우 if (throwable instanceof GptResponseException) { @@ -77,8 +79,6 @@ public void execute(OCRDto ocrDto, String username) throws IOException { NotificationType.REGISTERED, "이미 등록된 기프티콘 입니다."); } - // 처리 완료 - pendingVoucherService.delete(userService.read(username)); }); }