From 9f5d71f094c0b6ffedb3a5f86c6a4bcdb18bb426 Mon Sep 17 00:00:00 2001 From: Combi153 Date: Thu, 21 Sep 2023 22:24:52 +0900 Subject: [PATCH 1/6] =?UTF-8?q?[refactor]=20=ED=9A=8C=EC=9B=90=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EC=8B=9C=20=EC=BF=BC=EB=A6=AC=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=20(#398)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/TripDeleteEventHandler.java | 11 +++-- .../tripdraw/trip/domain/PointRepository.java | 8 ++++ .../tripdraw/trip/domain/TripRepository.java | 2 + .../TripDeleteEventHandlerTest.java | 13 ++++++ .../trip/domain/PointRepositoryTest.java | 45 +++++++++++++++++-- 5 files changed, 71 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/dev/tripdraw/trip/application/TripDeleteEventHandler.java b/backend/src/main/java/dev/tripdraw/trip/application/TripDeleteEventHandler.java index c23fbdfb3..51f425177 100644 --- a/backend/src/main/java/dev/tripdraw/trip/application/TripDeleteEventHandler.java +++ b/backend/src/main/java/dev/tripdraw/trip/application/TripDeleteEventHandler.java @@ -1,21 +1,24 @@ package dev.tripdraw.trip.application; import dev.tripdraw.member.domain.MemberDeleteEvent; +import dev.tripdraw.trip.domain.PointRepository; import dev.tripdraw.trip.domain.TripRepository; +import java.util.List; +import lombok.RequiredArgsConstructor; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; +@RequiredArgsConstructor @Component public class TripDeleteEventHandler { private final TripRepository tripRepository; - - public TripDeleteEventHandler(TripRepository tripRepository) { - this.tripRepository = tripRepository; - } + private final PointRepository pointRepository; @EventListener public void deletePostByMemberId(MemberDeleteEvent event) { + List tripIds = tripRepository.findAllTripIdsByMemberId(event.memberId()); + pointRepository.deleteByTripIds(tripIds); tripRepository.deleteByMemberId(event.memberId()); } } diff --git a/backend/src/main/java/dev/tripdraw/trip/domain/PointRepository.java b/backend/src/main/java/dev/tripdraw/trip/domain/PointRepository.java index ac4e94e36..aae34c2c2 100644 --- a/backend/src/main/java/dev/tripdraw/trip/domain/PointRepository.java +++ b/backend/src/main/java/dev/tripdraw/trip/domain/PointRepository.java @@ -3,7 +3,11 @@ import static dev.tripdraw.trip.exception.TripExceptionType.POINT_NOT_FOUND; import dev.tripdraw.trip.exception.TripException; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface PointRepository extends JpaRepository { @@ -11,4 +15,8 @@ default Point getById(Long id) { return findById(id) .orElseThrow(() -> new TripException(POINT_NOT_FOUND)); } + + @Modifying + @Query("DELETE FROM Point p WHERE p.trip.id IN :tripIds") + void deleteByTripIds(@Param(value = "tripIds") List tripIds); } diff --git a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java index edb45c393..f69f04db1 100644 --- a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java +++ b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java @@ -27,4 +27,6 @@ default Trip getTripWithPoints(Long tripId) { return findTripWithPoints(tripId) .orElseThrow(() -> new TripException(TRIP_NOT_FOUND)); } + + List findAllTripIdsByMemberId(Long memberId); } diff --git a/backend/src/test/java/dev/tripdraw/trip/application/TripDeleteEventHandlerTest.java b/backend/src/test/java/dev/tripdraw/trip/application/TripDeleteEventHandlerTest.java index 5ac2d23bf..88d35cf3c 100644 --- a/backend/src/test/java/dev/tripdraw/trip/application/TripDeleteEventHandlerTest.java +++ b/backend/src/test/java/dev/tripdraw/trip/application/TripDeleteEventHandlerTest.java @@ -1,10 +1,13 @@ package dev.tripdraw.trip.application; +import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.times; import dev.tripdraw.member.domain.MemberDeleteEvent; +import dev.tripdraw.trip.domain.PointRepository; import dev.tripdraw.trip.domain.TripRepository; +import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; @@ -21,6 +24,9 @@ class TripDeleteEventHandlerTest { @Mock private TripRepository tripRepository; + @Mock + private PointRepository pointRepository; + @InjectMocks private TripDeleteEventHandler tripDeleteEventHandler; @@ -29,6 +35,10 @@ class TripDeleteEventHandlerTest { // given MemberDeleteEvent memberDeleteEvent = new MemberDeleteEvent(1L); + List tripIds = List.of(1L); + given(tripRepository.findAllTripIdsByMemberId(memberDeleteEvent.memberId())) + .willReturn(tripIds); + // when tripDeleteEventHandler.deletePostByMemberId(memberDeleteEvent); @@ -36,5 +46,8 @@ class TripDeleteEventHandlerTest { then(tripRepository) .should(times(1)) .deleteByMemberId(1L); + then(pointRepository) + .should(times(1)) + .deleteByTripIds(tripIds); } } diff --git a/backend/src/test/java/dev/tripdraw/trip/domain/PointRepositoryTest.java b/backend/src/test/java/dev/tripdraw/trip/domain/PointRepositoryTest.java index ea3da0513..5fff89f8b 100644 --- a/backend/src/test/java/dev/tripdraw/trip/domain/PointRepositoryTest.java +++ b/backend/src/test/java/dev/tripdraw/trip/domain/PointRepositoryTest.java @@ -4,6 +4,7 @@ import static dev.tripdraw.trip.exception.TripExceptionType.POINT_NOT_FOUND; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.SoftAssertions.assertSoftly; import static org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace.NONE; import dev.tripdraw.common.config.JpaConfig; @@ -12,6 +13,7 @@ import dev.tripdraw.member.domain.MemberRepository; import dev.tripdraw.trip.exception.TripException; import java.time.LocalDateTime; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -37,19 +39,22 @@ class PointRepositoryTest { @Autowired private MemberRepository memberRepository; - private Trip trip; + private Trip huchuTrip; + private Trip herbTrip; @BeforeEach void setUp() { - Member member = memberRepository.save(new Member("통후추", "kakaoId", KAKAO)); - trip = tripRepository.save(Trip.from(member)); + Member huchu = memberRepository.save(new Member("후추", "kakaoId", KAKAO)); + Member herb = memberRepository.save(new Member("허브", "kakaoId", KAKAO)); + huchuTrip = tripRepository.save(Trip.from(huchu)); + herbTrip = tripRepository.save(Trip.from(herb)); } @Test void 위치정보_ID로_위치정보를_조회한다() { // given Point point = new Point(3.14, 5.25, LocalDateTime.now()); - point.setTrip(trip); + point.setTrip(huchuTrip); pointRepository.save(point); // when @@ -69,4 +74,36 @@ void setUp() { .isInstanceOf(TripException.class) .hasMessage(POINT_NOT_FOUND.message()); } + + @Test + void 여행_ID_목록으로_위치정보를_삭제한다() { + // given + Point huchuFirstPoint = new Point(3.14, 5.25, LocalDateTime.now()); + huchuFirstPoint.setTrip(huchuTrip); + Point huchuSecondPoint = new Point(3.14, 5.25, LocalDateTime.now()); + huchuSecondPoint.setTrip(huchuTrip); + + Point herbFirstPoint = new Point(3.14, 5.25, LocalDateTime.now()); + herbFirstPoint.setTrip(herbTrip); + Point herbSecondPoint = new Point(3.14, 5.25, LocalDateTime.now()); + herbSecondPoint.setTrip(herbTrip); + + pointRepository.save(huchuFirstPoint); + pointRepository.save(huchuSecondPoint); + pointRepository.save(herbFirstPoint); + pointRepository.save(herbSecondPoint); + + List tripIds = List.of(huchuTrip.id(), herbTrip.id()); + + // when + pointRepository.deleteByTripIds(tripIds); + + // then + assertSoftly(softly -> { + softly.assertThat(pointRepository.existsById(huchuFirstPoint.id())).isFalse(); + softly.assertThat(pointRepository.existsById(huchuSecondPoint.id())).isFalse(); + softly.assertThat(pointRepository.existsById(herbFirstPoint.id())).isFalse(); + softly.assertThat(pointRepository.existsById(herbSecondPoint.id())).isFalse(); + }); + } } From 5f60ccadae6aea2d5520d49d077525c6058acee1 Mon Sep 17 00:00:00 2001 From: Combi153 Date: Thu, 21 Sep 2023 22:36:28 +0900 Subject: [PATCH 2/6] =?UTF-8?q?[test]=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80=20(#398)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/tripdraw/trip/domain/TripRepository.java | 3 ++- .../tripdraw/trip/domain/TripRepositoryTest.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java index f69f04db1..38ff467f3 100644 --- a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java +++ b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java @@ -28,5 +28,6 @@ default Trip getTripWithPoints(Long tripId) { .orElseThrow(() -> new TripException(TRIP_NOT_FOUND)); } - List findAllTripIdsByMemberId(Long memberId); + @Query("SELECT t.id FROM Trip t WHERE t.member.id = :memberId") + List findAllTripIdsByMemberId(@Param(value = "memberId") Long memberId); } diff --git a/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java b/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java index a062430a4..6ebabe3bd 100644 --- a/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java +++ b/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java @@ -14,6 +14,7 @@ import dev.tripdraw.trip.exception.TripException; import java.time.LocalDateTime; import java.util.List; +import java.util.stream.IntStream; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -123,4 +124,18 @@ void setUp() { .isInstanceOf(TripException.class) .hasMessage(TRIP_NOT_FOUND.message()); } + + @Test + void 회원_ID를_갖는_모든_여행_ID를_조회한다() { + // given + List tripIds = IntStream.range(0, 5) + .mapToObj(value -> tripRepository.save(Trip.from(member)).id()) + .toList(); + + // when + List foundIds = tripRepository.findAllTripIdsByMemberId(member.id()); + + // then + assertThat(foundIds).isEqualTo(tripIds); + } } From 6cc528eef9b745a75e014db074acd945db4a1940 Mon Sep 17 00:00:00 2001 From: Combi153 Date: Sat, 23 Sep 2023 22:10:46 +0900 Subject: [PATCH 3/6] =?UTF-8?q?[refactor]=20=EA=B0=90=EC=83=81,=20?= =?UTF-8?q?=EC=97=AC=ED=96=89=20=EC=82=AD=EC=A0=9C=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20(#398)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/dev/tripdraw/post/domain/PostRepository.java | 8 +++++--- .../java/dev/tripdraw/trip/domain/TripRepository.java | 5 ++++- .../tripdraw/member/application/MemberServiceTest.java | 2 +- .../java/dev/tripdraw/post/domain/PostRepositoryTest.java | 4 ++-- .../java/dev/tripdraw/trip/domain/TripRepositoryTest.java | 2 +- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java b/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java index 7baa16bd7..b239b6db8 100644 --- a/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java +++ b/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java @@ -3,12 +3,12 @@ import static dev.tripdraw.post.exception.PostExceptionType.POST_NOT_FOUND; import dev.tripdraw.post.exception.PostException; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -import java.util.List; - public interface PostRepository extends JpaRepository { @Query("SELECT p FROM Post p JOIN FETCH p.point where p.tripId = :tripId") @@ -19,5 +19,7 @@ default Post getByPostId(Long id) { .orElseThrow(() -> new PostException(POST_NOT_FOUND)); } - void deleteByMemberId(Long memberId); + @Modifying + @Query("DELETE FROM Post p WHERE p.member.id = :memberId") + void deleteByMemberId(@Param(value = "memberId") Long memberId); } diff --git a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java index 38ff467f3..86446588f 100644 --- a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java +++ b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -13,7 +14,9 @@ public interface TripRepository extends JpaRepository { List findAllByMemberId(Long memberId); - void deleteByMemberId(Long memberId); + @Modifying + @Query("DELETE FROM Trip t WHERE t.member.id = :memberId") + void deleteByMemberId(@Param(value = "memberId") Long memberId); default Trip getById(Long id) { return findById(id) diff --git a/backend/src/test/java/dev/tripdraw/member/application/MemberServiceTest.java b/backend/src/test/java/dev/tripdraw/member/application/MemberServiceTest.java index d2a168944..220bbc7f6 100644 --- a/backend/src/test/java/dev/tripdraw/member/application/MemberServiceTest.java +++ b/backend/src/test/java/dev/tripdraw/member/application/MemberServiceTest.java @@ -50,7 +50,7 @@ void setUp() { member = memberRepository.save(new Member("통후추", "kakaoId", KAKAO)); trip = tripRepository.save(new Trip(TripName.from("통후추의 여행"), member)); Point point = new Point(3.14, 5.25, LocalDateTime.now()); - trip.add(point); + point.setTrip(trip); postRepository.save(new Post( "제목", point, diff --git a/backend/src/test/java/dev/tripdraw/post/domain/PostRepositoryTest.java b/backend/src/test/java/dev/tripdraw/post/domain/PostRepositoryTest.java index d6d3b6c25..e6dc7dddb 100644 --- a/backend/src/test/java/dev/tripdraw/post/domain/PostRepositoryTest.java +++ b/backend/src/test/java/dev/tripdraw/post/domain/PostRepositoryTest.java @@ -71,13 +71,13 @@ void setUp() { void 회원_ID로_감상을_삭제한다() { // given Post post = new Post("제목", point, "위치", "오늘은 날씨가 좋네요.", member, trip.id()); - postRepository.save(post); + Long postId = postRepository.save(post).id(); // when postRepository.deleteByMemberId(member.id()); // then - assertThat(postRepository.findById(post.id())).isEmpty(); + assertThat(postRepository.existsById(postId)).isFalse(); } @Test diff --git a/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java b/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java index 6ebabe3bd..0428aa357 100644 --- a/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java +++ b/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java @@ -99,7 +99,7 @@ void setUp() { tripRepository.deleteByMemberId(member.id()); // then - assertThat(tripRepository.findById(trip.id())).isEmpty(); + assertThat(tripRepository.existsById(trip.id())).isFalse(); } @Test From 06927806a77edb9eb6de90fc80dc84a71d8c3c87 Mon Sep 17 00:00:00 2001 From: Combi153 Date: Thu, 21 Sep 2023 22:24:52 +0900 Subject: [PATCH 4/6] =?UTF-8?q?[refactor]=20=ED=9A=8C=EC=9B=90=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EC=8B=9C=20=EC=BF=BC=EB=A6=AC=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=20(#398)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/TripDeleteEventHandler.java | 11 +++-- .../tripdraw/trip/domain/PointRepository.java | 8 ++++ .../tripdraw/trip/domain/TripRepository.java | 2 + .../TripDeleteEventHandlerTest.java | 13 ++++++ .../trip/domain/PointRepositoryTest.java | 45 +++++++++++++++++-- 5 files changed, 71 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/dev/tripdraw/trip/application/TripDeleteEventHandler.java b/backend/src/main/java/dev/tripdraw/trip/application/TripDeleteEventHandler.java index c23fbdfb3..51f425177 100644 --- a/backend/src/main/java/dev/tripdraw/trip/application/TripDeleteEventHandler.java +++ b/backend/src/main/java/dev/tripdraw/trip/application/TripDeleteEventHandler.java @@ -1,21 +1,24 @@ package dev.tripdraw.trip.application; import dev.tripdraw.member.domain.MemberDeleteEvent; +import dev.tripdraw.trip.domain.PointRepository; import dev.tripdraw.trip.domain.TripRepository; +import java.util.List; +import lombok.RequiredArgsConstructor; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; +@RequiredArgsConstructor @Component public class TripDeleteEventHandler { private final TripRepository tripRepository; - - public TripDeleteEventHandler(TripRepository tripRepository) { - this.tripRepository = tripRepository; - } + private final PointRepository pointRepository; @EventListener public void deletePostByMemberId(MemberDeleteEvent event) { + List tripIds = tripRepository.findAllTripIdsByMemberId(event.memberId()); + pointRepository.deleteByTripIds(tripIds); tripRepository.deleteByMemberId(event.memberId()); } } diff --git a/backend/src/main/java/dev/tripdraw/trip/domain/PointRepository.java b/backend/src/main/java/dev/tripdraw/trip/domain/PointRepository.java index ac4e94e36..aae34c2c2 100644 --- a/backend/src/main/java/dev/tripdraw/trip/domain/PointRepository.java +++ b/backend/src/main/java/dev/tripdraw/trip/domain/PointRepository.java @@ -3,7 +3,11 @@ import static dev.tripdraw.trip.exception.TripExceptionType.POINT_NOT_FOUND; import dev.tripdraw.trip.exception.TripException; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface PointRepository extends JpaRepository { @@ -11,4 +15,8 @@ default Point getById(Long id) { return findById(id) .orElseThrow(() -> new TripException(POINT_NOT_FOUND)); } + + @Modifying + @Query("DELETE FROM Point p WHERE p.trip.id IN :tripIds") + void deleteByTripIds(@Param(value = "tripIds") List tripIds); } diff --git a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java index edb45c393..f69f04db1 100644 --- a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java +++ b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java @@ -27,4 +27,6 @@ default Trip getTripWithPoints(Long tripId) { return findTripWithPoints(tripId) .orElseThrow(() -> new TripException(TRIP_NOT_FOUND)); } + + List findAllTripIdsByMemberId(Long memberId); } diff --git a/backend/src/test/java/dev/tripdraw/trip/application/TripDeleteEventHandlerTest.java b/backend/src/test/java/dev/tripdraw/trip/application/TripDeleteEventHandlerTest.java index 5ac2d23bf..88d35cf3c 100644 --- a/backend/src/test/java/dev/tripdraw/trip/application/TripDeleteEventHandlerTest.java +++ b/backend/src/test/java/dev/tripdraw/trip/application/TripDeleteEventHandlerTest.java @@ -1,10 +1,13 @@ package dev.tripdraw.trip.application; +import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.times; import dev.tripdraw.member.domain.MemberDeleteEvent; +import dev.tripdraw.trip.domain.PointRepository; import dev.tripdraw.trip.domain.TripRepository; +import java.util.List; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; @@ -21,6 +24,9 @@ class TripDeleteEventHandlerTest { @Mock private TripRepository tripRepository; + @Mock + private PointRepository pointRepository; + @InjectMocks private TripDeleteEventHandler tripDeleteEventHandler; @@ -29,6 +35,10 @@ class TripDeleteEventHandlerTest { // given MemberDeleteEvent memberDeleteEvent = new MemberDeleteEvent(1L); + List tripIds = List.of(1L); + given(tripRepository.findAllTripIdsByMemberId(memberDeleteEvent.memberId())) + .willReturn(tripIds); + // when tripDeleteEventHandler.deletePostByMemberId(memberDeleteEvent); @@ -36,5 +46,8 @@ class TripDeleteEventHandlerTest { then(tripRepository) .should(times(1)) .deleteByMemberId(1L); + then(pointRepository) + .should(times(1)) + .deleteByTripIds(tripIds); } } diff --git a/backend/src/test/java/dev/tripdraw/trip/domain/PointRepositoryTest.java b/backend/src/test/java/dev/tripdraw/trip/domain/PointRepositoryTest.java index ea3da0513..5fff89f8b 100644 --- a/backend/src/test/java/dev/tripdraw/trip/domain/PointRepositoryTest.java +++ b/backend/src/test/java/dev/tripdraw/trip/domain/PointRepositoryTest.java @@ -4,6 +4,7 @@ import static dev.tripdraw.trip.exception.TripExceptionType.POINT_NOT_FOUND; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.SoftAssertions.assertSoftly; import static org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace.NONE; import dev.tripdraw.common.config.JpaConfig; @@ -12,6 +13,7 @@ import dev.tripdraw.member.domain.MemberRepository; import dev.tripdraw.trip.exception.TripException; import java.time.LocalDateTime; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -37,19 +39,22 @@ class PointRepositoryTest { @Autowired private MemberRepository memberRepository; - private Trip trip; + private Trip huchuTrip; + private Trip herbTrip; @BeforeEach void setUp() { - Member member = memberRepository.save(new Member("통후추", "kakaoId", KAKAO)); - trip = tripRepository.save(Trip.from(member)); + Member huchu = memberRepository.save(new Member("후추", "kakaoId", KAKAO)); + Member herb = memberRepository.save(new Member("허브", "kakaoId", KAKAO)); + huchuTrip = tripRepository.save(Trip.from(huchu)); + herbTrip = tripRepository.save(Trip.from(herb)); } @Test void 위치정보_ID로_위치정보를_조회한다() { // given Point point = new Point(3.14, 5.25, LocalDateTime.now()); - point.setTrip(trip); + point.setTrip(huchuTrip); pointRepository.save(point); // when @@ -69,4 +74,36 @@ void setUp() { .isInstanceOf(TripException.class) .hasMessage(POINT_NOT_FOUND.message()); } + + @Test + void 여행_ID_목록으로_위치정보를_삭제한다() { + // given + Point huchuFirstPoint = new Point(3.14, 5.25, LocalDateTime.now()); + huchuFirstPoint.setTrip(huchuTrip); + Point huchuSecondPoint = new Point(3.14, 5.25, LocalDateTime.now()); + huchuSecondPoint.setTrip(huchuTrip); + + Point herbFirstPoint = new Point(3.14, 5.25, LocalDateTime.now()); + herbFirstPoint.setTrip(herbTrip); + Point herbSecondPoint = new Point(3.14, 5.25, LocalDateTime.now()); + herbSecondPoint.setTrip(herbTrip); + + pointRepository.save(huchuFirstPoint); + pointRepository.save(huchuSecondPoint); + pointRepository.save(herbFirstPoint); + pointRepository.save(herbSecondPoint); + + List tripIds = List.of(huchuTrip.id(), herbTrip.id()); + + // when + pointRepository.deleteByTripIds(tripIds); + + // then + assertSoftly(softly -> { + softly.assertThat(pointRepository.existsById(huchuFirstPoint.id())).isFalse(); + softly.assertThat(pointRepository.existsById(huchuSecondPoint.id())).isFalse(); + softly.assertThat(pointRepository.existsById(herbFirstPoint.id())).isFalse(); + softly.assertThat(pointRepository.existsById(herbSecondPoint.id())).isFalse(); + }); + } } From 950ce0e6c132a1ab2ed8acfd32c8bb556935ad2f Mon Sep 17 00:00:00 2001 From: Combi153 Date: Thu, 21 Sep 2023 22:36:28 +0900 Subject: [PATCH 5/6] =?UTF-8?q?[test]=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80=20(#398)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/tripdraw/trip/domain/TripRepository.java | 3 ++- .../tripdraw/trip/domain/TripRepositoryTest.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java index f69f04db1..38ff467f3 100644 --- a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java +++ b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java @@ -28,5 +28,6 @@ default Trip getTripWithPoints(Long tripId) { .orElseThrow(() -> new TripException(TRIP_NOT_FOUND)); } - List findAllTripIdsByMemberId(Long memberId); + @Query("SELECT t.id FROM Trip t WHERE t.member.id = :memberId") + List findAllTripIdsByMemberId(@Param(value = "memberId") Long memberId); } diff --git a/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java b/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java index a062430a4..6ebabe3bd 100644 --- a/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java +++ b/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java @@ -14,6 +14,7 @@ import dev.tripdraw.trip.exception.TripException; import java.time.LocalDateTime; import java.util.List; +import java.util.stream.IntStream; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; @@ -123,4 +124,18 @@ void setUp() { .isInstanceOf(TripException.class) .hasMessage(TRIP_NOT_FOUND.message()); } + + @Test + void 회원_ID를_갖는_모든_여행_ID를_조회한다() { + // given + List tripIds = IntStream.range(0, 5) + .mapToObj(value -> tripRepository.save(Trip.from(member)).id()) + .toList(); + + // when + List foundIds = tripRepository.findAllTripIdsByMemberId(member.id()); + + // then + assertThat(foundIds).isEqualTo(tripIds); + } } From 5f1c52e32a0f446409f8e00e483e62f064884af0 Mon Sep 17 00:00:00 2001 From: Combi153 Date: Sat, 23 Sep 2023 22:10:46 +0900 Subject: [PATCH 6/6] =?UTF-8?q?[refactor]=20=EA=B0=90=EC=83=81,=20?= =?UTF-8?q?=EC=97=AC=ED=96=89=20=EC=82=AD=EC=A0=9C=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20(#398)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/dev/tripdraw/post/domain/PostRepository.java | 8 +++++--- .../java/dev/tripdraw/trip/domain/TripRepository.java | 5 ++++- .../tripdraw/member/application/MemberServiceTest.java | 2 +- .../java/dev/tripdraw/post/domain/PostRepositoryTest.java | 4 ++-- .../java/dev/tripdraw/trip/domain/TripRepositoryTest.java | 2 +- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java b/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java index 7baa16bd7..b239b6db8 100644 --- a/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java +++ b/backend/src/main/java/dev/tripdraw/post/domain/PostRepository.java @@ -3,12 +3,12 @@ import static dev.tripdraw.post.exception.PostExceptionType.POST_NOT_FOUND; import dev.tripdraw.post.exception.PostException; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -import java.util.List; - public interface PostRepository extends JpaRepository { @Query("SELECT p FROM Post p JOIN FETCH p.point where p.tripId = :tripId") @@ -19,5 +19,7 @@ default Post getByPostId(Long id) { .orElseThrow(() -> new PostException(POST_NOT_FOUND)); } - void deleteByMemberId(Long memberId); + @Modifying + @Query("DELETE FROM Post p WHERE p.member.id = :memberId") + void deleteByMemberId(@Param(value = "memberId") Long memberId); } diff --git a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java index 38ff467f3..86446588f 100644 --- a/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java +++ b/backend/src/main/java/dev/tripdraw/trip/domain/TripRepository.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -13,7 +14,9 @@ public interface TripRepository extends JpaRepository { List findAllByMemberId(Long memberId); - void deleteByMemberId(Long memberId); + @Modifying + @Query("DELETE FROM Trip t WHERE t.member.id = :memberId") + void deleteByMemberId(@Param(value = "memberId") Long memberId); default Trip getById(Long id) { return findById(id) diff --git a/backend/src/test/java/dev/tripdraw/member/application/MemberServiceTest.java b/backend/src/test/java/dev/tripdraw/member/application/MemberServiceTest.java index d2a168944..220bbc7f6 100644 --- a/backend/src/test/java/dev/tripdraw/member/application/MemberServiceTest.java +++ b/backend/src/test/java/dev/tripdraw/member/application/MemberServiceTest.java @@ -50,7 +50,7 @@ void setUp() { member = memberRepository.save(new Member("통후추", "kakaoId", KAKAO)); trip = tripRepository.save(new Trip(TripName.from("통후추의 여행"), member)); Point point = new Point(3.14, 5.25, LocalDateTime.now()); - trip.add(point); + point.setTrip(trip); postRepository.save(new Post( "제목", point, diff --git a/backend/src/test/java/dev/tripdraw/post/domain/PostRepositoryTest.java b/backend/src/test/java/dev/tripdraw/post/domain/PostRepositoryTest.java index d6d3b6c25..e6dc7dddb 100644 --- a/backend/src/test/java/dev/tripdraw/post/domain/PostRepositoryTest.java +++ b/backend/src/test/java/dev/tripdraw/post/domain/PostRepositoryTest.java @@ -71,13 +71,13 @@ void setUp() { void 회원_ID로_감상을_삭제한다() { // given Post post = new Post("제목", point, "위치", "오늘은 날씨가 좋네요.", member, trip.id()); - postRepository.save(post); + Long postId = postRepository.save(post).id(); // when postRepository.deleteByMemberId(member.id()); // then - assertThat(postRepository.findById(post.id())).isEmpty(); + assertThat(postRepository.existsById(postId)).isFalse(); } @Test diff --git a/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java b/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java index 6ebabe3bd..0428aa357 100644 --- a/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java +++ b/backend/src/test/java/dev/tripdraw/trip/domain/TripRepositoryTest.java @@ -99,7 +99,7 @@ void setUp() { tripRepository.deleteByMemberId(member.id()); // then - assertThat(tripRepository.findById(trip.id())).isEmpty(); + assertThat(tripRepository.existsById(trip.id())).isFalse(); } @Test