diff --git a/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/entity/FileMetaData.java b/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/entity/FileMetaData.java index 1a211bdf..aca3eeb9 100644 --- a/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/entity/FileMetaData.java +++ b/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/entity/FileMetaData.java @@ -8,21 +8,18 @@ import jakarta.persistence.Id; import jakarta.persistence.Index; import jakarta.persistence.Table; -import java.time.LocalDateTime; import java.util.UUID; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLRestriction; @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Table(name = "file_meta_data",indexes = {@Index(columnList = "domainType,entityId,fileStatus")}) -@SQLDelete(sql = "update file_meta_data set deleted_at = CURRENT_TIMESTAMP where id=?") -@SQLRestriction("deleted_at IS NULL") +@SQLRestriction("file_status != 'DELETED'") public class FileMetaData extends BaseEntity { @Id @@ -47,9 +44,6 @@ public class FileMetaData extends BaseEntity { @Enumerated(EnumType.STRING) private FileCategory fileCategory; - @Column(name = "deleted_at", columnDefinition = "TIMESTAMP") - private LocalDateTime deletedAt; - @Builder private FileMetaData(UUID id, String fileKey, String fileName, DomainType domainType, Long entityId, FileStatus fileStatus, diff --git a/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/repository/FileMetaDataRepository.java b/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/repository/FileMetaDataRepository.java index 5fffa11f..3e3c477f 100644 --- a/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/repository/FileMetaDataRepository.java +++ b/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/repository/FileMetaDataRepository.java @@ -11,14 +11,35 @@ public interface FileMetaDataRepository extends JpaRepository { - @Query("select fmd from FileMetaData fmd where fmd.domainType = :domainType and fmd.entityId = :entityId and fmd.fileStatus = :fileStatus") + @Query(""" + select fmd from FileMetaData fmd + where fmd.domainType = :domainType + and fmd.entityId = :entityId + and fmd.fileStatus = :fileStatus + and fmd.fileStatus != 'DELETED' + """) List findAllByDomainTypeAndEntityIdWithFileStatus( - @Param("domainType") DomainType domainType, - @Param("entityId") Long entityId, - @Param("fileStatus") FileStatus fileStatus + @Param("domainType") DomainType domainType, + @Param("entityId") Long entityId, + @Param("fileStatus") FileStatus fileStatus ); - List findAllByDomainTypeAndEntityId(DomainType domainType, Long entityId); + @Query(value = """ + select * from file_meta_data + where domain_type = :#{#domainType.name()} + and entity_id = :entityId + and file_status != 'DELETED' + """, nativeQuery = true) + List findAllByDomainTypeAndEntityId( + @Param("domainType") DomainType domainType, + @Param("entityId") Long entityId + ); - List findByIdIn(List ids); + @Query(value = """ + select * + from file_meta_data + where id in (:ids) + and file_status != 'DELETED' + """, nativeQuery = true) + List findByIdIn(@Param("ids") List ids); } diff --git a/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/service/FileMetaDataServiceImpl.java b/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/service/FileMetaDataServiceImpl.java index c741dd33..298c6081 100644 --- a/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/service/FileMetaDataServiceImpl.java +++ b/src/main/java/ddingdong/ddingdongBE/domain/filemetadata/service/FileMetaDataServiceImpl.java @@ -96,8 +96,14 @@ public void updateStatusToDelete(DomainType domainType, Long entityId) { fileMetaDatas.forEach(fileMetaData -> { fileMetaData.updateStatus(DELETED); }); - entityManager.flush(); - fileMetaDataRepository.deleteAll(fileMetaDatas); + } + + private List getNewIds(List ids) { + List fileMetaDatas = fileMetaDataRepository.findByIdIn(toUUIDs(ids)); + return fileMetaDatas.stream() + .filter(FileMetaData::isPending) + .map(fileMetaData -> String.valueOf(fileMetaData.getId())) + .toList(); } private List getNewIds(List ids) { @@ -122,8 +128,6 @@ private void deleteOldIds(List ids, DomainType domainType, Long entityId .filter(fileMetaData -> !ids.contains(String.valueOf(fileMetaData.getId()))) .toList(); deleteTarget.forEach(target -> target.updateStatus(DELETED)); - entityManager.flush(); - fileMetaDataRepository.deleteAll(deleteTarget); } private FileMetaData findById(UUID id) { diff --git a/src/main/resources/db/migration/V27__drop_file_meta_data_column.sql b/src/main/resources/db/migration/V27__drop_file_meta_data_column.sql new file mode 100644 index 00000000..599d64ac --- /dev/null +++ b/src/main/resources/db/migration/V27__drop_file_meta_data_column.sql @@ -0,0 +1,2 @@ +ALTER TABLE file_meta_data + DROP COLUMN deleted_at; diff --git a/src/test/java/ddingdong/ddingdongBE/domain/filemetadata/repository/FileMetaDataRepositoryTest.java b/src/test/java/ddingdong/ddingdongBE/domain/filemetadata/repository/FileMetaDataRepositoryTest.java new file mode 100644 index 00000000..27b9b472 --- /dev/null +++ b/src/test/java/ddingdong/ddingdongBE/domain/filemetadata/repository/FileMetaDataRepositoryTest.java @@ -0,0 +1,75 @@ +package ddingdong.ddingdongBE.domain.filemetadata.repository; + +import ddingdong.ddingdongBE.common.support.DataJpaTestSupport; +import ddingdong.ddingdongBE.domain.filemetadata.entity.DomainType; +import ddingdong.ddingdongBE.domain.filemetadata.entity.FileMetaData; +import ddingdong.ddingdongBE.domain.filemetadata.entity.FileStatus; +import jakarta.persistence.EntityManager; +import java.util.List; +import java.util.UUID; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +class FileMetaDataRepositoryTest extends DataJpaTestSupport { + + @Autowired + private FileMetaDataRepository fileMetaDataRepository; + + @Autowired + private EntityManager entityManager; + + @Test + void findAllByDomainTypeAndEntityId() { + DomainType domainType = DomainType.CLUB_PROFILE; + Long id = 1L; + FileMetaData fileMetaData = FileMetaData.builder() + .id(UUID.randomUUID()) + .fileKey("123") + .fileName("1234.png") + .domainType(domainType) + .entityId(id) + .fileStatus(FileStatus.COUPLED) + .build(); + fileMetaDataRepository.save(fileMetaData); + + List fileMetaDataList = fileMetaDataRepository.findAllByDomainTypeAndEntityId(domainType, id); + + Assertions.assertThat(fileMetaDataList.size()).isEqualTo(1); + Assertions.assertThat(fileMetaDataList.get(0).getFileKey()).isEqualTo("123"); + } + + @DisplayName("여러 개 UUID를 사용하여 fileMetaData를 조회한다.") + @Test + void findByIn() { + // given + DomainType domainType = DomainType.CLUB_PROFILE; + Long entityId = 1L; + Long entityId2 = 2L; + UUID id = UUID.randomUUID(); + UUID id2 = UUID.randomUUID(); + FileMetaData fileMetaData = FileMetaData.builder() + .id(id) + .fileKey("123") + .fileName("1234.png") + .domainType(domainType) + .entityId(entityId) + .fileStatus(FileStatus.COUPLED) + .build(); + FileMetaData fileMetaData2 = FileMetaData.builder() + .id(id2) + .fileKey("12344") + .fileName("123455.png") + .domainType(domainType) + .entityId(entityId2) + .fileStatus(FileStatus.DELETED) + .build(); + fileMetaDataRepository.saveAll(List.of(fileMetaData, fileMetaData2)); + // when + List fileMetaDataList = fileMetaDataRepository.findByIdIn(List.of(id, id2)); + // then + Assertions.assertThat(fileMetaDataList.size()).isEqualTo(1); + Assertions.assertThat(fileMetaDataList.get(0).getFileKey()).isEqualTo("123"); + } +} diff --git a/src/test/java/ddingdong/ddingdongBE/domain/filemetadata/service/FileMetaDataServiceImplTest.java b/src/test/java/ddingdong/ddingdongBE/domain/filemetadata/service/FileMetaDataServiceImplTest.java index b8242cfe..ea90b715 100644 --- a/src/test/java/ddingdong/ddingdongBE/domain/filemetadata/service/FileMetaDataServiceImplTest.java +++ b/src/test/java/ddingdong/ddingdongBE/domain/filemetadata/service/FileMetaDataServiceImplTest.java @@ -141,11 +141,9 @@ void updateAllToActivatedAndAttached() { List result = fileMetaDataRepository.findAllByDomainTypeAndEntityIdWithFileStatus( domainType, entityId, FileStatus.COUPLED); - FileMetaData attachedFileMetaData = fileMetaDataRepository.findById(id2).orElse(null); assertThat(result).hasSize(2) .extracting("id", "fileStatus") .contains(tuple(id1, FileStatus.COUPLED), tuple(id3, FileStatus.COUPLED)); - assertThat(attachedFileMetaData).isEqualTo(null); } @DisplayName("FileMetaData 수정 - COUPLED & DELETED 기존 아이디를 그대로 입력할 경우") @@ -199,7 +197,6 @@ void updateAllToAttached() { .set("fileStatus", FileStatus.COUPLED) .sample() )); - //when fileMetaDataService.updateStatusToDelete(domainType, entityId); em.flush();