Skip to content

Commit

Permalink
fix: PinImage @where 적용으로 지연 로딩 시 soft delete 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
yoondgu committed Oct 5, 2023
1 parent 28e0e4e commit 83efb4b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public void removeImageById(AuthMember authMember, Long pinImageId) {
}

private PinImage findPinImage(Long pinImageId) {
return pinImageRepository.findByIdAndIsDeletedFalse(pinImageId)
return pinImageRepository.findById(pinImageId)
.orElseThrow(() -> new PinBadRequestException(ILLEGAL_PIN_IMAGE_ID));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Where;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Where(clause = "is_deleted = false")
public class PinImage extends BaseTimeEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.mapbefine.mapbefine.pin.domain;

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;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface PinImageRepository extends JpaRepository<PinImage, Long> {

Optional<PinImage> findByIdAndIsDeletedFalse(Long pinId);
Optional<PinImage> findById(Long pinId);

List<PinImage> findAllByPinIdAndIsDeletedFalse(Long pinId);
List<PinImage> findAllByPinId(Long pinId);

@Modifying(clearAutomatically = true)
@Query("update PinImage p set p.isDeleted = true where p.pin.id = :pinId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,15 @@ void blockMember_Success() {
adminCommandService.blockMember(member.getId());

//then
PinImage deletedPinImage = pinImageRepository.findById(pinImage.getId()).get();
Member blockedMember = memberRepository.findById(member.getId()).get();

assertAll(
() -> assertThat(blockedMember.getMemberInfo().getStatus()).isEqualTo(Status.BLOCKED),
() -> assertThat(topicRepository.existsById(topic.getId())).isFalse(),
() -> assertThat(pinRepository.existsById(pin.getId())).isFalse(),
() -> assertThat(deletedPinImage.isDeleted()).isTrue(),
() -> assertThat(
bookmarkRepository.existsByMemberIdAndTopicId(member.getId(), topic.getId())).isFalse(),
() -> assertThat(pinImageRepository.existsById(pinImage.getId())).isFalse(),
() -> assertThat(bookmarkRepository.existsByMemberIdAndTopicId(member.getId(), topic.getId()))
.isFalse(),
() -> assertThat(atlasRepository.existsByMemberIdAndTopicId(member.getId(), topic.getId())).isFalse(),
() -> assertThat(permissionRepository.existsByTopicIdAndMemberId(topic.getId(), member.getId()))
.isFalse()
Expand Down Expand Up @@ -186,9 +185,7 @@ void deletePinImage_Success() {
adminCommandService.deletePinImage(pinImage.getId());

//then
PinImage deletedPinImage = pinImageRepository.findById(pinImage.getId()).get();

assertThat(deletedPinImage.isDeleted()).isTrue();
assertThat(pinImageRepository.findById(pinImage.getId())).isEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void removeById_Success() {
// then
assertThat(pinRepository.findById(pinId))
.isEmpty();
assertThat(pinImageRepository.findByIdAndIsDeletedFalse(pinId))
assertThat(pinImageRepository.findById(pinId))
.isEmpty();
}

Expand Down Expand Up @@ -267,7 +267,7 @@ void removeImageById_Success() {
pinCommandService.removeImageById(authMember, pinImageId);

// then
assertThat(pinImageRepository.findByIdAndIsDeletedFalse(pinImageId))
assertThat(pinImageRepository.findById(pinImageId))
.isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
import com.mapbefine.mapbefine.topic.TopicFixture;
import com.mapbefine.mapbefine.topic.domain.Topic;
import com.mapbefine.mapbefine.topic.domain.TopicRepository;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.util.List;

@DataJpaTest
class PinImageRepositoryTest {

Expand Down Expand Up @@ -62,7 +61,7 @@ void deleteById_Success() {
pinImageRepository.deleteById(pinImageId);

//then
assertThat(pinImageRepository.findByIdAndIsDeletedFalse(pinImageId))
assertThat(pinImageRepository.findById(pinImageId))
.isEmpty();
}

Expand All @@ -81,7 +80,7 @@ void deleteAllByPinId_Success() {
pinImageRepository.deleteAllByPinId(pin.getId());

//then
assertThat(pinImageRepository.findByIdAndIsDeletedFalse(pin.getId()))
assertThat(pinImageRepository.findById(pin.getId()))
.isEmpty();
}

Expand All @@ -102,9 +101,9 @@ void deleteAllByMemberId_Success() {
pinImageRepository.deleteAllByPinIds(List.of(pin.getId(), otherPin.getId()));

//then
assertThat(pinImageRepository.findByIdAndIsDeletedFalse(pin.getId()))
assertThat(pinImageRepository.findById(pin.getId()))
.isEmpty();
assertThat(pinImageRepository.findAllByPinIdAndIsDeletedFalse(otherPin.getId()))
assertThat(pinImageRepository.findAllByPinId(otherPin.getId()))
.isEmpty();
}

Expand Down

0 comments on commit 83efb4b

Please sign in to comment.