Skip to content

Commit

Permalink
Merge pull request #86 from Na-o-man/fix/#84/photo-delete-fix
Browse files Browse the repository at this point in the history
[FIX] 삭제 로직 벌크 삭제로 수정
  • Loading branch information
bflykky authored Aug 11, 2024
2 parents 7b6c724 + 8503b5d commit 4a6840b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,4 +13,7 @@
public interface PhotoRepository extends JpaRepository<Photo, Long> {
List<Photo> findByIdInAndShareGroupId(List<Long> photoIdList, Long shareGroupId);
List<Photo> findByIdIn(List<Long> photoIdList);
@Modifying
@Query("DELETE FROM Photo p WHERE p.id IN :photoIdList")
void deleteAllByPhotoIdList(@Param("photoIdList") List<Long> photoIdList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,16 @@ public List<Photo> deletePhotoList(PhotoRequest.PhotoDeletedRequest request, Mem

// 각 사진에 대해 S3에서 객체 삭제 및 데이터베이스에서 삭제
for (Photo photo : photoList) {
deletePhoto(photo);
deletePhoto(photo.getName());
}

// ID 목록을 추출하여 삭제 쿼리 실행
List<Long> photoIdList = photoList.stream()
.map(Photo::getId)
.toList();

photoRepository.deleteAllByPhotoIdList(photoIdList);

return photoList; // 삭제된 사진 목록 반환
}

Expand Down Expand Up @@ -151,7 +158,6 @@ private GeneratePresignedUrlRequest getGeneratePreSignedUrlRequest(String bucket
return generatePresignedUrlRequest;
}


// 원본 사진 전체 경로 생성
private String createPath(String fileName) {
String fileId = createFileId();
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 4a6840b

Please sign in to comment.