Skip to content

Commit

Permalink
Merge pull request #97 from miinyeong/main
Browse files Browse the repository at this point in the history
[#96]Feat: 사진첩 삭제 API 생성
  • Loading branch information
miinyeong authored Nov 18, 2023
2 parents 88f8461 + ec10ea5 commit 6774c92
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/onestep/server/ImageInfra/S3Config.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.onestep.server.entity.ImageInfra;
package com.onestep.server.ImageInfra;


import com.amazonaws.auth.AWSCredentials;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -41,6 +44,11 @@ public List<ViewPhotoBookDTO> 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+"번 사진첩 삭제 완료";
}
// 사진첩 사진 수정
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhotoBookComment,Long> {
@Query("select c from PhotoBookComment c where c.photoBook.photo_id =:photoBookId")
List<PhotoBookComment> findByPhotoBookId(Long photoBookId);
List<PhotoBookComment> findByPhotoBookId(@Param("photoBookId")Long photoBookId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,4 +15,9 @@ public interface IPhotoBookRepository extends JpaRepository<PhotoBook, Long> {
@Query("SELECT p FROM PhotoBook p WHERE p.user.family =:family ")
List<PhotoBook> 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);

}
18 changes: 13 additions & 5 deletions src/main/java/com/onestep/server/service/image/S3Uploader.java
Original file line number Diff line number Diff line change
@@ -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 멤버변수가 있으면 생성자 항목에 포함시킴
Expand Down Expand Up @@ -67,13 +71,17 @@ 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());
fos.close();
return convertFile;

}
// 파일 삭제
public void delete(String fileKey) {
log.info("test3={}", fileKey);
amazonS3Client.deleteObject(bucket, fileKey);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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){
Expand Down Expand Up @@ -80,4 +87,18 @@ public List<ViewPhotoBookDTO> readPhotoBook(String family_id){
}
return viewPhotoBookDTOS;
}

// 사진첩 삭제
public void deletePhotoBook(Long photoBook_id){
// S3 업로드된 파일 삭제
Optional<PhotoBook> 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);
}

}

0 comments on commit 6774c92

Please sign in to comment.