From e91481c87451a0461f23f37b6ba0888138c3e2fa Mon Sep 17 00:00:00 2001 From: Jihun Kim Date: Tue, 29 Jul 2025 20:43:23 +0900 Subject: [PATCH] =?UTF-8?q?fix(Bookmark):=20=EB=B6=81=EB=A7=88=ED=81=AC=20?= =?UTF-8?q?=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookmark/service/BookmarkService.java | 19 +++++++++++++------ .../repository/BookmarkRepository.java | 4 +++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/nowait-app-user-api/src/main/java/com/nowait/applicationuser/bookmark/service/BookmarkService.java b/nowait-app-user-api/src/main/java/com/nowait/applicationuser/bookmark/service/BookmarkService.java index 4b2da27d..d2c383af 100644 --- a/nowait-app-user-api/src/main/java/com/nowait/applicationuser/bookmark/service/BookmarkService.java +++ b/nowait-app-user-api/src/main/java/com/nowait/applicationuser/bookmark/service/BookmarkService.java @@ -13,6 +13,7 @@ import com.nowait.applicationuser.store.dto.StorePageReadResponse; import com.nowait.applicationuser.store.service.StoreService; import com.nowait.domaincorerdb.store.entity.Store; +import com.nowait.domaincorerdb.store.exception.StoreNotFoundException; import com.nowait.domaincorerdb.store.repository.StoreRepository; import com.nowait.domaincorerdb.user.entity.User; import com.nowait.domaincorerdb.user.exception.UserNotFoundException; @@ -37,17 +38,18 @@ public BookmarkCreateResponse createBookmark(Long storeId, CustomOAuth2User cust parameterValidation(storeId, customOAuth2User); Store store = storeRepository.findById(storeId) - .orElseThrow(() -> new EntityNotFoundException(storeId + " store not found.")); + .orElseThrow(StoreNotFoundException::new); User user = userRepository.findById(customOAuth2User.getUserId()) - .orElseThrow(() -> new EntityNotFoundException("User not found")); + .orElseThrow(UserNotFoundException::new); - Optional isBookmark = bookmarkRepository.findRawByUserAndStoreAndDeletedFalse(user, store); + Optional isBookmark = bookmarkRepository.findByUserAndStore(user, store); if (isBookmark.isPresent()) { Bookmark bookmark = isBookmark.get(); if (bookmark.isDeleted()) { bookmark.restore(); - return BookmarkCreateResponse.fromEntity(bookmarkRepository.save(bookmark)); + Bookmark restored = bookmarkRepository.save(bookmark); + return BookmarkCreateResponse.fromEntity(bookmarkRepository.save(restored)); } else { throw new IllegalArgumentException("already bookmarked"); } @@ -81,8 +83,13 @@ public List getBookmarks(CustomOAuth2User customOAuth2Use @Transactional public String deleteBookmark(Long storeId, CustomOAuth2User customOAuth2User) { parameterValidation(storeId, customOAuth2User); - Bookmark bookmark = bookmarkRepository.findActiveByUserIdAndStoreId(storeId, customOAuth2User.getUserId()) - .orElseThrow(() -> new EntityNotFoundException(storeId + " bookmark not found.")); + User user = customOAuth2User.getUser(); + Store store = storeRepository.findById(storeId) + .orElseThrow(StoreNotFoundException::new); + + Bookmark bookmark = bookmarkRepository.findByUserAndStoreAndDeletedFalse(user, store) + .orElseThrow(() -> new EntityNotFoundException(storeId + " 활성화된 bookmark가 없습니다.")); + if (!Objects.equals(bookmark.getUser().getId(), customOAuth2User.getUserId())) { throw new IllegalArgumentException("you can only delete your own bookmark"); } diff --git a/nowait-domain/domain-user-rdb/src/main/java/com/nowait/domainuserrdb/bookmark/repository/BookmarkRepository.java b/nowait-domain/domain-user-rdb/src/main/java/com/nowait/domainuserrdb/bookmark/repository/BookmarkRepository.java index 53914f5c..bac91e52 100644 --- a/nowait-domain/domain-user-rdb/src/main/java/com/nowait/domainuserrdb/bookmark/repository/BookmarkRepository.java +++ b/nowait-domain/domain-user-rdb/src/main/java/com/nowait/domainuserrdb/bookmark/repository/BookmarkRepository.java @@ -17,7 +17,9 @@ public interface BookmarkRepository extends JpaRepository { boolean existsByUserAndStoreAndDeletedFalse(User user, Store store); - Optional findRawByUserAndStoreAndDeletedFalse(User user, Store store); + Optional findByUserAndStoreAndDeletedFalse(User user, Store store); + + Optional findByUserAndStore(User user, Store store); Collection findAllByUserAndDeletedFalse(User user);