Skip to content

Commit

Permalink
✏️ [FIX] 이미 즐겨찾기가 추가된결과 실행되지 않도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ddongseop committed Mar 20, 2024
1 parent a2e6baf commit bee359d
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public void createFavorite(Long memberId, FavoriteRequestDto request) {
Member member = memberRepository.findByIdOrThrow(memberId);
Book book = bookRepository.findByIdOrThrow(request.bookId());

if (member.getFavorites().stream()
.anyMatch(favorite -> favorite.getBook().getId().equals(request.bookId()))) {
return; // 이미 해당 책이 즐겨찾기에 추가된 경우, 추가 작업을 하지 않고 함수를 종료
}

Favorite favorite = Favorite.of(member, book);
favoriteRepository.save(favorite);
} // memberId와 bookId를 favorite 에 저장하는 로직
Expand All @@ -40,9 +45,9 @@ public List<FavoriteBookResponseDto> getFavorite(Long memberId) {
List<Favorite> favorites = favoriteRepository.findByMemberOrderByCreatedAtDesc(member);

return favorites.stream()
.limit(3) // 최신순 3개만 가져오기
.map(favorite -> FavoriteBookResponseDto.of(favorite.getBook()))
.collect(Collectors.toList());
.limit(3) // 최신순 3개만 가져오기
.map(favorite -> FavoriteBookResponseDto.of(favorite.getBook()))
.collect(Collectors.toList());
} // memberId를 이용해 그 멤버가 즐겨찾기 해놓은 레큐북 목록들을 반환하는 로직

@Transactional
Expand All @@ -51,7 +56,7 @@ public void deleteFavorite(Long memberId, FavoriteRequestDto request) {
Book book = bookRepository.findByIdOrThrow(request.bookId());

Favorite favorite = favoriteRepository.findByMemberAndBook(member, book)
.orElseThrow(() -> new CustomException(ErrorType.NOT_FOUND_FAVORITE_ERROR));
.orElseThrow(() -> new CustomException(ErrorType.NOT_FOUND_FAVORITE_ERROR));
favoriteRepository.delete(favorite);
}
}

0 comments on commit bee359d

Please sign in to comment.