Skip to content

Commit

Permalink
Merge pull request #400 from woowacourse-teams/refactor/#398
Browse files Browse the repository at this point in the history
[refactor] 회원 삭제 시 쿼리 변경 (#398)
  • Loading branch information
Combi153 authored Sep 26, 2023
2 parents bc31e3b + 74bac38 commit f7a3f20
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post, Long> {

@Query("SELECT p FROM Post p JOIN FETCH p.point where p.tripId = :tripId")
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<Long> tripIds = tripRepository.findAllTripIdsByMemberId(event.memberId());
pointRepository.deleteByTripIds(tripIds);
tripRepository.deleteByMemberId(event.memberId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
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<Point, Long> {

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<Long> tripIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
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;

public interface TripRepository extends JpaRepository<Trip, Long> {

List<Trip> 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)
Expand All @@ -27,4 +30,7 @@ default Trip getTripWithPoints(Long tripId) {
return findTripWithPoints(tripId)
.orElseThrow(() -> new TripException(TRIP_NOT_FOUND));
}

@Query("SELECT t.id FROM Trip t WHERE t.member.id = :memberId")
List<Long> findAllTripIdsByMemberId(@Param(value = "memberId") Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,6 +24,9 @@ class TripDeleteEventHandlerTest {
@Mock
private TripRepository tripRepository;

@Mock
private PointRepository pointRepository;

@InjectMocks
private TripDeleteEventHandler tripDeleteEventHandler;

Expand All @@ -29,12 +35,19 @@ class TripDeleteEventHandlerTest {
// given
MemberDeleteEvent memberDeleteEvent = new MemberDeleteEvent(1L);

List<Long> tripIds = List.of(1L);
given(tripRepository.findAllTripIdsByMemberId(memberDeleteEvent.memberId()))
.willReturn(tripIds);

// when
tripDeleteEventHandler.deletePostByMemberId(memberDeleteEvent);

// then
then(tripRepository)
.should(times(1))
.deleteByMemberId(1L);
then(pointRepository)
.should(times(1))
.deleteByTripIds(tripIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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<Long> 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();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,7 +99,7 @@ void setUp() {
tripRepository.deleteByMemberId(member.id());

// then
assertThat(tripRepository.findById(trip.id())).isEmpty();
assertThat(tripRepository.existsById(trip.id())).isFalse();
}

@Test
Expand All @@ -123,4 +124,18 @@ void setUp() {
.isInstanceOf(TripException.class)
.hasMessage(TRIP_NOT_FOUND.message());
}

@Test
void 회원_ID를_갖는_모든_여행_ID를_조회한다() {
// given
List<Long> tripIds = IntStream.range(0, 5)
.mapToObj(value -> tripRepository.save(Trip.from(member)).id())
.toList();

// when
List<Long> foundIds = tripRepository.findAllTripIdsByMemberId(member.id());

// then
assertThat(foundIds).isEqualTo(tripIds);
}
}

0 comments on commit f7a3f20

Please sign in to comment.