diff --git a/src/main/java/com/onestep/server/ImageInfra/S3Config.java b/src/main/java/com/onestep/server/ImageInfra/S3Config.java index 0379826..2b042a0 100644 --- a/src/main/java/com/onestep/server/ImageInfra/S3Config.java +++ b/src/main/java/com/onestep/server/ImageInfra/S3Config.java @@ -1,4 +1,4 @@ -package com.onestep.server.entity.ImageInfra; +package com.onestep.server.ImageInfra; import com.amazonaws.auth.AWSCredentials; diff --git a/src/main/java/com/onestep/server/controller/photoBook/PhotoBookController.java b/src/main/java/com/onestep/server/controller/photoBook/PhotoBookController.java index 0e59a20..4c7b3e2 100644 --- a/src/main/java/com/onestep/server/controller/photoBook/PhotoBookController.java +++ b/src/main/java/com/onestep/server/controller/photoBook/PhotoBookController.java @@ -1,9 +1,11 @@ package com.onestep.server.controller.photoBook; +import com.amazonaws.services.cloudformation.model.StackInstance; import com.onestep.server.entity.PhotoBook; import com.onestep.server.entity.PhotoBookComment; import com.onestep.server.entity.photoBook.ViewPhotoBookDTO; import com.onestep.server.entity.photoBookComment.ViewPhotoBookCommentDTO; +import com.onestep.server.repository.IPhotoBookRepository; import com.onestep.server.service.image.S3Uploader; import com.onestep.server.service.photoBook.PhotoBookService; import lombok.RequiredArgsConstructor; @@ -24,6 +26,7 @@ public class PhotoBookController { private final PhotoBookService photoBookService; private final S3Uploader s3Uploader; + private final IPhotoBookRepository iPhotoBookRepository; // 사진첩 사진 등록 @PostMapping(value = "/photobook/write/{user_id}", consumes = {MediaType.APPLICATION_JSON_VALUE, "multipart/form-data"}) public String writePhotoBook(@PathVariable String user_id, @RequestPart(value = "writeTxt") String writeTxt, @RequestPart(value = "img") MultipartFile img) throws IOException { @@ -41,6 +44,11 @@ public List readPhotoBook(@PathVariable String family_id){ return viewPhotoBookDTOS; } // 사진첩 사진 삭제 + @DeleteMapping(value = "/photobook/delete/{photoBook_id}") + public String deletePhotoBook(@PathVariable Long photoBook_id){ + photoBookService.deletePhotoBook(photoBook_id); + return photoBook_id+"번 사진첩 삭제 완료"; + } // 사진첩 사진 수정 } diff --git a/src/main/java/com/onestep/server/repository/IPhotoBookCommentRepository.java b/src/main/java/com/onestep/server/repository/IPhotoBookCommentRepository.java index cdcda7f..ba0ddd3 100644 --- a/src/main/java/com/onestep/server/repository/IPhotoBookCommentRepository.java +++ b/src/main/java/com/onestep/server/repository/IPhotoBookCommentRepository.java @@ -2,11 +2,15 @@ import com.onestep.server.entity.PhotoBookComment; 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.transaction.annotation.Transactional; import java.util.List; public interface IPhotoBookCommentRepository extends JpaRepository { @Query("select c from PhotoBookComment c where c.photoBook.photo_id =:photoBookId") - List findByPhotoBookId(Long photoBookId); + List findByPhotoBookId(@Param("photoBookId")Long photoBookId); + } diff --git a/src/main/java/com/onestep/server/repository/IPhotoBookRepository.java b/src/main/java/com/onestep/server/repository/IPhotoBookRepository.java index 3afefbf..32a3517 100644 --- a/src/main/java/com/onestep/server/repository/IPhotoBookRepository.java +++ b/src/main/java/com/onestep/server/repository/IPhotoBookRepository.java @@ -3,8 +3,10 @@ import com.onestep.server.entity.Family; import com.onestep.server.entity.PhotoBook; 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.transaction.annotation.Transactional; import java.util.List; @@ -13,4 +15,9 @@ public interface IPhotoBookRepository extends JpaRepository { @Query("SELECT p FROM PhotoBook p WHERE p.user.family =:family ") List findPhotoBookByFamilyId(@Param("family") Family family); + @Modifying + @Transactional + @Query("delete from PhotoBook p where p.photo_id =:photoBook_id") + void deleteByPhotoBookId(@Param("photoBook_id") Long photoBook_id); + } diff --git a/src/main/java/com/onestep/server/service/image/S3Uploader.java b/src/main/java/com/onestep/server/service/image/S3Uploader.java index 50abb4c..f146093 100644 --- a/src/main/java/com/onestep/server/service/image/S3Uploader.java +++ b/src/main/java/com/onestep/server/service/image/S3Uploader.java @@ -1,22 +1,26 @@ package com.onestep.server.service.image; +import com.amazonaws.AmazonServiceException; +import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.PutObjectRequest; -import javax.persistence.PersistenceContext; + + +import com.onestep.server.ImageInfra.S3Config; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.util.Optional; + @Slf4j @RequiredArgsConstructor // final 멤버변수가 있으면 생성자 항목에 포함시킴 @@ -67,7 +71,6 @@ private void removeNewFile(File targetFile) { // 로컬에 파일 업로드 하기 private File convert(MultipartFile file) throws IOException { File convertFile = new File(file.getOriginalFilename()); - log.info("test={}","test1"); convertFile.createNewFile(); FileOutputStream fos = new FileOutputStream(convertFile); fos.write(file.getBytes()); @@ -75,5 +78,10 @@ private File convert(MultipartFile file) throws IOException { return convertFile; } + // 파일 삭제 + public void delete(String fileKey) { + log.info("test3={}", fileKey); + amazonS3Client.deleteObject(bucket, fileKey); + } } \ No newline at end of file diff --git a/src/main/java/com/onestep/server/service/photoBook/PhotoBookService.java b/src/main/java/com/onestep/server/service/photoBook/PhotoBookService.java index 46492a2..6cfe801 100644 --- a/src/main/java/com/onestep/server/service/photoBook/PhotoBookService.java +++ b/src/main/java/com/onestep/server/service/photoBook/PhotoBookService.java @@ -1,6 +1,7 @@ package com.onestep.server.service.photoBook; + import com.onestep.server.entity.Family; import com.onestep.server.entity.PhotoBook; import com.onestep.server.entity.PhotoBookComment; @@ -9,10 +10,13 @@ import com.onestep.server.entity.photoBook.ViewPhotoBookDTO; import com.onestep.server.entity.photoBookComment.ViewPhotoBookCommentDTO; import com.onestep.server.repository.IFamilyRepository; +import com.onestep.server.repository.IPhotoBookCommentRepository; import com.onestep.server.repository.IPhotoBookRepository; import com.onestep.server.repository.IUserRepository; +import com.onestep.server.service.image.S3Uploader; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; + import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -27,8 +31,11 @@ @Transactional public class PhotoBookService { private final IPhotoBookRepository iPhotoBookRepository; + private final IPhotoBookCommentRepository iPhotoBookCommentRepository; private final IUserRepository iUserRepository; private final IFamilyRepository iFamilyRepository; + private final S3Uploader s3Uploader; + //사진첩에 사진 등록 public PhotoBook writePhotoBook(String user_id, String writeTxt,String url){ @@ -80,4 +87,18 @@ public List readPhotoBook(String family_id){ } return viewPhotoBookDTOS; } + + // 사진첩 삭제 + public void deletePhotoBook(Long photoBook_id){ + // S3 업로드된 파일 삭제 + Optional findUrl = iPhotoBookRepository.findById(photoBook_id); + String url = findUrl.get().getPhoto_img(); + log.info("test={}", url); + url = url.replaceAll("https://conteswt-bucket.s3.ap-northeast-2.amazonaws.com/",""); + log.info("test2={}", url); + s3Uploader.delete(url); + // PhotoBook DB 삭제 + iPhotoBookRepository.deleteByPhotoBookId(photoBook_id); + } + }