From 055774fa97c394b0d7b0007221148389b89a9ced Mon Sep 17 00:00:00 2001 From: JSoi Date: Fri, 5 Aug 2022 05:51:35 +0900 Subject: [PATCH] =?UTF-8?q?#28=20[Add]=20=EB=B6=81=EB=A7=88=ED=81=AC=20?= =?UTF-8?q?=ED=8F=B4=EB=8D=94=20=EC=8D=B8=EB=84=A4=EC=9D=BC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80,=20Folder=20API=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookmark/controller/FolderController.java | 3 ++- .../bookmark/dto/FolderResponseDto.java | 3 --- .../bookmark/repository/BookmarkRepository.java | 7 +++---- .../bookmark/repository/FolderRepository.java | 2 ++ .../bookmark/service/FolderService.java | 17 +++++++++++++---- .../store/repository/StoreImgRepository.java | 2 -- 6 files changed, 20 insertions(+), 14 deletions(-) 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 19698d6..81dde4c 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/controller/FolderController.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/controller/FolderController.java @@ -62,7 +62,8 @@ public SuccessResponse folderUpdate (@PathVariable int folderId, /** 폴더 리스트 */ @GetMapping("/folderList") public List folderList (@AuthenticationPrincipal User user){ - return folderService.folderList(user.getUsername()); +// return folderService.folderList(user.getUsername()); + return folderService.folderListV2(user.getUsername()); } /** 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 50846be..62cd492 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/dto/FolderResponseDto.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/dto/FolderResponseDto.java @@ -18,7 +18,6 @@ public class FolderResponseDto { private int id; private String folderName; private String thumbNail; - private List bookmarkList; public FolderResponseDto(Folder folder) { @@ -38,13 +37,11 @@ public static FolderResponseDto FolderDtoRes(Folder folder) { } } else if(folder.getBookmarkList() == null) { - return FolderResponseDto.builder() .folderName(folder.getFolderName()) .id(folder.getId()) .thumbNail(null) .build(); - } return FolderResponseDto.builder() 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 bdfb0f2..6029bc7 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/repository/BookmarkRepository.java @@ -19,9 +19,8 @@ public interface BookmarkRepository extends CrudRepository { 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); + + @Query(value = "select b.store_id from bookmark b where b.folder_id=:folderId order by b.created_at desc limit 1;", nativeQuery = true) + Long findLatestStore(@Param("folderId") int folderId); } \ No newline at end of file diff --git a/src/main/java/com/mpnp/baechelin/bookmark/repository/FolderRepository.java b/src/main/java/com/mpnp/baechelin/bookmark/repository/FolderRepository.java index 201df21..6dc5416 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/repository/FolderRepository.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/repository/FolderRepository.java @@ -3,6 +3,8 @@ import com.mpnp.baechelin.bookmark.domain.Folder; import com.mpnp.baechelin.user.domain.User; 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; 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 51c3556..cc56a08 100644 --- a/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java +++ b/src/main/java/com/mpnp/baechelin/bookmark/service/FolderService.java @@ -10,6 +10,9 @@ import com.mpnp.baechelin.exception.CustomException; import com.mpnp.baechelin.exception.ErrorCode; +import com.mpnp.baechelin.store.domain.Store; +import com.mpnp.baechelin.store.domain.StoreImage; +import com.mpnp.baechelin.store.repository.StoreRepository; import com.mpnp.baechelin.user.domain.User; import com.mpnp.baechelin.user.repository.UserRepository; import lombok.RequiredArgsConstructor; @@ -28,6 +31,8 @@ public class FolderService { private final FolderRepository folderRepository; private final UserRepository userRepository; private final BookmarkRepository bookmarkRepository; + private final StoreRepository storeRepository; + /** * 폴더 생성 */ @@ -74,12 +79,11 @@ public List folderList(String socialId) { List folderResponseDtoList = new ArrayList<>(); for (Folder obj : user.getFolderList()) { FolderResponseDto folderResponseDto = FolderResponseDto.FolderDtoRes(obj); -// List latestFolder = bookmarkRepository.findLatestFolder(obj.getId()); -// folderResponseDto.setThumbNail(); folderResponseDtoList.add(folderResponseDto); } return folderResponseDtoList; } + @Transactional(readOnly = true) public List folderListV2(String socialId) { User user = userRepository.findBySocialId(socialId); @@ -89,8 +93,13 @@ public List folderListV2(String socialId) { List folderResponseDtoList = new ArrayList<>(); for (Folder obj : user.getFolderList()) { FolderResponseDto folderResponseDto = FolderResponseDto.FolderDtoRes(obj); - List latestFolder = bookmarkRepository.findLatestFolder(obj.getId()); - //TODO ThumbNail + // 폴더의 최신 업장 찾기 + Long latestStoreId = bookmarkRepository.findLatestStore(obj.getId()); + if (latestStoreId != null) { + Store latestStore = storeRepository.findById(latestStoreId).orElseThrow(() -> new CustomException(ErrorCode.NO_STORE_FOUND)); + Optional storeImage = latestStore.getStoreImageList().stream().findFirst(); + storeImage.ifPresent(image -> folderResponseDto.setThumbNail(image.getStoreImageUrl())); + } folderResponseDtoList.add(folderResponseDto); } 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 82e827e..256c554 100644 --- a/src/main/java/com/mpnp/baechelin/store/repository/StoreImgRepository.java +++ b/src/main/java/com/mpnp/baechelin/store/repository/StoreImgRepository.java @@ -10,8 +10,6 @@ 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);