-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(회원) 게시글 검색 기능 #314
(회원) 게시글 검색 기능 #314
Changes from all commits
940fbe5
94ae487
76bec8f
58986ff
919c707
d5eb3a0
ed1e23e
dea5510
083b235
62938c3
f8924a3
58994c4
f9e32bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,4 +94,32 @@ private OrderSpecifier orderBy(final PostSortType postSortType) { | |
} | ||
} | ||
|
||
@Override | ||
public List<Post> findAllWithKeyword( | ||
final String keyword, | ||
final PostClosingType postClosingType, | ||
final PostSortType postSortType, | ||
final Long categoryId, | ||
final Pageable pageable | ||
) { | ||
return jpaQueryFactory | ||
.selectFrom(post) | ||
.join(post.writer).fetchJoin() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
위의 상황에서는 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3로 지정했던 만큼 크게 중요한 사안은 아니었습니다! |
||
.leftJoin(post.postCategories.postCategories, postCategory) | ||
.where( | ||
containsKeywordInTitleOrContent(keyword), | ||
categoryIdEq(categoryId), | ||
deadlineEq(postClosingType) | ||
) | ||
.orderBy(orderBy(postSortType)) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
} | ||
|
||
private BooleanExpression containsKeywordInTitleOrContent(final String keyword) { | ||
return post.postBody.title.contains(keyword) | ||
.or(post.postBody.content.contains(keyword)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,6 @@ | |
import com.votogether.domain.post.entity.Post; | ||
import com.votogether.domain.post.entity.PostBody; | ||
import com.votogether.domain.post.entity.PostClosingType; | ||
import com.votogether.domain.post.entity.PostOption; | ||
import com.votogether.domain.post.entity.PostSortType; | ||
import com.votogether.domain.post.service.PostService; | ||
import com.votogether.exception.GlobalExceptionHandler; | ||
|
@@ -659,7 +658,6 @@ void postClosedEarly() throws Exception { | |
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원본인이 작성한 게시글 목록을 조회한다.") | ||
void getPostsByWriter() throws JsonProcessingException { | ||
// given | ||
|
@@ -714,6 +712,53 @@ void getPostsByWriter() throws JsonProcessingException { | |
); | ||
} | ||
|
||
@Test | ||
@DisplayName("키워드를 통해 게시글 목록을 조회한다.") | ||
void searchPostsWithKeyword() throws JsonProcessingException { | ||
// given | ||
long postId = 1L; | ||
Member member = MemberFixtures.FEMALE_20.get(); | ||
|
||
TokenPayload tokenPayload = new TokenPayload(1L, 1L, 1L); | ||
given(tokenProcessor.resolveToken(anyString())).willReturn("token"); | ||
given(tokenProcessor.parseToken(anyString())).willReturn(tokenPayload); | ||
given(memberService.findById(anyLong())).willReturn(member); | ||
|
||
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.of(post, member); | ||
|
||
given(postService.searchPostsWithKeyword(anyString(), anyInt(), any(), any(), anyLong(), any(Member.class))) | ||
.willReturn(List.of(postResponse)); | ||
|
||
// when | ||
List<PostResponse> result = RestAssuredMockMvc.given().log().all() | ||
.headers(HttpHeaders.AUTHORIZATION, "Bearer token") | ||
.param("keyword", "하이") | ||
.param("page", 0) | ||
.param("postClosingType", PostClosingType.PROGRESS) | ||
.param("postSortType", PostSortType.LATEST) | ||
.param("category", 1L) | ||
.when().get("/posts/search") | ||
.then().log().all() | ||
.status(HttpStatus.OK) | ||
.extract() | ||
.as(new ParameterizedTypeReference<List<PostResponse>>() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이해하신바가 맞습니다! |
||
}.getType()); | ||
|
||
// then | ||
assertThat(result.get(0)).usingRecursiveComparison().isEqualTo(postResponse); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
@DisplayName("게시글을 삭제한다.") | ||
void delete() { | ||
// given | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -405,8 +405,10 @@ void findClosedPostsVotedByMember() { | |
Slice<Post> posts = postRepository.findClosedPostsVotedByMember(member, pageRequest); | ||
|
||
// then | ||
assertThat(posts).hasSize(2); | ||
assertThat(posts.getContent().get(0)).usingRecursiveComparison().isEqualTo(closedPost1); | ||
assertAll( | ||
() -> assertThat(posts).hasSize(2), | ||
() -> assertThat(posts.getContent().get(0)).usingRecursiveComparison().isEqualTo(closedPost1) | ||
); | ||
} | ||
|
||
@Test | ||
|
@@ -472,8 +474,10 @@ void findOpenPostsVotedByMember() { | |
Slice<Post> posts = postRepository.findOpenPostsVotedByMember(member, pageRequest); | ||
|
||
// then | ||
assertThat(posts).hasSize(2); | ||
assertThat(posts.getContent().get(0)).usingRecursiveComparison().isEqualTo(openPost1); | ||
assertAll( | ||
() -> assertThat(posts).hasSize(2), | ||
() -> assertThat(posts.getContent().get(0)).usingRecursiveComparison().isEqualTo(openPost1) | ||
); | ||
} | ||
|
||
@Test | ||
|
@@ -526,6 +530,127 @@ void findPostsVotedByMember() { | |
|
||
} | ||
|
||
@Nested | ||
@DisplayName("키워드 검색을 통해 게시글 목록을 조회한다.") | ||
class FindingPostsByKeyword { | ||
|
||
Category development; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Category love; | ||
|
||
Post devClosedPost; | ||
Post devOpenPost; | ||
Post loveClosedPost; | ||
Post loveOpenPost; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
development = categoryRepository.save(Category.builder().name("개발").build()); | ||
love = categoryRepository.save(Category.builder().name("연애").build()); | ||
|
||
devClosedPost = postTestPersister.builder() | ||
.postBody(PostBody.builder().title("자바제목").content("자바내용").build()) | ||
.deadline(LocalDateTime.of(1000, 7, 12, 0, 0)) | ||
.save(); | ||
postCategoryRepository.save(PostCategory.builder().post(devClosedPost).category(development).build()); | ||
|
||
devOpenPost = postTestPersister.builder() | ||
.postBody(PostBody.builder().title("자바제목1").content("자바내용1").build()) | ||
.deadline(LocalDateTime.of(3000, 7, 12, 0, 0)) | ||
.save(); | ||
postCategoryRepository.save(PostCategory.builder().post(devOpenPost).category(development).build()); | ||
|
||
loveClosedPost = postTestPersister.builder() | ||
.postBody(PostBody.builder().title("커플제목").content("커플내용").build()) | ||
.deadline(LocalDateTime.of(1000, 7, 12, 0, 0)) | ||
.save(); | ||
postCategoryRepository.save(PostCategory.builder().post(loveClosedPost).category(love).build()); | ||
|
||
loveOpenPost = postTestPersister.builder() | ||
.postBody(PostBody.builder().title("커플제목1").content("커플내용1").build()) | ||
.deadline(LocalDateTime.of(3000, 7, 12, 0, 0)) | ||
.save(); | ||
postCategoryRepository.save(PostCategory.builder().post(loveOpenPost).category(love).build()); | ||
|
||
} | ||
|
||
@Test | ||
@DisplayName("특정 카테고리에 속한 게시글 목록을 키워드를 통해 검색한다.") | ||
void searchPostsWithCategory() { | ||
// when | ||
List<Post> posts = postRepository.findAllWithKeyword( | ||
"자바", | ||
PostClosingType.ALL, | ||
PostSortType.LATEST, | ||
development.getId(), | ||
PageRequest.of(0, 10) | ||
); | ||
|
||
//then | ||
assertAll( | ||
() -> assertThat(posts).hasSize(2), | ||
() -> assertThat(posts.get(0)).isEqualTo(devOpenPost), | ||
() -> assertThat(posts.get(1)).isEqualTo(devClosedPost) | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("게시글 목록을 키워드를 통해 검색한다.(제목)") | ||
void searchPostsWithKeywordInTitle() { | ||
// when | ||
List<Post> posts = postRepository.findAllWithKeyword( | ||
"제목1", | ||
PostClosingType.ALL, | ||
PostSortType.LATEST, | ||
null, | ||
PageRequest.of(0, 10) | ||
); | ||
|
||
//then | ||
assertAll( | ||
() -> assertThat(posts).hasSize(2), | ||
() -> assertThat(posts.get(0)).isEqualTo(loveOpenPost), | ||
() -> assertThat(posts.get(1)).isEqualTo(devOpenPost) | ||
); | ||
} | ||
|
||
@Test | ||
@DisplayName("게시글 목록을 키워드를 통해 검색한다.(내용)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제목 or 내용으로 검색하는건데 중복되서 테스트 하는건 아닐까요,,,?? 일단 추가해보았습니다! |
||
void searchPostsWithKeywordInContent() { | ||
// when | ||
List<Post> posts = postRepository.findAllWithKeyword( | ||
"내용", | ||
PostClosingType.ALL, | ||
PostSortType.LATEST, | ||
null, | ||
PageRequest.of(0, 10) | ||
); | ||
|
||
//then | ||
assertThat(posts).hasSize(4); | ||
} | ||
|
||
@Test | ||
@DisplayName("게시글 목록을 키워드를 통해 검색한다.(제목 + 내용)") | ||
void searchPostsWithKeywordInTitleAndContent() { | ||
// when | ||
List<Post> posts = postRepository.findAllWithKeyword( | ||
"1", | ||
PostClosingType.ALL, | ||
PostSortType.LATEST, | ||
null, | ||
PageRequest.of(0, 10) | ||
); | ||
|
||
//then | ||
assertAll( | ||
() -> assertThat(posts).hasSize(2), | ||
() -> assertThat(posts.get(0)).isEqualTo(loveOpenPost), | ||
() -> assertThat(posts.get(1)).isEqualTo(devOpenPost) | ||
); | ||
} | ||
|
||
} | ||
|
||
@Nested | ||
@DisplayName("회원이 작성한 게시글 목록을 조회한다.") | ||
class findPostsByWriter { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q
쿼리 파라미터이 네이밍이 잘못된다면 400이 나가지 않나요?
만약 그렇다면 표기 되어있으면 좋을 것 같아요 :)