From 32c2d6322fe19fb6917e9cf52cf0dfd5e55b8c47 Mon Sep 17 00:00:00 2001 From: Photogrammer Date: Fri, 23 Aug 2024 18:16:14 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20bookmark=20duplicated=20name=20exception?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookmark/controller/BookmarkController.java | 6 ++++-- .../server/bookmark/dao/BookmarkRepository.java | 2 ++ .../talkka/server/bookmark/dto/BookmarkRespDto.java | 6 ++---- .../exception/DuplicatedBookmarkNameException.java | 9 +++++++++ .../server/bookmark/service/BookmarkService.java | 11 +++++++++-- 5 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 server/src/main/java/com/talkka/server/bookmark/exception/DuplicatedBookmarkNameException.java diff --git a/server/src/main/java/com/talkka/server/bookmark/controller/BookmarkController.java b/server/src/main/java/com/talkka/server/bookmark/controller/BookmarkController.java index eda4cfcb..5f106a5c 100644 --- a/server/src/main/java/com/talkka/server/bookmark/controller/BookmarkController.java +++ b/server/src/main/java/com/talkka/server/bookmark/controller/BookmarkController.java @@ -19,6 +19,7 @@ import com.talkka.server.bookmark.dto.BookmarkRespDto; import com.talkka.server.bookmark.exception.BookmarkNotFoundException; import com.talkka.server.bookmark.exception.BookmarkUserNotFoundException; +import com.talkka.server.bookmark.exception.DuplicatedBookmarkNameException; import com.talkka.server.bookmark.exception.enums.InvalidTransportTypeEnumException; import com.talkka.server.bookmark.service.BookmarkService; import com.talkka.server.oauth.domain.OAuth2UserInfo; @@ -65,7 +66,8 @@ public ResponseEntity createBookmark(@AuthenticationPrincipal OAuth2UserInfo try { BookmarkRespDto bookmark = bookmarkService.createBookmark(bookmarkReqDto, oAuth2UserInfo.getUserId()); return ResponseEntity.ok(bookmark); - } catch (BookmarkUserNotFoundException | InvalidTransportTypeEnumException exception) { + } catch (BookmarkUserNotFoundException | InvalidTransportTypeEnumException | + DuplicatedBookmarkNameException exception) { response = ResponseEntity.badRequest().body(exception.getMessage()); } return response; @@ -80,7 +82,7 @@ public ResponseEntity updateBookmark(@AuthenticationPrincipal OAuth2UserInfo BookmarkRespDto bookmark = bookmarkService.updateBookmark(bookmarkReqDto, oAuth2UserInfo.getUserId(), bookmarkId); response = ResponseEntity.ok(bookmark); - } catch (BookmarkNotFoundException | BookmarkUserNotFoundException + } catch (BookmarkNotFoundException | BookmarkUserNotFoundException | DuplicatedBookmarkNameException | InvalidTransportTypeEnumException exception) { response = ResponseEntity.badRequest().body(exception.getMessage()); } catch (ContentAccessException exception) { diff --git a/server/src/main/java/com/talkka/server/bookmark/dao/BookmarkRepository.java b/server/src/main/java/com/talkka/server/bookmark/dao/BookmarkRepository.java index 1f3274fa..03154162 100644 --- a/server/src/main/java/com/talkka/server/bookmark/dao/BookmarkRepository.java +++ b/server/src/main/java/com/talkka/server/bookmark/dao/BookmarkRepository.java @@ -7,5 +7,7 @@ public interface BookmarkRepository extends JpaRepository { List findByUserId(Long userId); + boolean existsByNameAndUserId(String name, Long userId); + boolean existsByIdAndUserId(Long bookmarkId, Long userId); } diff --git a/server/src/main/java/com/talkka/server/bookmark/dto/BookmarkRespDto.java b/server/src/main/java/com/talkka/server/bookmark/dto/BookmarkRespDto.java index b945ba0b..9944a447 100644 --- a/server/src/main/java/com/talkka/server/bookmark/dto/BookmarkRespDto.java +++ b/server/src/main/java/com/talkka/server/bookmark/dto/BookmarkRespDto.java @@ -3,20 +3,18 @@ import java.util.List; import com.talkka.server.bookmark.dao.BookmarkEntity; -import com.talkka.server.user.dto.UserDto; -import com.talkka.server.user.dto.UserRespDto; public record BookmarkRespDto( Long id, String name, - UserRespDto user, + Long userId, List details ) { public static BookmarkRespDto of(BookmarkEntity bookmark) { return new BookmarkRespDto( bookmark.getId(), bookmark.getName(), - UserRespDto.of(UserDto.of(bookmark.getUser())), + bookmark.getUser().getId(), bookmark.getDetails().stream() .map(BookmarkDetailRespDto::of) .toList() diff --git a/server/src/main/java/com/talkka/server/bookmark/exception/DuplicatedBookmarkNameException.java b/server/src/main/java/com/talkka/server/bookmark/exception/DuplicatedBookmarkNameException.java new file mode 100644 index 00000000..1930fe34 --- /dev/null +++ b/server/src/main/java/com/talkka/server/bookmark/exception/DuplicatedBookmarkNameException.java @@ -0,0 +1,9 @@ +package com.talkka.server.bookmark.exception; + +public class DuplicatedBookmarkNameException extends RuntimeException { + private static final String MESSAGE = "이미 존재하는 북마크 이름입니다."; + + public DuplicatedBookmarkNameException() { + super(MESSAGE); + } +} diff --git a/server/src/main/java/com/talkka/server/bookmark/service/BookmarkService.java b/server/src/main/java/com/talkka/server/bookmark/service/BookmarkService.java index 4c964621..4f2c3875 100644 --- a/server/src/main/java/com/talkka/server/bookmark/service/BookmarkService.java +++ b/server/src/main/java/com/talkka/server/bookmark/service/BookmarkService.java @@ -11,6 +11,7 @@ import com.talkka.server.bookmark.dto.BookmarkRespDto; import com.talkka.server.bookmark.exception.BookmarkNotFoundException; import com.talkka.server.bookmark.exception.BookmarkUserNotFoundException; +import com.talkka.server.bookmark.exception.DuplicatedBookmarkNameException; import com.talkka.server.bookmark.exception.enums.InvalidTransportTypeEnumException; import com.talkka.server.common.validator.ContentAccessValidator; import com.talkka.server.review.exception.ContentAccessException; @@ -48,7 +49,11 @@ public List getBookmarkByUserId(Long userId) { @Transactional public BookmarkRespDto createBookmark(BookmarkReqDto dto, Long userId) throws BookmarkUserNotFoundException, + DuplicatedBookmarkNameException, InvalidTransportTypeEnumException { + if (bookmarkRepository.existsByNameAndUserId(dto.name(), userId)) { + throw new DuplicatedBookmarkNameException(); + } UserEntity user = userRepository.findById(userId).orElseThrow(BookmarkUserNotFoundException::new); BookmarkEntity bookmark = dto.toEntity(user); dto.details().stream() @@ -62,8 +67,11 @@ public BookmarkRespDto updateBookmark(BookmarkReqDto dto, Long userId, Long book BookmarkUserNotFoundException, BookmarkNotFoundException, InvalidTransportTypeEnumException, + DuplicatedBookmarkNameException, ContentAccessException { - + if (bookmarkRepository.existsByNameAndUserId(dto.name(), userId)) { + throw new DuplicatedBookmarkNameException(); + } UserEntity user = userRepository.findById(userId).orElseThrow(BookmarkUserNotFoundException::new); BookmarkEntity bookmark = bookmarkRepository.findById(bookmarkId) .orElseThrow(BookmarkNotFoundException::new); @@ -82,7 +90,6 @@ public BookmarkRespDto updateBookmark(BookmarkReqDto dto, Long userId, Long book public Long deleteBookmark(Long userId, Long bookmarkId) throws BookmarkUserNotFoundException, BookmarkNotFoundException { - UserEntity user = userRepository.findById(userId).orElseThrow(BookmarkUserNotFoundException::new); BookmarkEntity bookmark = bookmarkRepository.findById(bookmarkId) .orElseThrow(BookmarkUserNotFoundException::new);