From 4f9804c22d31afd2e45d771ba65a5c5cf25f7d16 Mon Sep 17 00:00:00 2001 From: Donghoon Jeong <112836685+jjeongdong@users.noreply.github.com> Date: Sun, 11 Aug 2024 14:19:43 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix=20:=20=EC=82=AD=EC=A0=9C=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=B2=8C=ED=81=AC=20=EC=82=AD=EC=A0=9C=EB=A1=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../photo/repository/PhotoRepository.java | 6 ++++++ .../photo/service/PhotoServiceImpl.java | 21 +++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) 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..13d7ccc 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 :idList") + void deleteAllByIdList(@Param("idList") List idList); } 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..b7e75e0 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.deleteAllByIdList(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)); } } From 8503b5dc7126da131a070d1aaf20a6eeb1db64fe Mon Sep 17 00:00:00 2001 From: Donghoon Jeong <112836685+jjeongdong@users.noreply.github.com> Date: Sun, 11 Aug 2024 14:53:22 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix=20:=20=EB=B3=80=EC=88=98=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../umc/naoman/domain/photo/repository/PhotoRepository.java | 4 ++-- .../com/umc/naoman/domain/photo/service/PhotoServiceImpl.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 13d7ccc..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 @@ -14,6 +14,6 @@ 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 :idList") - void deleteAllByIdList(@Param("idList") List idList); + @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 b7e75e0..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 @@ -114,7 +114,7 @@ public List deletePhotoList(PhotoRequest.PhotoDeletedRequest request, Mem .map(Photo::getId) .toList(); - photoRepository.deleteAllByIdList(photoIdList); + photoRepository.deleteAllByPhotoIdList(photoIdList); return photoList; // 삭제된 사진 목록 반환 }