diff --git a/src/main/java/com/umc/naoman/domain/photo/repository/PhotoRepository.java b/src/main/java/com/umc/naoman/domain/photo/repository/PhotoRepository.java index ad3a844..c3a2a08 100644 --- a/src/main/java/com/umc/naoman/domain/photo/repository/PhotoRepository.java +++ b/src/main/java/com/umc/naoman/domain/photo/repository/PhotoRepository.java @@ -2,6 +2,9 @@ import com.umc.naoman.domain.photo.entity.Photo; 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; @@ -10,4 +13,7 @@ public interface PhotoRepository extends JpaRepository { List findByIdInAndShareGroupId(List photoIdList, Long shareGroupId); List findByIdIn(List photoIdList); + @Modifying + @Query("DELETE FROM Photo p WHERE p.id IN :photoIdList") + void deleteAllByPhotoIdList(@Param("photoIdList") List photoIdList); } diff --git a/src/main/java/com/umc/naoman/domain/photo/service/PhotoServiceImpl.java b/src/main/java/com/umc/naoman/domain/photo/service/PhotoServiceImpl.java index 2b03ff7..0fec79b 100644 --- a/src/main/java/com/umc/naoman/domain/photo/service/PhotoServiceImpl.java +++ b/src/main/java/com/umc/naoman/domain/photo/service/PhotoServiceImpl.java @@ -106,9 +106,16 @@ public List deletePhotoList(PhotoRequest.PhotoDeletedRequest request, Mem // 각 사진에 대해 S3에서 객체 삭제 및 데이터베이스에서 삭제 for (Photo photo : photoList) { - deletePhoto(photo); + deletePhoto(photo.getName()); } + // ID 목록을 추출하여 삭제 쿼리 실행 + List photoIdList = photoList.stream() + .map(Photo::getId) + .toList(); + + photoRepository.deleteAllByPhotoIdList(photoIdList); + return photoList; // 삭제된 사진 목록 반환 } @@ -151,7 +158,6 @@ private GeneratePresignedUrlRequest getGeneratePreSignedUrlRequest(String bucket return generatePresignedUrlRequest; } - // 원본 사진 전체 경로 생성 private String createPath(String fileName) { String fileId = createFileId(); @@ -193,13 +199,10 @@ private Photo checkAndSavePhotoInDB(String photoUrl, String photoName, ShareGrou return photoRepository.save(photo); // 저장된 Photo 객체 반환 } - private void deletePhoto(Photo photo) { + private void deletePhoto(String photoName) { // S3에서 원본 및 변환된 이미지 삭제 - s3Template.deleteObject(bucketName, RAW_PATH_PREFIX + "/" + photo.getName()); - s3Template.deleteObject(bucketName, W200_PATH_PREFIX + "/" + photoConverter.convertExtension(photo.getName())); - s3Template.deleteObject(bucketName, W400_PATH_PREFIX + "/" + photoConverter.convertExtension(photo.getName())); - - // 데이터베이스에서 사진 삭제 - photoRepository.delete(photo); + s3Template.deleteObject(bucketName, RAW_PATH_PREFIX + "/" + photoName); + s3Template.deleteObject(bucketName, W200_PATH_PREFIX + "/" + photoConverter.convertExtension(photoName)); + s3Template.deleteObject(bucketName, W400_PATH_PREFIX + "/" + photoConverter.convertExtension(photoName)); } }