Skip to content

Commit

Permalink
[FIX] 좋아요 이미 눌렀는지 확인하는 로직 추가 #27
Browse files Browse the repository at this point in the history
  • Loading branch information
eeddiinn committed Jul 13, 2024
1 parent 8052be9 commit 63c7a22
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@

public interface NoticeLikeRepository extends JpaRepository<NoticeLike, Long> {
Optional<NoticeLike> findByNoticeAndMember(Notice notice, Member member);

boolean existsByNoticeAndMember(Notice notice, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ public void likeNotice(Long noticeId) {
Notice notice = noticeRepository.findById(noticeId)
.orElseThrow(() -> new RuntimeException("공지사항이 존재하지 않습니다."));

// 이미 좋아요를 누른 것인지 확인
boolean alreadyLiked = noticeLikeRepository.existsByNoticeAndMember(notice, member);
if (alreadyLiked) {
throw new BusinessException(ErrorMessage.ALREADY_LIKED);
}

// noticeLike를 1 증가시킵니다.
notice.setNoticeLike(notice.getNoticeLike() + 1);
noticeRepository.save(notice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum ErrorMessage {
JWT_UNAUTHORIZED_EXCEPTION(HttpStatus.UNAUTHORIZED.value(), "사용자의 로그인 검증을 실패했습니다."),
APPROVEADMIN_UNAUTHORIZED_EXCEPTION(HttpStatus.UNAUTHORIZED.value(), "승인된 관리자의 로그인 검증을 실패했습니다."),
ALREADY_SAVED(HttpStatus.CONFLICT.value(), "이미 저장한 공지입니다."),
ALREADY_LIKED(HttpStatus.CONFLICT.value(), "이미 좋아요한 공지입니다."),
;
private final int status;
private final String message;
Expand Down

0 comments on commit 63c7a22

Please sign in to comment.