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);