From 24afde35ac3985bf3a5c5d30a512edb04219fa0d Mon Sep 17 00:00:00 2001 From: JSoi Date: Fri, 5 Aug 2022 04:42:45 +0900 Subject: [PATCH] =?UTF-8?q?#28=20[Fix]=20=EB=B6=81=EB=A7=88=ED=81=AC=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/BookmarkController.java | 22 +++---------------- .../bookmark/controller/FolderController.java | 22 +++++++++++++++++++ .../bookmark/dto/FolderResponseDto.java | 4 +++- .../repository/BookmarkRepository.java | 8 +++++++ .../bookmark/service/BookmarkService.java | 8 +++---- .../bookmark/service/FolderService.java | 9 ++++++-- .../store/repository/StoreImgRepository.java | 4 +++- 7 files changed, 50 insertions(+), 27 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 aa37393..1740079 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/controller/BookmarkController.java @@ -40,32 +40,16 @@ public SuccessResponse bookmark(@RequestBody BookmarkRequestDto bookmarkRequestD return new SuccessResponse("북마크를 폴더에 저장 완료"); } - @DeleteMapping("/bookmark/{bookmarkId}") - public SuccessResponse bookmarkDelete(@PathVariable int bookmarkId, + @DeleteMapping("/bookmark/{storeId}") + public SuccessResponse bookmarkDelete(@PathVariable Long storeId, @AuthenticationPrincipal User user) { if (user == null) { throw new CustomException(ErrorCode.NO_USER_FOUND); } - bookmarkService.bookmarkDelete(bookmarkId, user.getUsername()); + bookmarkService.bookmarkDelete(storeId, user.getUsername()); return new SuccessResponse("북마크를 삭제 완료"); } - /** - * @param folderId 조회하고자 하는 폴더 아이디 - * @param pageable 페이징 객체 - * @param user 접근하는 유저 - * @return 페이징된 객체와 List를 안에 넣어 리턴한다 - bookmark는 항상 "Y" - */ - @GetMapping("/bookmark/{folderId}") - public BookmarkPagedResponseDto bookmarkList(@PathVariable int folderId, - @PageableDefault(size = 20, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable, - @AuthenticationPrincipal User user) { - if (user == null) { - throw new CustomException(ErrorCode.NO_USER_FOUND); - } - return bookmarkService.bookmarkList(user.getUsername(), folderId, pageable); - } - @GetMapping("/bookmarkTop") public ResponseEntity> bookmarkTop(@AuthenticationPrincipal User user, @PageableDefault(page = 0, size = 5, sort = "id", direction = Sort.Direction.DESC) Pageable pageable) { diff --git a/src/main/java/com/mpnp/baechelin/bookmark/controller/FolderController.java b/src/main/java/com/mpnp/baechelin/bookmark/controller/FolderController.java index a0d49b3..19698d6 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/controller/FolderController.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/controller/FolderController.java @@ -1,13 +1,18 @@ package com.mpnp.baechelin.bookmark.controller; +import com.mpnp.baechelin.bookmark.dto.BookmarkPagedResponseDto; import com.mpnp.baechelin.bookmark.dto.FolderRequestDto; import com.mpnp.baechelin.bookmark.dto.FolderResponseDto; +import com.mpnp.baechelin.bookmark.service.BookmarkService; import com.mpnp.baechelin.bookmark.service.FolderService; import com.mpnp.baechelin.common.SuccessResponse; import com.mpnp.baechelin.exception.CustomException; import com.mpnp.baechelin.exception.ErrorCode; 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.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; @@ -21,6 +26,7 @@ public class FolderController { private final FolderService folderService; + private final BookmarkService bookmarkService; /** 폴더 신규 생성 */ @PostMapping("/folder") @@ -58,4 +64,20 @@ public SuccessResponse folderUpdate (@PathVariable int folderId, public List folderList (@AuthenticationPrincipal User user){ return folderService.folderList(user.getUsername()); } + + /** + * @param folderId 조회하고자 하는 폴더 아이디 + * @param pageable 페이징 객체 + * @param user 접근하는 유저 + * @return 페이징된 객체와 List를 안에 넣어 리턴한다 - bookmark는 항상 "Y" + */ + @GetMapping("/folder/{folderId}") + public BookmarkPagedResponseDto bookmarkList(@PathVariable int folderId, + @PageableDefault(size = 20, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable, + @AuthenticationPrincipal User user) { + if (user == null) { + throw new CustomException(ErrorCode.NO_USER_FOUND); + } + return bookmarkService.bookmarkList(user.getUsername(), folderId, pageable); + } } diff --git a/src/main/java/com/mpnp/baechelin/bookmark/dto/FolderResponseDto.java b/src/main/java/com/mpnp/baechelin/bookmark/dto/FolderResponseDto.java index 941e615..50846be 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/dto/FolderResponseDto.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/dto/FolderResponseDto.java @@ -17,6 +17,7 @@ public class FolderResponseDto { private int id; private String folderName; + private String thumbNail; private List bookmarkList; @@ -32,7 +33,6 @@ public static FolderResponseDto FolderDtoRes(Folder folder) { if(folder.getBookmarkList() != null) { for (Bookmark bookmark : folder.getBookmarkList()) { - BookmarkInfoDto bookmarkInfoDto = new BookmarkInfoDto(bookmark); bookmarks.add(bookmarkInfoDto); @@ -42,6 +42,7 @@ public static FolderResponseDto FolderDtoRes(Folder folder) { return FolderResponseDto.builder() .folderName(folder.getFolderName()) .id(folder.getId()) + .thumbNail(null) .build(); } @@ -50,6 +51,7 @@ public static FolderResponseDto FolderDtoRes(Folder folder) { .folderName(folder.getFolderName()) .id(folder.getId()) .bookmarkList(bookmarks) + .thumbNail(null) .build(); } } 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 684e9e8..bdfb0f2 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java @@ -7,13 +7,21 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; import java.util.List; +import java.util.Optional; public interface BookmarkRepository extends CrudRepository { Page findAllByFolderId(Folder folderId, Pageable pageable); boolean existsByStoreIdAndUserId(Store store, User user); + Optional findByStoreIdAndUserId(Store store, User user); + + @Query("select b.storeId from Bookmark b where b.folderId=:folderId order by b.createdAt desc") + List findLatestFolder(@Param("folderId") int folderId); + 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 871e2f3..6cb122d 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/service/BookmarkService.java @@ -57,12 +57,12 @@ public void bookmark(BookmarkRequestDto bookmarkRequestDto, String socialId) { } } @Transactional - public void bookmarkDelete(int bookmarkId, String socialId) { + public void bookmarkDelete(Long storeId, String socialId) { User user = userRepository.findBySocialId(socialId); if(user == null) { throw new CustomException(ErrorCode.NO_USER_FOUND); } - Bookmark bookmark = bookmarkRepository.findById(bookmarkId).orElseThrow(() -> new CustomException(ErrorCode.NO_BOOKMARK_FOUND)); - Store store = bookmark.getStoreId(); + Store store = storeRepository.findById(storeId).orElseThrow(()-> new CustomException(ErrorCode.NO_STORE_FOUND)); + Bookmark bookmark = bookmarkRepository.findByStoreIdAndUserId(store, user).orElseThrow(() -> new CustomException(ErrorCode.NO_BOOKMARK_FOUND)); store.removeBookmark(bookmark); - bookmarkRepository.deleteById(bookmarkId); + bookmarkRepository.delete(bookmark); storeService.updateBookmarkCnt(store,socialId); } diff --git a/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java b/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java index 4e4151f..923fa28 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java @@ -27,7 +27,7 @@ public class FolderService { private final FolderRepository folderRepository; private final UserRepository userRepository; - + private final BookmarkRepository bookmarkRepository; /** * 폴더 생성 */ @@ -73,8 +73,13 @@ public List folderList(String socialId) { } List folderResponseDtoList = new ArrayList<>(); for (Folder obj : user.getFolderList()) { - folderResponseDtoList.add(FolderResponseDto.FolderDtoRes(obj)); + FolderResponseDto folderResponseDto = FolderResponseDto.FolderDtoRes(obj); + List latestFolder = bookmarkRepository.findLatestFolder(obj.getId()); +// folderResponseDto.setThumbNail(); +// folderResponseDtoList.add(); + } return folderResponseDtoList; } + } 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 6e6bbbe..82e827e 100644 --- a/src/main/java/com/mpnp/baechelin/store/repository/StoreImgRepository.java +++ b/src/main/java/com/mpnp/baechelin/store/repository/StoreImgRepository.java @@ -4,12 +4,14 @@ import com.mpnp.baechelin.store.domain.StoreImage; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; import java.util.Optional; public interface StoreImgRepository extends JpaRepository { - + @Query("select si.storeImageUrl from StoreImage si where si.store=:storeId") + String findLatestImage(@Param("storeId") int storeId); List findAllByStoreId(Long storeId); StoreImage findByStoreId(long storeId);