Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
✅ 작업 내용
QuestionRepositoryImpl 클래스 수정
• QuestionRepositoryImpl 클래스는 QueryDSL을 사용하여 데이터베이스 쿼리를 실행합니다.
• 주요 기능은 searchQuestionsOfCursorPagingByContent 메서드를 통해 페이징된 질문 목록을 검색하는 것입니다.
searchQuestionsOfCursorPagingByContent 메서드 구현
• 이 메서드는 검색어와 페이지네이션 커서를 사용하여 질문 목록을 검색합니다.
• 커서 기반 페이지네이션을 구현하기 위해 cursorViewCount와 questionId를 사용합니다.
• 이 값들이 null이거나 0일 경우 최대 값(Integer.MAX_VALUE 및 Long.MAX_VALUE)을 사용하여 처음 페이지를 가져오도록 설정합니다.
• BooleanExpression을 사용하여 페이징 커서 조건을 만듭니다.
• 쿼리를 실행하여 질문 목록을 가져오고, hasNext 플래그를 설정하여 다음 페이지가 있는지 확인합니다.
• 마지막 질문의 viewCount와 id를 사용하여 다음 커서를 설정합니다.
SliceQuestionResponse 클래스 생성
• SliceQuestionResponse 클래스는 커서 기반 페이지네이션을 지원하기 위해 확장된 슬라이스 응답 클래스입니다.
• 기본 SliceResponse 클래스를 확장하여 nextCursorViewCount와 nextQuestionId 필드를 추가합니다.
쿼리 메서드 분리 및 리팩토링
• createBooleanTemplate 메서드: 검색어에 대한 Boolean 템플릿을 생성합니다.
• createCursorPredicate 메서드: 커서 조건을 생성합니다.
• fetchQuestions 메서드: 쿼리를 실행하여 질문 목록을 가져옵니다.
• mapToResponse 메서드: 질문 엔티티를 DTO로 변환합니다.
🤔 고민했던 점과 해결 방법
• 커서 기반 페이지네이션을 구현하기 위해 cursorViewCount와 questionId를 사용해야 했습니다.
• 처음 페이지를 가져올 때는 이 값들이 null 또는 0일 수 있으므로, 이를 최대 값으로 설정하여 처음 페이지를 가져오도록 했습니다.
2. 커서 조건 생성:
• 커서 조건을 생성할 때 viewCount와 questionId를 조합하여 정확한 페이지네이션을 구현해야 했습니다.
• 동일한 viewCount를 가진 질문들 사이에서 중복되지 않도록 id를 추가 조건으로 사용했습니다.
3. 응답 포맷 확장:
• 기본 슬라이스 응답을 확장하여 커서 값을 포함하도록 했습니다.
• 이를 통해 클라이언트가 다음 페이지를 요청할 때 필요한 커서 값을 쉽게 전달할 수 있게 했습니다.
4. 쿼리 최적화:
• fetchQuestions 메서드에서 필요한 데이터만 가져오도록 쿼리를 최적화했습니다.
• pageSize + 1을 가져와서 hasNext 플래그를 설정하고, 필요시 마지막 요소를 제거했습니다.
🔊 도움이 필요한 부분