Skip to content

Commit

Permalink
[feat] 카테고리로 질문 리스트 조회 API 추가 / RestDocs 문서 추가 (#93)
Browse files Browse the repository at this point in the history
* feat: 카테고리로 질문 리스트 조회 API 추가 / RestDocs 문서 추가

* docs: API 문서 최신화
  • Loading branch information
donggi-lee-bit authored Oct 8, 2023
1 parent 7bd8f94 commit e66c55e
Show file tree
Hide file tree
Showing 8 changed files with 536 additions and 174 deletions.
20 changes: 19 additions & 1 deletion src/docs/asciidoc/question/question-read.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
=== 카테고리 질문 조회
=== 카테고리로 질문 id 리스트 조회

API : `GET /api/golrabas/category/{category}`

Expand Down Expand Up @@ -28,6 +28,24 @@ include::{snippets}/questions-read-invalid-category/response-body.adoc[]
===== HTTP Response
include::{snippets}/questions-read-invalid-category/http-response.adoc[]

=== 카테고리로 질문 리스트 조회

API : `GET /api/golrabas/category/{category}/question`

==== 성공
===== Path Parameters
include::{snippets}/question-list-read-by-category/path-parameters.adoc[]
===== HTTP Request
include::{snippets}/question-list-read-by-category/http-request.adoc[]

==== Response
===== Response Fields
include::{snippets}/question-list-read-by-category/response-fields.adoc[]
===== Response Body
include::{snippets}/question-list-read-by-category/response-body.adoc[]
===== HTTP Response
include::{snippets}/question-list-read-by-category/http-response.adoc[]

=== 특정 질문 조회

API : `GET /api/golrabas/{questionId}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface QuestionFinder {
* @param noOffsetPageCommand 페이징 정보가 담긴 객체
* @return 페이징된 질문 객체
*/
QuestionPaginationDto findAllBy(NoOffsetPageCommand noOffsetPageCommand);
QuestionPaginationDto findAllIdsByCategory(NoOffsetPageCommand noOffsetPageCommand);

/**
* 질문을 키워드로 검색하여 리스트로 조회합니다.
Expand All @@ -39,4 +39,12 @@ public interface QuestionFinder {
* @return 페이징된 질문 객체
*/
QuestionPaginationDto search(String keyword, NoOffsetPageCommand noOffsetPageCommand);

/**
* 어드민에서 카테고리로 질문 리스트를 조회합니다.
* @param category 조회할 카테고리
* @param pageCommand 페이징 정보가 담긴 객체
* @return 페이징된 질문 객체
*/
QuestionPaginationDto findAllByCategory(Category category, NoOffsetPageCommand pageCommand);
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public QuestionDto getQuestion(Long questionId) {

@Override
@Transactional(readOnly = true)
public QuestionPaginationDto findAllBy(NoOffsetPageCommand pageCommand) {
public QuestionPaginationDto findAllIdsByCategory(NoOffsetPageCommand pageCommand) {
checkNoOffsetPageSize(pageCommand.getSize());

Long searchAfterId = pageCommand.getSearchAfterId() == 0
Expand Down Expand Up @@ -157,6 +157,21 @@ public QuestionPaginationDto search(String keyword, NoOffsetPageCommand pageComm
return QuestionPaginationDto.ofEntity(sliceQuestions);
}

@Override
@Transactional(readOnly = true)
public QuestionPaginationDto findAllByCategory(Category category, NoOffsetPageCommand pageCommand) {
checkNoOffsetPageSize(pageCommand.getSize());

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

Slice<Question> sliceQuestions = questionRepository.findAllByCategoryAndSearchAfterIdAndPageable(category, 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);

/**
* 카테고리로 저장소에서 질문 리스트를 검색 id 기준 이후의 페이지만큼 조회합니다.
* @param category 조회할 카테고리
* @param searchAfterId 검색 기준 대상
* @param ofSize 페이지 크기
* @return 페이징 된 질문 객체
*/
Slice<Question> findAllByCategoryAndSearchAfterIdAndPageable(Category category, Long searchAfterId, Pageable ofSize);

/**
* 저장소에서 질문을 키워드로 검색하고 페이지만큼 조회합니다.
* @param keyword 검색할 키워드
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public interface QuestionJpaRepository extends QuestionRepository, JpaRepository
@Query(value = "select q from Question q where q.content.content like %:keyword% and q.id <= :searchAfterId order by q.id desc")
Slice<Question> findAllByContentContainingAndSearchAfterIdAndPageable(@Param("keyword") String keyword, @Param("searchAfterId") Long searchAfterId, Pageable ofSize);

@Query(value = "select q from Question q where q.category = :category and q.id <= :searchAfterId order by q.id desc")
Slice<Question> findAllByCategoryAndSearchAfterIdAndPageable(@Param("category") Category category, @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 @@ -49,15 +49,28 @@ public ApiResponse<Void> register(@RequestBody QuestionResultCommand resultComma
}

/**
* 카테고리 이름으로 해당 카테고리의 질문을 조회합니다.
* 카테고리 이름으로 해당 카테고리의 질문 id를 조회합니다.
* 질문은 랜덤한 순서로 조회합니다.
*/
@GetMapping("/category/{category}")
public ApiResponse<RandomQuestionsDto> getQuestionsByCategory(@PathVariable("category") Category category) {
public ApiResponse<RandomQuestionsDto> getQuestionIdsByCategory(@PathVariable("category") Category category) {
RandomQuestionsDto randomQuestionsDto = questionFinder.getRandomQuestionsByCategory(category);
return ApiResponse.success(randomQuestionsDto);
}

/**
* 어드민에서 카테고리로 질문 리스트를 조회합니다.
*/
@GetMapping("/category/{category}/question")
public ApiResponse<QuestionPaginationDto> getQuestionsByCategory(
@PathVariable("category") Category category,
@RequestParam(required = false, defaultValue = "0") String searchAfterId,
@RequestParam(required = false, defaultValue = "20") String size
) {
QuestionPaginationDto questionPaginationDto = questionFinder.findAllByCategory(category, new NoOffsetPageCommand(searchAfterId, size));
return ApiResponse.success(questionPaginationDto);
}

@GetMapping("/question/{questionId}")
public ApiResponse<QuestionDto> getQuestion(@PathVariable("questionId") Long questionId) {
QuestionDto questionDto = questionFinder.getQuestion(questionId);
Expand All @@ -69,7 +82,7 @@ public ApiResponse<QuestionPaginationDto> getAll(
@RequestParam(required = false, defaultValue = "0") String searchAfterId,
@RequestParam(required = false, defaultValue = "20") String size
) {
QuestionPaginationDto questionPaginationDto = questionFinder.findAllBy(new NoOffsetPageCommand(searchAfterId, size));
QuestionPaginationDto questionPaginationDto = questionFinder.findAllIdsByCategory(new NoOffsetPageCommand(searchAfterId, size));
return ApiResponse.success(questionPaginationDto);
}

Expand Down
Loading

0 comments on commit e66c55e

Please sign in to comment.