Skip to content

Commit

Permalink
fix: 논리적 삭제 데이터는 조회에서 제외 #66 (#68)
Browse files Browse the repository at this point in the history
* test: 쿼리 메서드 사용

* fix: sqlDelete문에 테이블명 변경사항 반영

* fix: 삭제된 데이터 제외하고 조회하도록 조건 추가

* fix: 삭제된 데이터 제외하고 조회하도록 조건 추가
  • Loading branch information
linirini authored Jul 24, 2024
1 parent 8d2f42d commit e5d12b8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.staccato.member.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.staccato.member.domain.Member;

public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findByIdAndIsDeletedIsFalse(long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@SQLDelete(sql = "UPDATE mate SET is_deleted = true WHERE id = ?")
@SQLDelete(sql = "UPDATE travel_member SET is_deleted = true WHERE id = ?")
public class TravelMember extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.staccato.travel.domain.TravelMember;

public interface TravelMemberRepository extends JpaRepository<TravelMember, Long> {
List<TravelMember> findAllByMemberId(long memberId);
List<TravelMember> findAllByMemberIdAndIsDeletedIsFalse(long memberId);

@Query("SELECT tm FROM TravelMember tm WHERE tm.member.id = :memberId AND YEAR(tm.travel.startAt) = :year")
@Query("SELECT tm FROM TravelMember tm WHERE tm.member.id = :memberId AND YEAR(tm.travel.startAt) = :year AND tm.isDeleted IS FALSE")
List<TravelMember> findAllByMemberIdAndTravelStartAtYear(@Param("memberId") long memberId, @Param("year") int year);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void saveTravelMember(Long memberId, Travel travel) {
}

private Member getMemberById(long memberId) {
return memberRepository.findById(memberId)
return memberRepository.findByIdAndIsDeletedIsFalse(memberId)
.orElseThrow(() -> new IllegalArgumentException("Invalid Operation"));
}

Expand All @@ -70,7 +70,7 @@ public TravelResponses readAllTravels(long memberId, Integer year) {
}

private TravelResponses readAll(long memberId) {
List<TravelMember> travelMembers = travelMemberRepository.findAllByMemberId(memberId);
List<TravelMember> travelMembers = travelMemberRepository.findAllByMemberIdAndIsDeletedIsFalse(memberId);
return getTravelDetailResponses(travelMembers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ void findAllByMemberIdAndTravelStartAtYear() {
assertThat(result).hasSize(2);
}

@DisplayName("사용자 식별자와 년도로 삭제 되지 않은 여행 상세 목록만 조회한다.")
@Test
void findAllByMemberIdAndTravelStartAtYearWithoutDeleted() {
// given
Member member = memberRepository.save(Member.builder().nickname("staccato").build());
Travel travel = travelRepository.save(createTravel(LocalDate.of(2023, 7, 1), LocalDate.of(2023, 7, 10)));
Travel travel2 = travelRepository.save(createTravel(LocalDate.of(2023, 12, 31), LocalDate.of(2024, 1, 10)));
TravelMember target = travelMemberRepository.save(new TravelMember(member, travel));
travelMemberRepository.save(new TravelMember(member, travel2));
travelMemberRepository.deleteById(target.getId());

// when
List<TravelMember> result = travelMemberRepository.findAllByMemberIdAndTravelStartAtYear(member.getId(), 2023);

// then
assertThat(result).hasSize(1);
}

private static Travel createTravel(LocalDate startAt, LocalDate endAt) {
return Travel.builder()
.title("여행")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void createTravel() {

// when
long travelId = travelService.createTravel(travelRequest, member.getId());
TravelMember travelMember = travelMemberRepository.findAll().get(0);
TravelMember travelMember = travelMemberRepository.findAllByMemberIdAndIsDeletedIsFalse(member.getId()).get(0);

// then
assertAll(
Expand Down

0 comments on commit e5d12b8

Please sign in to comment.