From a194695ccdc031ba9bdef788649035a130c1ba7f Mon Sep 17 00:00:00 2001 From: BlueBerrySoda Date: Sat, 4 May 2024 21:56:39 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20#146-answerCount=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EC=9D=84=20=EC=9C=84=ED=95=9C=20SQL=EB=AC=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/qna/dto/QuestionListResponse.java | 1 + .../domain/qna/dto/QuestionSliceResponse.java | 3 +-- .../repository/QuestionRepositoryImpl.java | 24 +++++++------------ 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/back/src/main/java/com/example/capstone/domain/qna/dto/QuestionListResponse.java b/back/src/main/java/com/example/capstone/domain/qna/dto/QuestionListResponse.java index 1561701749..073c97c8e2 100644 --- a/back/src/main/java/com/example/capstone/domain/qna/dto/QuestionListResponse.java +++ b/back/src/main/java/com/example/capstone/domain/qna/dto/QuestionListResponse.java @@ -12,6 +12,7 @@ public record QuestionListResponse ( String context, String tag, String country, + Long answerCount, LocalDateTime createdDate ) { } diff --git a/back/src/main/java/com/example/capstone/domain/qna/dto/QuestionSliceResponse.java b/back/src/main/java/com/example/capstone/domain/qna/dto/QuestionSliceResponse.java index 63d1d81f4d..4b39fe232c 100644 --- a/back/src/main/java/com/example/capstone/domain/qna/dto/QuestionSliceResponse.java +++ b/back/src/main/java/com/example/capstone/domain/qna/dto/QuestionSliceResponse.java @@ -6,7 +6,6 @@ public record QuestionSliceResponse( Long lastCursorId, Boolean hasNext, - List questionList, - Map answerCountList + List questionList ) { } diff --git a/back/src/main/java/com/example/capstone/domain/qna/repository/QuestionRepositoryImpl.java b/back/src/main/java/com/example/capstone/domain/qna/repository/QuestionRepositoryImpl.java index f4c84850f8..bd4bf71576 100644 --- a/back/src/main/java/com/example/capstone/domain/qna/repository/QuestionRepositoryImpl.java +++ b/back/src/main/java/com/example/capstone/domain/qna/repository/QuestionRepositoryImpl.java @@ -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 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; } @@ -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) { @@ -88,4 +82,4 @@ private BooleanExpression tagEq(String tag) { private BooleanExpression questionIdEq(Long id) { return answer.question.id.eq(id); } -} +} \ No newline at end of file