Skip to content
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

[feat] 질문 키워드 검색 API 추가 #92

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/docs/asciidoc/question/question-read.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ API : `GET /api/golrabas/question`
==== 성공
===== HTTP Request
include::{snippets}/question-list-read/http-request.adoc[]
===== Query Paramters
===== Query Parameters
include::{snippets}/question-list-read/query-parameters.adoc[]

==== Response
Expand All @@ -75,3 +75,21 @@ include::{snippets}/question-list-read/response-fields.adoc[]
include::{snippets}/question-list-read/response-body.adoc[]
===== HTTP Response
include::{snippets}/question-list-read/http-response.adoc[]

=== 질문 키워드 검색

API : `GET /api/golrabas/question/search`

==== 성공
===== HTTP Request
include::{snippets}/question-search/http-request.adoc[]
===== Query Parameters
include::{snippets}/question-search/query-parameters.adoc[]

==== Response
===== Response Fields
include::{snippets}/question-search/response-fields.adoc[]
===== Response Body
include::{snippets}/question-search/response-body.adoc[]
===== HTTP Response
include::{snippets}/question-search/http-response.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ public interface QuestionFinder {
* 페이지 요청에 따른 모든 질문을 조회합니다.
*
* @param noOffsetPageCommand 페이징 정보가 담긴 객체
* @return 페이징 된 글 객체
* @return 페이징된 질문 객체
*/
QuestionPaginationDto findAllBy(NoOffsetPageCommand noOffsetPageCommand);

/**
* 질문을 키워드로 검색하여 리스트로 조회합니다.
* @param keyword 검색할 키워드
* @param noOffsetPageCommand 페이징 정보가 담긴 객체
* @return 페이징된 질문 객체
*/
QuestionPaginationDto search(String keyword, NoOffsetPageCommand noOffsetPageCommand);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import donggi.dev.kkeuroolryo.core.question.domain.exception.QuestionInvalidChoiceException;
import donggi.dev.kkeuroolryo.core.question.domain.exception.QuestionNotFoundException;
import donggi.dev.kkeuroolryo.web.comment.dto.NoOffsetPageCommand;
import donggi.dev.kkeuroolryo.web.question.dto.QuestionActiveUpdateDto;
import donggi.dev.kkeuroolryo.web.question.dto.QuestionRegisterDto;
import donggi.dev.kkeuroolryo.web.question.dto.QuestionResultCommand;
import java.util.Arrays;
Expand Down Expand Up @@ -143,6 +142,21 @@ public QuestionPaginationDto findAllBy(NoOffsetPageCommand pageCommand) {
return QuestionPaginationDto.ofEntity(sliceQuestions);
}

@Override
@Transactional(readOnly = true)
public QuestionPaginationDto search(String keyword, NoOffsetPageCommand pageCommand) {
checkNoOffsetPageSize(pageCommand.getSize());

Long searchAfterId = pageCommand.getSearchAfterId() == 0
? questionRepository.getMaxId()
: pageCommand.getSearchAfterId();

Slice<Question> sliceQuestions = questionRepository.findAllByContentContainingAndSearchAfterIdAndPageable(keyword, searchAfterId,
Pageable.ofSize(Math.toIntExact(pageCommand.getSize())));

return QuestionPaginationDto.ofEntity(sliceQuestions);
}

private void checkNoOffsetPageSize(Long size) {
if (size > QUESTION_PAGE_LIMIT_SIZE) {
throw new NoOffsetPageInvalidException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public interface QuestionRepository {
*/
Slice<Question> findAllBySearchAfterIdAndPageable(Long searchAfterId, Pageable ofSize);

/**
* 저장소에서 질문을 키워드로 검색하고 페이지만큼 조회합니다.
* @param keyword 검색할 키워드
* @param searchAfterId 검색 기준 대상
* @param ofSize 페이지 크기
* @return 페이징 된 질문 객체
*/
Slice<Question> findAllByContentContainingAndSearchAfterIdAndPageable(String keyword, Long searchAfterId, Pageable ofSize);

/**
* 저장소에 id로 해당 질문이 존재하는지 여부를 반환합니다.
* @param questionId 검색할 질문 id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public interface QuestionJpaRepository extends QuestionRepository, JpaRepository
@Query(value = "select q from Question q where q.id <= :searchAfterId and q.active = true order by q.id desc")
Slice<Question> findAllBySearchAfterIdAndPageable(@Param("searchAfterId") Long searchAfterId, Pageable ofSize);

@Query(value = "select q from Question q where q.content.content like %:keyword% and q.id <= :searchAfterId and q.active = true order by q.id desc")
Slice<Question> findAllByContentContainingAndSearchAfterIdAndPageable(@Param("keyword") String keyword, @Param("searchAfterId") Long searchAfterId, Pageable ofSize);

@Override
default Question getById(Long questionId) {
return findById(questionId).orElseThrow(QuestionNotFoundException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public ApiResponse<QuestionPaginationDto> getAll(
return ApiResponse.success(questionPaginationDto);
}

@GetMapping("/question/search")
public ApiResponse<QuestionPaginationDto> search(
@RequestParam String keyword,
@RequestParam(required = false, defaultValue = "0") String searchAfterId,
@RequestParam(required = false, defaultValue = "20") String size
) {
QuestionPaginationDto questionPaginationDto = questionFinder.search(keyword, new NoOffsetPageCommand(searchAfterId, size));
return ApiResponse.success(questionPaginationDto);
}

@PatchMapping("/question/{questionId}/active/{active}")
public ApiResponse<Void> changeActive(@PathVariable("questionId") Long questionId,
@PathVariable("active") boolean active) {
Expand Down
Loading