diff --git a/nowait-app-user-api/src/main/java/com/nowait/applicationuser/reservation/service/ReservationService.java b/nowait-app-user-api/src/main/java/com/nowait/applicationuser/reservation/service/ReservationService.java index ca69a598..2416e0d5 100644 --- a/nowait-app-user-api/src/main/java/com/nowait/applicationuser/reservation/service/ReservationService.java +++ b/nowait-app-user-api/src/main/java/com/nowait/applicationuser/reservation/service/ReservationService.java @@ -1,5 +1,7 @@ package com.nowait.applicationuser.reservation.service; +import static com.nowait.common.exception.ErrorMessage.*; + import java.time.Duration; import java.time.Instant; import java.time.LocalDateTime; @@ -215,38 +217,42 @@ public WaitingResponseDto myWaitingInfo(Long storeId, CustomOAuth2User customOAu public boolean cancelWaiting(Long storeId, CustomOAuth2User customOAuth2User) { String userId = customOAuth2User.getUserId().toString(); - if (storeId == null || userId.trim().isEmpty()) { - throw new IllegalArgumentException("Invalid storeId or userId"); + if (storeId == null) { + throw new StoreNotFoundException(); + } + + if (userId.trim().isEmpty()) { + throw new UserNotFoundException(); } String reservationNumber = waitingUserRedisRepository.getReservationId(storeId, userId); if (reservationNumber == null) { - throw new IllegalArgumentException("Waiting not found"); + throw new ReservationNotFoundException(); } Integer partySize = waitingUserRedisRepository.getPartySize(storeId, userId); Long ts = waitingUserRedisRepository.getWaitingTimestamp(storeId, userId); + // 대기열에서 제거 및 결과 반환 - boolean removed = waitingUserRedisRepository.removeWaiting(storeId, userId); - if (!removed) { - throw new IllegalArgumentException("Waiting not found"); - } + if (reservationRepository.existsReservationByReservationNumber(reservationNumber)) { + Reservation reservation = Reservation.builder() + .reservationNumber(reservationNumber) + .partySize(partySize) + .status(ReservationStatus.CANCELLED) + .store(storeRepository.getReferenceById(storeId)) + .user(userRepository.getReferenceById(Long.parseLong(userId))) + .updatedAt(LocalDateTime.now()) + .requestedAt(ts != null ? LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.of("Asia/Seoul")) + : LocalDateTime.now()) + .build(); - Reservation reservation = Reservation.builder() - .reservationNumber(reservationNumber) - .partySize(partySize) - .status(ReservationStatus.CANCELLED) - .store(storeRepository.getReferenceById(storeId)) - .user(userRepository.getReferenceById(Long.parseLong(userId))) - .updatedAt(LocalDateTime.now()) - .requestedAt(ts != null ? LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.of("Asia/Seoul")) - : LocalDateTime.now()) - .build(); + reservationRepository.save(reservation); + } - reservationRepository.save(reservation); + waitingUserRedisRepository.removeWaiting(storeId, userId); waitingPermitLuaRepository.removeActiveMember(userId, String.valueOf(storeId), reservationNumber); + return true; - // return removed; } //TODO 성능 개선 필요 diff --git a/nowait-domain/domain-core-rdb/src/main/java/com/nowait/domaincorerdb/reservation/repository/ReservationRepository.java b/nowait-domain/domain-core-rdb/src/main/java/com/nowait/domaincorerdb/reservation/repository/ReservationRepository.java index 17e8c36b..5512a5f1 100644 --- a/nowait-domain/domain-core-rdb/src/main/java/com/nowait/domaincorerdb/reservation/repository/ReservationRepository.java +++ b/nowait-domain/domain-core-rdb/src/main/java/com/nowait/domaincorerdb/reservation/repository/ReservationRepository.java @@ -18,6 +18,8 @@ public interface ReservationRepository extends JpaRepository boolean existsByUserAndStoreAndStatusIn(User user, Store store, List statuses); + boolean existsReservationByReservationNumber(String reservationNumber); + Optional findFirstByStore_StoreIdAndUserIdAndStatusInAndRequestedAtBetweenOrderByRequestedAtDesc( Long storeId, Long userId, List statuses, LocalDateTime start, LocalDateTime end);