Skip to content

Commit

Permalink
fix: bookmark duplicated name exception 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
JuneParkCode committed Aug 23, 2024
1 parent 223f81c commit 32c2d63
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
public interface BookmarkRepository extends JpaRepository<BookmarkEntity, Long> {
List<BookmarkEntity> findByUserId(Long userId);

boolean existsByNameAndUserId(String name, Long userId);

boolean existsByIdAndUserId(Long bookmarkId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<BookmarkDetailRespDto> 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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.talkka.server.bookmark.exception;

public class DuplicatedBookmarkNameException extends RuntimeException {
private static final String MESSAGE = "이미 존재하는 북마크 이름입니다.";

public DuplicatedBookmarkNameException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +49,11 @@ public List<BookmarkRespDto> 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()
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 32c2d63

Please sign in to comment.