Skip to content

Commit 7c4d9f1

Browse files
authored
refactor: community-board findById 기능 QueryDSL 변경 (#54)
* test(community): CommunityBoard id로 조회 테스트 수정 및 예외 추가 * test(community): CommunityBoard id로 조회 Service 및 Repository 수정 * test(community): CommunityBoard id로 조회 테스트 수정 및 예외 추가 * refactor(community): repository getCommunityBoardWithId -> findById, getCommunityBoardsByWriterId -> findByWriterId 이름 변경
1 parent 405cba9 commit 7c4d9f1

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public interface CommunityBoardRepository {
1010
CommunityBoard save(CommunityBoard communityBoard);
1111
Optional<CommunityBoard> findById(Long id);
1212
List<CommunityBoard> getCommunityBoards();
13-
List<CommunityBoard> getCommunityBoardsByWriterId(UUID writerId);
13+
List<CommunityBoard> findByWriterId(UUID writerId);
1414
void deleteAllInBatch();
1515
}

src/main/java/com/somemore/community/repository/CommunityRepositoryImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ public CommunityBoard save(CommunityBoard communityBoard) {
2424

2525
@Override
2626
public Optional<CommunityBoard> findById(Long id) {
27-
return communityBoardJpaRepository.findById(id);
27+
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
28+
29+
return Optional.ofNullable(queryFactory
30+
.selectFrom(communityBoard)
31+
.where(communityBoard.id.eq(id)
32+
.and(communityBoard.deleted.eq(false)))
33+
.fetchOne());
2834
}
2935

3036
@Override
@@ -39,7 +45,7 @@ public List<CommunityBoard> getCommunityBoards() {
3945
}
4046

4147
@Override
42-
public List<CommunityBoard> getCommunityBoardsByWriterId(UUID writerId) {
48+
public List<CommunityBoard> findByWriterId(UUID writerId) {
4349
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
4450

4551
return queryFactory

src/main/java/com/somemore/community/service/query/CommunityBoardQueryService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public List<CommunityBoardGetResponseDto> getCommunityBoards() {
4343

4444
@Override
4545
public List<CommunityBoardGetResponseDto> getCommunityBoardsByWriterId(UUID writerId) {
46-
List<CommunityBoard> boards = communityBoardRepository.getCommunityBoardsByWriterId(writerId);
46+
List<CommunityBoard> boards = communityBoardRepository.findByWriterId(writerId);
4747
String writerNickname = getWriterNickname(writerId);
4848

4949
return boards.stream()

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class CommunityRepositoryTest extends IntegrationTestSupport {
1919
@Autowired
2020
private CommunityBoardRepository communityBoardRepository;
2121

22-
@DisplayName("커뮤니티 id로 커뮤니티 상세 정보를 조회할 수 있다. (Repository)")
22+
@DisplayName("커뮤니티 게시글 id로 커뮤니티 상세 정보를 조회할 수 있다. (Repository)")
2323
@Test
24-
void findById() {
24+
void getCommunityBoardById() {
2525
//given
2626
CommunityBoard communityBoard = CommunityBoard.builder()
2727
.title("테스트 커뮤니티 게시글 제목")
@@ -43,6 +43,27 @@ void findById() {
4343
assertThat(foundCommunityBoard.get().getWriterId()).isEqualTo(communityBoard.getWriterId());
4444
}
4545

46+
@DisplayName("삭제된 커뮤니티 id로 커뮤니티 상세 정보를 조회할 때 예외를 반환할 수 있다. (Repository)")
47+
@Test
48+
void getCommunityBoardByDeletedId() {
49+
//given
50+
CommunityBoard communityBoard = CommunityBoard.builder()
51+
.title("테스트 커뮤니티 게시글 제목")
52+
.content("테스트 커뮤니티 게시글 내용")
53+
.imgUrl("http://community.example.com/123")
54+
.writerId(UUID.randomUUID())
55+
.build();
56+
57+
communityBoardRepository.save(communityBoard);
58+
communityBoardRepository.deleteAllInBatch();
59+
60+
//when
61+
Optional<CommunityBoard> foundCommunityBoard = communityBoardRepository.findById(communityBoard.getId());
62+
63+
//then
64+
assertThat(foundCommunityBoard).isEmpty();
65+
}
66+
4667
@DisplayName("저장된 전체 커뮤니티 게시글을 목록으로 조회할 수 있다. (Repository)")
4768
@Test
4869
void getCommunityBoards() {
@@ -103,7 +124,7 @@ void getCommunityBoardsByWriterId() {
103124
communityBoardRepository.save(communityBoard3);
104125

105126
//when
106-
List<CommunityBoard> communityBoards = communityBoardRepository.getCommunityBoardsByWriterId(communityBoard1.getWriterId());
127+
List<CommunityBoard> communityBoards = communityBoardRepository.findByWriterId(communityBoard1.getWriterId());
107128

108129
//then
109130
assertThat(communityBoards).hasSize(2);

0 commit comments

Comments
 (0)