Skip to content

Commit

Permalink
refactor: 검증 메서드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
Ho-Tea committed Jul 25, 2024
1 parent d5edc1b commit 27e2153
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -88,10 +92,14 @@ private TravelResponses getTravelDetailResponses(List<TravelMember> 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("해당 여행 상세에 방문 기록이 남아있어 삭제할 수 없습니다.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public interface VisitRepository extends JpaRepository<Visit, Long> {
@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);
}

0 comments on commit 27e2153

Please sign in to comment.