Skip to content

Commit 955ee86

Browse files
authored
feat: 커뮤니티 게시글 검색 조회 (RDB) (#248)
* refactor(community): 전체 조회, 검색 조회 메서드 통합 * refactor(community): 전체 조회 수정 및 검색 조회 엔드포인트 추가 * test(community): 전체 조회 테스트 수정 및 검색 조회 테스트 추가 * chore(community): 불필요한 import 제거 * chore(community): 개행 수정
1 parent 90f40ca commit 955ee86

File tree

9 files changed

+99
-13
lines changed

9 files changed

+99
-13
lines changed

src/main/java/com/somemore/community/controller/CommunityBoardQueryApiController.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public ApiResponse<Page<CommunityBoardResponseDto>> getAll(
3030
) {
3131
return ApiResponse.ok(
3232
200,
33-
communityBoardQueryUseCase.getCommunityBoards(pageable.getPageNumber()),
33+
communityBoardQueryUseCase.getCommunityBoards(null, pageable.getPageNumber()),
3434
"전체 커뮤니티 게시글 리스트 조회 성공"
3535
);
3636
}
@@ -61,6 +61,19 @@ public ApiResponse<Page<CommunityBoardResponseDto>> getByWriterId(
6161
// );
6262
// }
6363

64+
@GetMapping("/community-boards/search")
65+
@Operation(summary = "커뮤니티 게시글 키워드 검색", description = "키워드를 포함한 커뮤니티 게시글 목록을 조회합니다.")
66+
public ApiResponse<Page<CommunityBoardResponseDto>> getCommunityBoardsBySearch(
67+
@RequestParam String keyword,
68+
Pageable pageable
69+
) {
70+
return ApiResponse.ok(
71+
200,
72+
communityBoardQueryUseCase.getCommunityBoards(keyword, pageable.getPageNumber()),
73+
"커뮤니티 게시글 검색 리스트 조회 성공"
74+
);
75+
}
76+
6477
@GetMapping("/community-board/{id}")
6578
@Operation(summary = "커뮤니티 게시글 상세 조회", description = "커뮤니티 게시글의 상세 정보를 조회합니다.")
6679
public ApiResponse<CommunityBoardDetailResponseDto> getById(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public interface CommunityBoardRepository {
1313
CommunityBoard save(CommunityBoard communityBoard);
1414
Optional<CommunityBoard> findById(Long id);
15-
Page<CommunityBoardView> findCommunityBoards(Pageable pageable);
15+
Page<CommunityBoardView> findCommunityBoards(String keyword, Pageable pageable);
1616
Page<CommunityBoardView> findByWriterId(UUID writerId, Pageable pageable);
1717
boolean existsById(Long id);
1818
default boolean doesNotExistById(Long id) {

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.somemore.community.domain.QCommunityBoard;
1010
import com.somemore.volunteer.domain.QVolunteer;
1111
import lombok.RequiredArgsConstructor;
12+
import org.apache.commons.lang3.StringUtils;
1213
import org.springframework.data.domain.Page;
1314
import org.springframework.data.domain.Pageable;
1415
import org.springframework.data.support.PageableExecutionUtils;
@@ -44,9 +45,10 @@ public Optional<CommunityBoard> findById(Long id) {
4445
}
4546

4647
@Override
47-
public Page<CommunityBoardView> findCommunityBoards(Pageable pageable) {
48+
public Page<CommunityBoardView> findCommunityBoards(String keyword, Pageable pageable) {
4849
List<CommunityBoardView> content = getCommunityBoardsQuery()
49-
.where(isNotDeleted())
50+
.where(isNotDeleted()
51+
.and(keywordEq(keyword)))
5052
.offset(pageable.getOffset())
5153
.limit(pageable.getPageSize())
5254
.fetch();
@@ -55,7 +57,8 @@ public Page<CommunityBoardView> findCommunityBoards(Pageable pageable) {
5557
.select(communityBoard.count())
5658
.from(communityBoard)
5759
.join(volunteer).on(communityBoard.writerId.eq(volunteer.id))
58-
.where(isNotDeleted());
60+
.where(isNotDeleted()
61+
.and(keywordEq(keyword)));
5962

6063
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
6164
}
@@ -160,6 +163,12 @@ private BooleanExpression isNotDeleted() {
160163

161164
private BooleanExpression isWriter(UUID writerId) {return communityBoard.writerId.eq(writerId); }
162165

166+
private BooleanExpression keywordEq(String keyword) {
167+
return StringUtils.isNotBlank(keyword)
168+
? communityBoard.title.containsIgnoreCase(keyword)
169+
: null;
170+
}
171+
163172
// private List<CommunityBoardDocument> getBoardDocuments(String keyword) {
164173
//
165174
// if (keyword == null || keyword.isEmpty()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public class CommunityBoardQueryService implements CommunityBoardQueryUseCase {
2828
private static final int PAGE_SIZE = 10;
2929

3030
@Override
31-
public Page<CommunityBoardResponseDto> getCommunityBoards(int page) {
31+
public Page<CommunityBoardResponseDto> getCommunityBoards(String keyword, int page) {
3232
Pageable pageable = PageRequest.of(page, PAGE_SIZE);
33-
Page<CommunityBoardView> boards = communityBoardRepository.findCommunityBoards(pageable);
33+
Page<CommunityBoardView> boards = communityBoardRepository.findCommunityBoards(keyword, pageable);
3434
return boards.map(CommunityBoardResponseDto::from);
3535
}
3636

src/main/java/com/somemore/community/usecase/board/CommunityBoardQueryUseCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.UUID;
1010

1111
public interface CommunityBoardQueryUseCase {
12-
Page<CommunityBoardResponseDto> getCommunityBoards(int page);
12+
Page<CommunityBoardResponseDto> getCommunityBoards(String keyword, int page);
1313
Page<CommunityBoardResponseDto> getCommunityBoardsByWriterId(UUID writerId, int page);
1414
CommunityBoardDetailResponseDto getCommunityBoardDetail(Long id);
1515
List<CommunityBoard> getAllCommunityBoards();

src/test/java/com/somemore/community/controller/CommunityBoardQueryApiControllerTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void getAll() throws Exception {
4242
//given
4343
Page<CommunityBoardResponseDto> page = new PageImpl<>(Collections.emptyList());
4444

45-
given(communityBoardQueryUseCase.getCommunityBoards(anyInt()))
45+
given(communityBoardQueryUseCase.getCommunityBoards(any(), anyInt()))
4646
.willReturn(page);
4747

4848
//when
@@ -56,7 +56,7 @@ void getAll() throws Exception {
5656
.value("전체 커뮤니티 게시글 리스트 조회 성공"));
5757

5858
verify(communityBoardQueryUseCase, times(1))
59-
.getCommunityBoards(anyInt());
59+
.getCommunityBoards(any(), anyInt());
6060
}
6161

6262
@Test
@@ -106,6 +106,29 @@ void getById() throws Exception {
106106
.getCommunityBoardDetail(any());
107107
}
108108

109+
@Test
110+
@DisplayName("커뮤니티 게시글 검색 조회 성공")
111+
void getBySearch() throws Exception {
112+
//given
113+
Page<CommunityBoardResponseDto> page = new PageImpl<>(Collections.emptyList());
114+
115+
given(communityBoardQueryUseCase.getCommunityBoards(any(), anyInt()))
116+
.willReturn(page);
117+
//when
118+
//then
119+
mockMvc.perform(get("/api/community-boards/search")
120+
.param("keyword", "봉사")
121+
.accept(MediaType.APPLICATION_JSON))
122+
.andExpect(status().isOk())
123+
.andExpect(jsonPath("$.code").value(200))
124+
.andExpect(jsonPath("$.data").exists())
125+
.andExpect(jsonPath("$.message")
126+
.value("커뮤니티 게시글 검색 리스트 조회 성공"));
127+
128+
verify(communityBoardQueryUseCase, times(1))
129+
.getCommunityBoards(any(), anyInt());
130+
}
131+
109132
// @Test
110133
// @DisplayName("커뮤니티 게시글 검색 조회 성공")
111134
// void getBySearch() throws Exception {

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void getCommunityBoards() {
9999
Pageable pageable = getPageable();
100100

101101
//when
102-
Page<CommunityBoardView> communityBoards = communityBoardRepository.findCommunityBoards(pageable);
102+
Page<CommunityBoardView> communityBoards = communityBoardRepository.findCommunityBoards(null, pageable);
103103

104104
//then
105105
assertThat(communityBoards).isNotNull();
@@ -138,6 +138,27 @@ void existsById() {
138138
assertThat(isExist).isTrue();
139139
}
140140

141+
@DisplayName("검색 키워드가 포함된 커뮤니티 게시글을 페이지로 조회할 수 있다. (Repository)")
142+
@Test
143+
void getCommunityBoardsBySearch() {
144+
145+
//given
146+
Pageable pageable = getPageable();
147+
148+
String title = "봉사";
149+
CommunityBoard communityBoard = createCommunityBoard(title, writerId);
150+
communityBoardRepository.save(communityBoard);
151+
152+
//when
153+
Page<CommunityBoardView> communityBoards = communityBoardRepository.findCommunityBoards("봉사", pageable);
154+
155+
//then
156+
assertThat(communityBoards).isNotNull();
157+
assertThat(communityBoards.getTotalElements()).isEqualTo(1);
158+
assertThat(communityBoards.getSize()).isEqualTo(10);
159+
assertThat(communityBoards.getNumber()).isZero();
160+
}
161+
141162
// @DisplayName("게시글을 elastic search index에 저장할 수 있다. (repository)")
142163
// @Test
143164
// void saveDocuments() {

src/test/java/com/somemore/community/service/board/CommunityBoardQueryServiceTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void getAllCommunityBoards() {
9292

9393
//given
9494
//when
95-
Page<CommunityBoardResponseDto> dtos = communityBoardQueryService.getCommunityBoards(2);
95+
Page<CommunityBoardResponseDto> dtos = communityBoardQueryService.getCommunityBoards(null, 2);
9696

9797
//then
9898
assertThat(dtos).isNotNull();
@@ -149,6 +149,26 @@ void getCommunityBoardDetailWithDeletedId() {
149149
.withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD.getMessage());
150150
}
151151

152+
@DisplayName("검색 키워드가 포함된 커뮤니티 게시글 리스트를 페이지로 조회한다.")
153+
@Test
154+
void getCommunityBoardsBySearch() {
155+
156+
//given
157+
String title = "봉사";
158+
CommunityBoard communityBoard = createCommunityBoard(title, writerId1);
159+
communityBoardRepository.save(communityBoard);
160+
161+
//when
162+
Page<CommunityBoardResponseDto> dtos = communityBoardQueryService.getCommunityBoards("봉사", 0);
163+
164+
//then
165+
assertThat(dtos).isNotNull();
166+
assertThat(dtos.getContent()).isNotNull();
167+
assertThat(dtos.getTotalElements()).isEqualTo(1);
168+
assertThat(dtos.getSize()).isEqualTo(10);
169+
assertThat(dtos.getTotalPages()).isEqualTo(1);
170+
}
171+
152172
// @DisplayName("게시글을 elastic search index에 저장한다. (service)")
153173
// @Test
154174
// void saveCommunityBoardDocuments() {

src/test/java/com/somemore/community/service/board/DeleteCommunityBoardServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void deleteCommunityBoardWithId() {
5959
deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId);
6060

6161
//then
62-
assertThat(communityBoardQueryUseCase.getCommunityBoards(0)).isEmpty();
62+
assertThat(communityBoardQueryUseCase.getCommunityBoards(null, 0)).isEmpty();
6363
}
6464

6565
@DisplayName("삭제된 커뮤니티 게시글의 id로 게시글을 삭제할 때 예외를 던진다.")

0 commit comments

Comments
 (0)