From 060dea9b9c786bb66d138e556df902039dbcd5dc Mon Sep 17 00:00:00 2001 From: kokoa322 Date: Sat, 16 Jul 2022 11:51:07 +0900 Subject: [PATCH] =?UTF-8?q?11#=20[update]=20=EB=A6=AC=EB=B7=B0=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C,=20=EC=88=98=EC=A0=95,=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=84=B0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/controller/ReviewController.java | 9 +- .../repository/ReviewImageRepository.java | 4 + .../review/repository/ReviewRepository.java | 2 - .../review/service/ReviewService.java | 162 +++++++++++------- .../tag/repository/TagRepository.java | 10 ++ 5 files changed, 119 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/mpnp/baechelin/review/controller/ReviewController.java b/src/main/java/com/mpnp/baechelin/review/controller/ReviewController.java index df95d31..968cff4 100644 --- a/src/main/java/com/mpnp/baechelin/review/controller/ReviewController.java +++ b/src/main/java/com/mpnp/baechelin/review/controller/ReviewController.java @@ -40,10 +40,11 @@ public ResponseEntity> getStoreReview(@PathVariable int @PostMapping("/review") public ResponseEntity review(@ModelAttribute ReviewRequestDto reviewRequestDto, @AuthenticationPrincipal User user) throws IOException { - if(user==null){ - throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); - } + + + if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다.");} reviewService.review(reviewRequestDto, user.getUsername()); + return new ResponseEntity<>(HttpStatus.OK); } @@ -54,7 +55,6 @@ public ResponseEntity reviewUpdate(@ModelAttribute ReviewRequestDto reviewReq @PathVariable int reviewId) throws IOException { if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); } - reviewService.reviewUpdate(reviewRequestDto, user.getUsername(), reviewId); return new ResponseEntity<>(HttpStatus.OK); @@ -66,7 +66,6 @@ public ResponseEntity reviewDelete(@AuthenticationPrincipal User user, @PathVariable int reviewId) throws IOException { if(user==null){ throw new IllegalArgumentException("해당하는 회원 정보가 없습니다."); } - reviewService.reviewDelete(user.getUsername(), reviewId); return new ResponseEntity<>(HttpStatus.OK); diff --git a/src/main/java/com/mpnp/baechelin/review/repository/ReviewImageRepository.java b/src/main/java/com/mpnp/baechelin/review/repository/ReviewImageRepository.java index 2f1153d..9a59957 100644 --- a/src/main/java/com/mpnp/baechelin/review/repository/ReviewImageRepository.java +++ b/src/main/java/com/mpnp/baechelin/review/repository/ReviewImageRepository.java @@ -1,7 +1,11 @@ package com.mpnp.baechelin.review.repository; +import com.mpnp.baechelin.review.domain.Review; import com.mpnp.baechelin.review.domain.ReviewImage; import org.springframework.data.jpa.repository.JpaRepository; public interface ReviewImageRepository extends JpaRepository { + void deleteAllByReviewId(Review review); + + void deleteByReviewId(Review review); } diff --git a/src/main/java/com/mpnp/baechelin/review/repository/ReviewRepository.java b/src/main/java/com/mpnp/baechelin/review/repository/ReviewRepository.java index a964443..1353f42 100644 --- a/src/main/java/com/mpnp/baechelin/review/repository/ReviewRepository.java +++ b/src/main/java/com/mpnp/baechelin/review/repository/ReviewRepository.java @@ -9,7 +9,5 @@ import java.util.Optional; public interface ReviewRepository extends JpaRepository { - List findAllByStoreId(Store store); - } diff --git a/src/main/java/com/mpnp/baechelin/review/service/ReviewService.java b/src/main/java/com/mpnp/baechelin/review/service/ReviewService.java index 4ee3eee..ae506eb 100644 --- a/src/main/java/com/mpnp/baechelin/review/service/ReviewService.java +++ b/src/main/java/com/mpnp/baechelin/review/service/ReviewService.java @@ -30,108 +30,148 @@ @RequiredArgsConstructor @Transactional public class ReviewService { - private final ReviewRepository reviewRepository; - private final UserRepository userRepository; - private final StoreRepository storeRepository; - private final TagRepository tagRepository; + private final AwsS3Manager awsS3Manager; + private final TagRepository tagRepository; + private final UserRepository userRepository; + private final StoreRepository storeRepository; + private final ReviewRepository reviewRepository; private final ReviewImageRepository reviewImageRepository; - private final AwsS3Manager awsS3Manager; + + /** 리뷰 작성 */ public void review(ReviewRequestDto reviewRequestDto, String socialId) throws IOException { - int storeId = reviewRequestDto.getStoreId(); //리뷰 업장 ID + int storeId = reviewRequestDto.getStoreId(); Store store = storeRepository.findById(storeId).orElseThrow(() -> new IllegalArgumentException("해당하는 업장이 존재하지 않습니다.")); User user = userRepository.findBySocialId(socialId); Review review = new Review(reviewRequestDto, store, user); + + // todo 태크 매핑 List tagList = new ArrayList<>(); for (String s : reviewRequestDto.getTagList()) { + System.out.println("tag --> "+ s); tagList.add(new Tag(s, review)); } // 태그 -> 엔티티 변환 + List reviewImageUrlList = new ArrayList<>(); + List newReviewImage = reviewRequestDto.getImageFile(); - for (MultipartFile reviewImage : reviewRequestDto.getImageFile()) { - reviewImageUrlList.add(ReviewImage.builder().reviewId(review).reviewImageUrl(awsS3Manager.uploadFile(reviewImage)).build()); - } // 리뷰이미지 -> url -> 엔티티 변환 - tagRepository.saveAll(tagList); - reviewImageRepository.saveAll(reviewImageUrlList); + // todo 이미지가 널값이 아니라면 업로드 실행 + if(newReviewImage != null) { + for (MultipartFile reviewImageFile : newReviewImage) { + String fileDir = awsS3Manager.uploadFile(reviewImageFile); + log.info("upload --> "+ fileDir); + reviewImageUrlList.add(ReviewImage.builder().reviewId(review).reviewImageUrl(fileDir).build()); + } // 리뷰이미지 -> url -> 엔티티 변환 + } - reviewRepository.save(review); - storeRepository.save(store.updatePointAvg(reviewRequestDto.getPoint())); + tagRepository .saveAll(tagList); + reviewImageRepository.saveAll(reviewImageUrlList); + reviewRepository .save(review); // 아래의 {store.updatePointAvg()} 보다 리뷰가 먼저 처리되게 해야한다. + storeRepository.save(store.updatePointAvg(reviewRequestDto.getPoint())); //별점 평균 구하는 코드 } + + public List getReview(int storeId) { Store store = storeRepository.findById(storeId).orElseThrow(() -> new IllegalArgumentException("해당 가게가 없습니다")); return reviewRepository.findAllByStoreId(store) .stream().map(ReviewResponseDto::new).collect(Collectors.toList()); } + + + /** 리뷰 수정 */ public void reviewUpdate(ReviewRequestDto reviewRequestDto, String socialId, int reviewId) throws IOException { -// int storeId = reviewRequestDto.getStoreId(); -// User user = userRepository.findBySocialId(socialId); if(user == null){ new IllegalArgumentException("해당하는 소셜아이디를 찾을 수 없습니다."); } -// Store store = storeRepository.findById(storeId).orElseThrow(() -> new IllegalArgumentException("해당하는 업장이 존재하지 않습니다.")); -// Review review = reviewRepository.findById(reviewId).orElseThrow(() -> new IllegalArgumentException("해당하는 리뷰가 없습니다.")); -// -// -// // 리뷰이미지 변환 -// List imageFileList = reviewRequestDto.getImageFile(); -// List reviewImageUrlList = new ArrayList<>(); -// -// if(imageFileList != null && review.getReviewImageList() != null) { -// int index = 0; -// for (MultipartFile reviewImage : imageFileList) { -// awsS3Manager.deleteFile(review.getReviewImageList().get(index).getReviewImageUrl()); -// reviewImageUrlList.add(ReviewImage.builder().reviewImageUrl(awsS3Manager.uploadFile(reviewImage)).build()); -// index++; -// } -// } else if(imageFileList != null && review.getReviewImageList() != null){ -// -// } -// -// -// review.update(reviewRequestDto); -// List tags = new ArrayList<>(); -// List tagList = reviewRequestDto.getTagList(); -// -// -// if (tagList != null) { -// for (String tagObj : tagList) { -// Tag tag = new Tag(tagObj, review); -// review.addSingleTag(tag); -// tags.add(tag); -// } -// tagRepository.saveAll(tags); -// } -// -// reviewRepository.save(review); // 아래의 {store.updatePointAvg()} 보다 리뷰가 먼저 처리되게 해야한다. -// storeRepository .save(store.updatePointAvg(reviewRequestDto.getPoint())); + int storeId = reviewRequestDto.getStoreId(); + User user = userRepository.findBySocialId(socialId); if(user == null){ new IllegalArgumentException("해당하는 소셜아이디를 찾을 수 없습니다."); } + Store store = storeRepository.findById(storeId) .orElseThrow(() -> new IllegalArgumentException("해당하는 업장이 존재하지 않습니다.")); + Review review = reviewRepository.findById(reviewId) .orElseThrow(() -> new IllegalArgumentException("해당하는 리뷰가 없습니다.")); + + + List newImageFileList = reviewRequestDto.getImageFile(); // 새로운 이미지 파일 + List oldImageFileList = review.getReviewImageList(); // 기존에 이미지 파일 + List reviewImageUrlList = new ArrayList<>(); // 이미지 파일을 담을 리스트 + + + // todo 이미지 삭제 후 수정 작업 (1 -> 2) + // 1.기존리뷰에 기존 이미지가 있다면 삭제 + if(oldImageFileList != null) { + for (ReviewImage reviewImage : oldImageFileList) { + System.out.println("check -> " + reviewImage.getReviewImageUrl()); + System.out.println("delete -> " + reviewImage.getReviewImageUrl().substring(reviewImage.getReviewImageUrl().indexOf("com/") + 4)); + awsS3Manager.deleteFile(reviewImage.getReviewImageUrl().substring(reviewImage.getReviewImageUrl().indexOf("com/") + 4)); + + } + reviewImageRepository.deleteByReviewId(review); + } + + + // 2.수정할 이미지가 있다면 업로드 + if(newImageFileList != null) { + System.out.println("newImageFileList != null"); + for (MultipartFile reviewImageFile : newImageFileList) { + String fileDir = awsS3Manager.uploadFile(reviewImageFile); + System.out.println("update --> " + fileDir); + reviewImageUrlList.add(ReviewImage.builder().reviewId(review).reviewImageUrl(fileDir).build()); + } // 리뷰이미지 -> url -> 엔티티 변환 + } + + + List tagList = new ArrayList<>(); // 태그를 담는 리스트 + List newTagList = reviewRequestDto.getTagList(); // 새로운 태그 + + + // todo 태그 수정 작업 + List oldTagList = tagRepository.findAllByReviewId(review); + System.out.println("log Tag SIZE --> "+ oldTagList.size()); + // 1. 기존 태그 내용이 있다면 전체 삭제 + if (oldTagList != null){ + System.out.println("oldTagList != null"); + tagRepository.deleteAByReviewId(review); } + // 2. 태그 내용이 있다면 태그 수정 + if (newTagList != null) { + for (String newTag : newTagList) { + Tag tag = new Tag(newTag, review); + review.addSingleTag(tag); + tagList.add(tag); + } + tagRepository.saveAll(tagList); + } + + + review.update(reviewRequestDto); + reviewRepository.save(review); // 아래의 store.updatePointAvg() 보다 리뷰가 먼저 처리되게 해야한다. + storeRepository .save(store.updatePointAvg(reviewRequestDto.getPoint())); // 별점 평점 구하는 코드 + reviewImageRepository.saveAll(reviewImageUrlList); } /** 리뷰 삭제 */ public void reviewDelete(String socialId, int reviewId) { + User user = userRepository.findBySocialId(socialId); // 유저 매핑 + Optional review = reviewRepository.findById(reviewId); // 리뷰 매핑 - User user = userRepository.findBySocialId(socialId); - Optional review = reviewRepository.findById(reviewId); - List imageList = review.get().getReviewImageList(); + if(user == null){ new IllegalArgumentException("해당하는 소셜아이디를 찾을 수 없습니다."); } // 유저 유무 확인 예외처리 + review.orElseThrow(() -> new IllegalArgumentException("해당하는 리뷰가 이미 삭제 되었습니다.")); // 리뷰 유무 확인 예외처리 - // 1.유저 유무 예외처리 -> 2.리뷰 유무 예외처리 - if(user == null){ new IllegalArgumentException("해당하는 소셜아이디를 찾을 수 없습니다."); } - review.orElseThrow(() -> new IllegalArgumentException("해당하는 리뷰가 이미 삭제 되었습니다.")); + + List imageList = review.get().getReviewImageList(); - // 1.리뷰삭제 -> 2.이미지 삭제 - reviewRepository.deleteById(review.get().getId()); - if(!review.get().getReviewImageList().isEmpty()) { + // todo 1.리뷰삭제 -> 2.이미지 삭제 + reviewRepository.deleteById(review.get().getId()); // 1 + if(!review.get().getReviewImageList().isEmpty()) { // 2 for (ReviewImage reviewImage : imageList) { - System.out.println(reviewImage.getReviewImageUrl().substring(reviewImage.getReviewImageUrl().indexOf("com/")+4)); + System.out.println("delete -> "+reviewImage.getReviewImageUrl().substring(reviewImage.getReviewImageUrl().indexOf("com/")+4)); awsS3Manager.deleteFile(reviewImage.getReviewImageUrl().substring(reviewImage.getReviewImageUrl().indexOf("com/")+4)); } } diff --git a/src/main/java/com/mpnp/baechelin/tag/repository/TagRepository.java b/src/main/java/com/mpnp/baechelin/tag/repository/TagRepository.java index 4253a03..6020a69 100644 --- a/src/main/java/com/mpnp/baechelin/tag/repository/TagRepository.java +++ b/src/main/java/com/mpnp/baechelin/tag/repository/TagRepository.java @@ -1,7 +1,17 @@ package com.mpnp.baechelin.tag.repository; +import com.mpnp.baechelin.review.domain.Review; import com.mpnp.baechelin.tag.domain.Tag; +import com.mpnp.baechelin.user.domain.User; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; +import java.util.Optional; + public interface TagRepository extends JpaRepository { + void deleteAllByReviewId(Review review); + + List findAllByReviewId(Review review); + + void deleteAByReviewId(Review review); }