Skip to content

Commit

Permalink
Merge pull request #147 from kookmin-sw/refactor/be/#146-answer-count
Browse files Browse the repository at this point in the history
refactor: #146-answerCount 계산을 위한 SQL문 수정
  • Loading branch information
BlueBerrySoda authored May 4, 2024
2 parents 247f657 + a194695 commit 9c8bb80
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public record QuestionListResponse (
String context,
String tag,
String country,
Long answerCount,
LocalDateTime createdDate
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
public record QuestionSliceResponse(
Long lastCursorId,
Boolean hasNext,
List<QuestionListResponse> questionList,
Map<Long, Long> answerCountList
List<QuestionListResponse> questionList
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,23 @@ public QuestionSliceResponse getQuestionListByPaging(Long cursorId, Pageable pag
QuestionListResponse.class,
question.id, question.title,
question.author, question.context,
question.tag, question.country, question.createdDate
)
question.tag, question.country, answer.id.count(), question.createdDate
)
)
.from(question)
.leftJoin(answer)
.on(question.id.eq(answer.question.id))
.where(cursorId(cursorId),
wordEq(word),
tagEq(tag))
.orderBy(question.createdDate.desc())
.limit(page.getPageSize() + 1)
.groupBy(question.id)
.distinct()
.fetch();

Map<Long, Long> answerCount = new HashMap<>();
for(QuestionListResponse response : questionList) {
Long count = jpaQueryFactory
.select(answer.count())
.from(answer)
.where(questionIdEq(response.id()))
.fetchFirst();
answerCount.put(response.id(), count);
}

boolean hasNext = false;
if(questionList.size() > page.getPageSize()) {
if(questionList.size() > page.getPageSize() && questionList.size() != 0) {
questionList.remove(page.getPageSize());
hasNext = true;
}
Expand All @@ -64,7 +58,7 @@ public QuestionSliceResponse getQuestionListByPaging(Long cursorId, Pageable pag
lastCursorId = questionList.get(questionList.size() - 1).id();
}

return new QuestionSliceResponse(lastCursorId, hasNext, questionList, answerCount);
return new QuestionSliceResponse(lastCursorId, hasNext, questionList);
}

private BooleanExpression cursorId(Long cursorId) {
Expand All @@ -88,4 +82,4 @@ private BooleanExpression tagEq(String tag) {
private BooleanExpression questionIdEq(Long id) {
return answer.question.id.eq(id);
}
}
}

0 comments on commit 9c8bb80

Please sign in to comment.