Skip to content

Commit

Permalink
fix: modify pending-delete-method by pending-id
Browse files Browse the repository at this point in the history
  • Loading branch information
inh2613 committed Oct 31, 2023
1 parent 07323e7 commit bbb3fe1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ 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))
.subscribe(
// onSuccess
voucherSaveResponseDto -> {
System.out.println("등록 성공");
// 처리 완료
pendingVoucherService.delete(pendingId);
// 만료된 기프티콘을 등록할 경우
if (voucherService.read(voucherSaveResponseDto.getId()).getExpiresAt().isBefore(LocalDate.now())) {
fcmNotificationService.sendNotification("기프티콘 등록 성공", "만료된 기프티콘을 등록했습니다.", username);
Expand All @@ -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) {
Expand All @@ -77,8 +79,6 @@ public void execute(OCRDto ocrDto, String username) throws IOException {
NotificationType.REGISTERED,
"이미 등록된 기프티콘 입니다.");
}
// 처리 완료
pendingVoucherService.delete(userService.read(username));
});
}

Expand Down

0 comments on commit bbb3fe1

Please sign in to comment.