Skip to content

Commit

Permalink
feat: (#313) 비회원으로 게시글 검색하는 api 추가 (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
aiaiaiai1 authored Aug 12, 2023
1 parent 80c95ab commit 0f01338
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,24 @@ public ResponseEntity<Void> delete(@PathVariable final Long postId) {
return ResponseEntity.noContent().build();
}

@Operation(summary = "게시글 검색(비회원)", description = "비회원으로 키워드를 통해 게시글을 검색한다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "게시글 검색 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 쿼리 파라미터값 입력"),
})
@GetMapping("/search/guest")
public ResponseEntity<List<PostResponse>> searchPostsWithKeywordForGuest(
@RequestParam final String keyword,
@RequestParam final int page,
@RequestParam final PostClosingType postClosingType,
@RequestParam final PostSortType postSortType,
@RequestParam(required = false, name = "category") final Long categoryId
) {
final List<PostResponse> responses =
postService.searchPostsWithKeywordForGuest(keyword, page, postClosingType, postSortType, categoryId);
return ResponseEntity.ok(responses);
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,21 @@ public List<PostResponse> searchPostsWithKeyword(
.toList();
}

public List<PostResponse> searchPostsWithKeywordForGuest(
final String keyword,
final int page,
final PostClosingType postClosingType,
final PostSortType postSortType,
final Long categoryId
) {
final Pageable pageable = PageRequest.of(page, BASIC_PAGING_SIZE);
final List<Post> posts =
postRepository.findAllWithKeyword(keyword, postClosingType, postSortType, categoryId, pageable);

return posts.stream()
.map(post -> PostResponse.forGuest(post))
.toList();
}

@Transactional
public void delete(final Long postId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ void getPostsByWriter() throws JsonProcessingException {
}

@Test
@DisplayName("키워드를 통해 게시글 목록을 조회한다.")
@DisplayName("(회원) 키워드를 통해 게시글 목록을 조회한다.")
void searchPostsWithKeyword() throws JsonProcessingException {
// given
long postId = 1L;
Expand Down Expand Up @@ -759,6 +759,50 @@ void searchPostsWithKeyword() throws JsonProcessingException {
assertThat(result.get(0)).usingRecursiveComparison().isEqualTo(postResponse);
}

@Test
@DisplayName("(비회원) 키워드를 통해 게시글 목록을 조회한다.")
void searchPostsWithKeywordForGuest() {
// given
PostBody postBody = PostBody.builder()
.title("title")
.content("content")
.build();

Post post = Post.builder()
.writer(MALE_30.get())
.postBody(postBody)
.deadline(LocalDateTime.now().plusDays(3L).truncatedTo(ChronoUnit.MINUTES))
.build();

PostResponse postResponse = PostResponse.forGuest(post);

given(postService.searchPostsWithKeywordForGuest(
anyString(),
anyInt(),
any(PostClosingType.class),
any(PostSortType.class),
anyLong())
).willReturn(List.of(postResponse));

// when
List<PostResponse> result = RestAssuredMockMvc.given().log().all()
.param("keyword", "하이")
.param("page", 0)
.param("postClosingType", PostClosingType.PROGRESS)
.param("postSortType", PostSortType.LATEST)
.param("category", 1L)
.when().get("/posts/search/guest")
.then().log().all()
.status(HttpStatus.OK)
.extract()
.as(new ParameterizedTypeReference<List<PostResponse>>() {
}.getType());

// then
assertThat(result.get(0)).usingRecursiveComparison().isEqualTo(postResponse);
}


@DisplayName("게시글을 삭제한다.")
void delete() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1660,4 +1660,47 @@ private boolean hasKeywordInPostResponse(PostResponse postResponse, String keywo
return postResponse.title().contains(keyword) || postResponse.content().contains(keyword);
}

@Test
@DisplayName("비회원으로 키워드를 통해 게시글 목록을 검색한다.")
void getPostsByKeywordForGuest() {
Member member = memberRepository.save(MemberFixtures.MALE_20.get());

Post closedPost = postTestPersister.builder()
.postBody(PostBody.builder().title("제목").content("키워요").build())
.deadline(LocalDateTime.now().minusDays(3L))
.save();
Post openPost1 = postTestPersister.builder()
.postBody(PostBody.builder().title("키워드").content("안녕").build())
.deadline(LocalDateTime.now().plusDays(3L))
.save();

PostOption postOption = postOptionTestPersister.builder().post(closedPost).save();
PostOption postOption1 = postOptionTestPersister.builder().post(openPost1).save();
voteTestPersister.builder().member(member).postOption(postOption).save();
voteTestPersister.builder().member(member).postOption(postOption1).save();

entityManager.flush();
entityManager.clear();

// when
List<PostResponse> responses = postService.searchPostsWithKeywordForGuest(
"키워",
0,
PostClosingType.ALL,
PostSortType.LATEST,
null
);

// then
assertAll(
() -> assertThat(responses).hasSize(2),
() -> assertThat(responses.get(0).postId()).isEqualTo(openPost1.getId()),
() -> assertThat(responses.get(1).postId()).isEqualTo(closedPost.getId()),
() -> assertThat(hasKeywordInPostResponse(responses.get(0), "키워")).isTrue(),
() -> assertThat(hasKeywordInPostResponse(responses.get(1), "키워")).isTrue(),
() -> assertThat(responses.get(0).voteInfo().totalVoteCount()).isEqualTo(-1L),
() -> assertThat(responses.get(1).voteInfo().totalVoteCount()).isEqualTo(1L)
);
}

}

0 comments on commit 0f01338

Please sign in to comment.