Skip to content

Commit cc82066

Browse files
committed
feat(community): CommunityBoardRepository에 existsById 추가 및 검증 테스트 추가
1 parent 9b15a18 commit cc82066

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/main/java/com/somemore/community/repository/board/CommunityBoardRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ public interface CommunityBoardRepository {
1212
Optional<CommunityBoard> findById(Long id);
1313
List<CommunityBoardView> getCommunityBoards();
1414
List<CommunityBoardView> findByWriterId(UUID writerId);
15+
boolean existsById(Long id);
16+
default boolean doesNotExistById(Long id) {
17+
return !existsById(id);
18+
}
1519
void deleteAllInBatch();
1620
}

src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ public List<CommunityBoardView> findByWriterId(UUID writerId) {
5252
.fetch();
5353
}
5454

55+
@Override
56+
public boolean existsById(Long id) {
57+
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
58+
59+
return queryFactory
60+
.selectOne()
61+
.from(communityBoard)
62+
.where(communityBoard.id.eq(id)
63+
.and(communityBoard.deleted.eq(false)))
64+
.fetchFirst() != null;
65+
}
66+
5567
private JPAQuery<CommunityBoardView> getCommunityBoardsQuery() {
5668
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
5769
QVolunteer volunteer = QVolunteer.volunteer;

src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,27 @@ void getCommunityBoardsByWriterId() {
158158
assertThat(communityBoards.getLast().writerNickname()).isEqualTo(volunteer.getNickname());
159159
assertThat(communityBoards.getLast().communityBoard().getCreatedAt()).isEqualTo(communityBoard1.getCreatedAt());
160160
}
161+
162+
@DisplayName("게시글 id로 게시글이 존재하는지 확인할 수 있다.")
163+
@Test
164+
void existsById() {
165+
166+
//given
167+
UUID writerId = UUID.randomUUID();
168+
169+
CommunityBoard communityBoard = CommunityBoard.builder()
170+
.title("테스트 커뮤니티 게시글 제목")
171+
.content("테스트 커뮤니티 게시글 내용")
172+
.imgUrl("http://community.example.com/123")
173+
.writerId(writerId)
174+
.build();
175+
176+
CommunityBoard savedComment = communityBoardRepository.save(communityBoard);
177+
178+
//when
179+
boolean isExist = communityBoardRepository.existsById(savedComment.getId());
180+
181+
//then
182+
assertThat(isExist).isTrue();
183+
}
161184
}

0 commit comments

Comments
 (0)