From 27e21539299f300c981dd0e6879b9800e9b96442 Mon Sep 17 00:00:00 2001 From: yoonjuho Date: Thu, 25 Jul 2024 14:10:13 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EA=B2=80=EC=A6=9D=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/staccato/travel/service/TravelService.java | 14 +++++++++++--- .../staccato/visit/repository/VisitRepository.java | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/com/staccato/travel/service/TravelService.java b/backend/src/main/java/com/staccato/travel/service/TravelService.java index cdae8ba38..ecac0c26a 100644 --- a/backend/src/main/java/com/staccato/travel/service/TravelService.java +++ b/backend/src/main/java/com/staccato/travel/service/TravelService.java @@ -16,6 +16,8 @@ import com.staccato.travel.service.dto.request.TravelRequest; import com.staccato.travel.service.dto.response.TravelResponses; import com.staccato.visit.domain.Visit; +import com.staccato.visit.repository.VisitImageRepository; +import com.staccato.visit.repository.VisitLogRepository; import com.staccato.visit.repository.VisitRepository; import lombok.RequiredArgsConstructor; @@ -28,6 +30,8 @@ public class TravelService { private final TravelMemberRepository travelMemberRepository; private final VisitRepository visitRepository; private final MemberRepository memberRepository; + private final VisitImageRepository visitImageRepository; + private final VisitLogRepository visitLogRepository; @Transactional public long createTravel(TravelRequest travelRequest, Long memberId) { @@ -88,10 +92,14 @@ private TravelResponses getTravelDetailResponses(List travelMember @Transactional public void deleteTravel(Long travelId) { - if (!visitRepository.findAllByTravelId(travelId).isEmpty()) { - throw new StaccatoException("해당 여행 상세에 방문 기록이 남아있어 삭제할 수 없습니다."); - } + validateVisitExistsByTravelId(travelId); visitRepository.deleteByTravelId(travelId); travelRepository.deleteById(travelId); } + + private void validateVisitExistsByTravelId(Long travelId) { + if (visitRepository.existsByTravelId(travelId)) { + throw new StaccatoException("해당 여행 상세에 방문 기록이 남아있어 삭제할 수 없습니다."); + } + } } diff --git a/backend/src/main/java/com/staccato/visit/repository/VisitRepository.java b/backend/src/main/java/com/staccato/visit/repository/VisitRepository.java index 4b9d15e4b..e156a8594 100644 --- a/backend/src/main/java/com/staccato/visit/repository/VisitRepository.java +++ b/backend/src/main/java/com/staccato/visit/repository/VisitRepository.java @@ -15,4 +15,7 @@ public interface VisitRepository extends JpaRepository { @Modifying(clearAutomatically = true) @Query("UPDATE Visit vi SET vi.isDeleted = true WHERE vi.travel.id = :travelId") void deleteByTravelId(@Param("travelId") Long travelId); + + @Query("SELECT CASE WHEN COUNT(v) > 0 THEN true ELSE false END FROM Visit v WHERE v.travel.id = :travelId") + boolean existsByTravelId(@Param("travelId") Long travelId); }