From 2eff2c03b1e76457bc4522bb652cd9c5c00c3b5a Mon Sep 17 00:00:00 2001 From: kokoa322 Date: Thu, 28 Jul 2022 14:45:13 +0900 Subject: [PATCH] =?UTF-8?q?#28=20[update]=20=EC=B5=9C=EA=B7=BC=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D=ED=95=9C=20=EB=B6=81=EB=A7=88=ED=81=AC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BookmarkController.java | 14 +++++++ .../bookmark/dto/BookmarkInfoDto.java | 24 +++++++++-- .../repository/BookmarkRepository.java | 4 ++ .../bookmark/service/BookmarkService.java | 42 ++++++++++++++++++- .../repository/ReviewImageRepository.java | 7 +++- .../review/service/ReviewService.java | 18 ++++---- .../store/repository/StoreImgRepository.java | 9 ++++ .../store/repository/StoreRepository.java | 2 + .../tag/repository/TagRepository.java | 1 - 9 files changed, 105 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java b/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java index 594d926..1255a7e 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java @@ -1,8 +1,12 @@ package com.mpnp.baechelin.bookmark.controller; +import com.mpnp.baechelin.bookmark.dto.BookmarkInfoDto; import com.mpnp.baechelin.bookmark.dto.BookmarkRequestDto; import com.mpnp.baechelin.bookmark.service.BookmarkService; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -10,6 +14,8 @@ import org.springframework.security.core.userdetails.User; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RequiredArgsConstructor @RestController public class BookmarkController { @@ -35,4 +41,12 @@ public ResponseEntity bookmarkDelete(@PathVariable int bookmarkId, return new ResponseEntity<>(HttpStatus.OK); } + + @GetMapping("/bookmarkTop") + public ResponseEntity bookmarkTop(@AuthenticationPrincipal User user, + @PageableDefault(page = 0, size = 5, sort = "id", direction = Sort.Direction.DESC) Pageable pageable){ + + List bookmarkList = bookmarkService.bookmarkTop(user.getUsername(), pageable); + return new ResponseEntity<>(bookmarkList, HttpStatus.OK); + } } diff --git a/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkInfoDto.java b/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkInfoDto.java index b5fb24e..d3be0ea 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkInfoDto.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/dto/BookmarkInfoDto.java @@ -1,10 +1,9 @@ package com.mpnp.baechelin.bookmark.dto; -import com.mpnp.baechelin.store.domain.StoreImage; +import com.mpnp.baechelin.bookmark.domain.Bookmark; import lombok.*; -import java.util.List; @Getter @Setter @@ -18,7 +17,26 @@ public class BookmarkInfoDto { private String address; private String category; private String phoneNumber; + private String storeImageList; private int bookmarkId; private int storeId; - String storeImageList; + + + public BookmarkInfoDto(Bookmark bookmark){ + + this.pointAvg = Math.round(bookmark.getStoreId().getPointAvg()*10)/10.0; + this.name = bookmark.getStoreId().getName(); + this.address = bookmark.getStoreId().getAddress(); + this.category = bookmark.getStoreId().getCategory(); + this.phoneNumber = bookmark.getStoreId().getPhoneNumber(); + this.bookmarkId = bookmark.getId(); + this.storeId = (int) bookmark.getStoreId().getId(); + + if(bookmark.getStoreId().getStoreImageList() == null) { + this.storeImageList = bookmark.getStoreId().getStoreImageList().get(0).getStoreImageUrl(); + + } else if(bookmark.getStoreId().getStoreImageList() != null) { + this.storeImageList = ""; + } + } } diff --git a/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java b/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java index 00d463f..11b5dec 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java @@ -4,6 +4,8 @@ import com.mpnp.baechelin.bookmark.domain.Folder; import com.mpnp.baechelin.store.domain.Store; import com.mpnp.baechelin.user.domain.User; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; @@ -11,4 +13,6 @@ public interface BookmarkRepository extends JpaRepository { List findAllByFolderId(Folder folderId); boolean existsByStoreIdAndUserId(Store store, User user); + + Page findAllByUserId(User user, Pageable pageable); } \ No newline at end of file diff --git a/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java b/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java index d3caf4f..5a9a2d6 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java @@ -2,16 +2,23 @@ import com.mpnp.baechelin.bookmark.domain.Bookmark; import com.mpnp.baechelin.bookmark.domain.Folder; +import com.mpnp.baechelin.bookmark.dto.BookmarkInfoDto; import com.mpnp.baechelin.bookmark.dto.BookmarkRequestDto; import com.mpnp.baechelin.bookmark.repository.BookmarkRepository; import com.mpnp.baechelin.bookmark.repository.FolderRepository; import com.mpnp.baechelin.store.domain.Store; +import com.mpnp.baechelin.store.domain.StoreImage; +import com.mpnp.baechelin.store.repository.StoreImgRepository; import com.mpnp.baechelin.store.repository.StoreRepository; import com.mpnp.baechelin.user.domain.User; import com.mpnp.baechelin.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; @Service @@ -22,7 +29,8 @@ public class BookmarkService { private final FolderRepository folderRepository; private final StoreRepository storeRepository; private final UserRepository userRepository; - + private final StoreImgRepository storeImgRepository; +// private final StoreImageRepository storeImageRepository; public void bookmark(BookmarkRequestDto bookmarkRequestDto, String socialId) { Folder folder = folderRepository.findById(bookmarkRequestDto.getFolderId()).orElseThrow(()-> new IllegalArgumentException("폴더가 존재하지 않습니다")); @@ -47,4 +55,36 @@ public void bookmarkDelete(int bookmarkId, String socialId) { bookmarkRepository.deleteById(bookmarkId); } + + public List bookmarkTop(String socialId, Pageable pageable) { + + User user = userRepository.findBySocialId(socialId); + Page bookmarkPage = bookmarkRepository.findAllByUserId(user, pageable); + + + List bookmarkList = new ArrayList<>(); + for(Bookmark bookmark: bookmarkPage){ + Optional store = storeRepository.findById(bookmark.getStoreId().getId()); + List storeImageList = storeImgRepository.findAllByStoreId(store.get().getId()); + + BookmarkInfoDto bookmarkInfoDto = BookmarkInfoDto + .builder() + .bookmarkId(bookmark.getId()) + .name(store.get().getName()) + .phoneNumber(store.get().getPhoneNumber()) + .category(store.get().getCategory()) + .address(store.get().getAddress()) + .pointAvg(Math.round(store.get().getPointAvg()*10)/10.0) + .storeId((int) store.get().getId()) + .storeImageList(!storeImageList.isEmpty() ? storeImageList.get(0).getStoreImageUrl():"") + .build(); + +// BookmarkInfoDto bookmarkInfoDto = new BookmarkInfoDto(bookmark); + bookmarkList.add(bookmarkInfoDto); + } + + + + return bookmarkList; + } } 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 22887c7..cc17155 100644 --- a/src/main/java/com/mpnp/baechelin/review/repository/ReviewImageRepository.java +++ b/src/main/java/com/mpnp/baechelin/review/repository/ReviewImageRepository.java @@ -4,9 +4,12 @@ import com.mpnp.baechelin.review.domain.ReviewImage; import org.springframework.data.jpa.repository.JpaRepository; -import java.util.List; - public interface ReviewImageRepository extends JpaRepository { + void deleteAllByReviewId(Review review); void deleteByReviewId(Review review); + + void deleteAllInBatchByReviewId(Review review); + + void deleteInBatchByReviewId(Review review); } 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 151f8f8..ec60955 100644 --- a/src/main/java/com/mpnp/baechelin/review/service/ReviewService.java +++ b/src/main/java/com/mpnp/baechelin/review/service/ReviewService.java @@ -18,8 +18,10 @@ import com.mpnp.baechelin.util.AwsS3Manager; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.hibernate.annotations.SQLDelete; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -85,16 +87,16 @@ public void review(ReviewRequestDto reviewRequestDto, String socialId) throws IO } + /** 리뷰 조회 */ public PageInfoResponseDto getReview(long storeId, String socialId, Pageable pageable) { - Store store = storeRepository.findById(storeId).orElseThrow(() -> new IllegalArgumentException("해당 가게가 없습니다")); + User myUser = userRepository.findBySocialId(socialId); + Store store = storeRepository.findById(storeId).orElseThrow(() -> new IllegalArgumentException("해당 가게가 없습니다")); Page reviewList = reviewRepository.findAllByStoreId(store, pageable); - List reviewResponseDtoList = new ArrayList<>(); - User myUser = userRepository.findBySocialId(socialId); - + List reviewResponseDtoList = new ArrayList<>(); for(Review review: reviewList){ ReviewResponseDto reviewResponseDto = new ReviewResponseDto(review); Optional user = userRepository.findById(reviewResponseDto.getUserId()); @@ -102,7 +104,6 @@ public PageInfoResponseDto getReview(long storeId, String socialId, Pageable pag reviewResponseDtoList.add(reviewResponseDto); } - reviewList.isFirst(); PageInfoResponseDto pageInfoResponseDto = PageInfoResponseDto .builder() @@ -120,8 +121,8 @@ public PageInfoResponseDto getReview(long storeId, String socialId, Pageable pag } - @Transactional + @Modifying /** 리뷰 수정 */ public void reviewUpdate(ReviewRequestDto reviewRequestDto, String socialId, int reviewId) throws IOException { @@ -145,8 +146,7 @@ public void reviewUpdate(ReviewRequestDto reviewRequestDto, String socialId, int awsS3Manager.deleteFile(reviewImage.getReviewImageUrl().substring(reviewImage.getReviewImageUrl().indexOf("com/") + 4)); } - System.out.println(review.getId()); - reviewImageRepository.deleteAllByReviewId(review); + reviewImageRepository.deleteInBatchByReviewId(review); } @@ -170,7 +170,7 @@ public void reviewUpdate(ReviewRequestDto reviewRequestDto, String socialId, int // 1. 기존 태그 내용이 있다면 전체 삭제 if (oldTagList != null){ System.out.println("oldTagList != null"); - tagRepository.deleteAByReviewId(review); } + tagRepository.deleteAllByReviewId(review); } // 2. 태그 내용이 있다면 태그 수정 if (newTagList != null) { for (String newTag : newTagList) { diff --git a/src/main/java/com/mpnp/baechelin/store/repository/StoreImgRepository.java b/src/main/java/com/mpnp/baechelin/store/repository/StoreImgRepository.java index 9c67787..6e6bbbe 100644 --- a/src/main/java/com/mpnp/baechelin/store/repository/StoreImgRepository.java +++ b/src/main/java/com/mpnp/baechelin/store/repository/StoreImgRepository.java @@ -1,7 +1,16 @@ package com.mpnp.baechelin.store.repository; +import com.mpnp.baechelin.store.domain.Store; import com.mpnp.baechelin.store.domain.StoreImage; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; +import java.util.Optional; public interface StoreImgRepository extends JpaRepository { + + List findAllByStoreId(Long storeId); + + StoreImage findByStoreId(long storeId); } diff --git a/src/main/java/com/mpnp/baechelin/store/repository/StoreRepository.java b/src/main/java/com/mpnp/baechelin/store/repository/StoreRepository.java index 1d29124..9e59a3e 100644 --- a/src/main/java/com/mpnp/baechelin/store/repository/StoreRepository.java +++ b/src/main/java/com/mpnp/baechelin/store/repository/StoreRepository.java @@ -1,10 +1,12 @@ package com.mpnp.baechelin.store.repository; import com.mpnp.baechelin.store.domain.Store; +import com.mpnp.baechelin.store.domain.StoreImage; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; public interface StoreRepository extends JpaRepository { } \ No newline at end of file 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 6020a69..af296be 100644 --- a/src/main/java/com/mpnp/baechelin/tag/repository/TagRepository.java +++ b/src/main/java/com/mpnp/baechelin/tag/repository/TagRepository.java @@ -13,5 +13,4 @@ public interface TagRepository extends JpaRepository { List findAllByReviewId(Review review); - void deleteAByReviewId(Review review); }