From e9cc339e3c2f52a437cf3d78e00d0db674d33e5d Mon Sep 17 00:00:00 2001 From: nuyh Date: Wed, 22 Nov 2023 17:30:51 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=A7=84=EB=8F=84=EC=9C=A8=20sette?= =?UTF-8?q?r=20=EC=A0=9C=EA=B1=B0=20=ED=9B=84=20dto=20=EB=82=B4=EB=B6=80?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EB=A7=A4=ED=95=91=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #1609 --- .../roadmap/application/KeywordService.java | 9 +++--- .../roadmap/application/RoadMapService.java | 5 +-- .../application/dto/KeywordResponse.java | 32 +++++++++---------- .../application/dto/KeywordsResponse.java | 10 +++--- .../application/RoadMapServiceTest.java | 2 +- 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/backend/src/main/java/wooteco/prolog/roadmap/application/KeywordService.java b/backend/src/main/java/wooteco/prolog/roadmap/application/KeywordService.java index c5d687c4f..b76335c4c 100644 --- a/backend/src/main/java/wooteco/prolog/roadmap/application/KeywordService.java +++ b/backend/src/main/java/wooteco/prolog/roadmap/application/KeywordService.java @@ -13,6 +13,7 @@ import java.util.List; +import static java.util.Collections.emptyMap; import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION; import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_SESSION_NOT_FOUND_EXCEPTION; @@ -66,7 +67,7 @@ public KeywordResponse findKeywordWithAllChild(final Long sessionId, final Long Keyword keyword = keywordRepository.findFetchByIdOrderBySeq(keywordId); - return KeywordResponse.createWithAllChildResponse(keyword); + return KeywordResponse.createWithAllChildResponse(keyword, emptyMap(), emptyMap()); } @Transactional(readOnly = true) @@ -75,7 +76,7 @@ public KeywordResponse newFindKeywordWithAllChild(final Long keywordId) { Keyword keyword = keywordRepository.findFetchByIdOrderBySeq(keywordId); - return KeywordResponse.createWithAllChildResponse(keyword); + return KeywordResponse.createWithAllChildResponse(keyword, emptyMap(), emptyMap()); } @Transactional(readOnly = true) @@ -84,14 +85,14 @@ public KeywordsResponse findSessionIncludeRootKeywords(final Long sessionId) { List keywords = keywordRepository.findBySessionIdAndParentIsNull(sessionId); - return KeywordsResponse.from(keywords); + return KeywordsResponse.of(keywords, emptyMap(), emptyMap()); } @Transactional(readOnly = true) public KeywordsResponse newFindSessionIncludeRootKeywords() { List keywords = keywordRepository.newFindByParentIsNull(); - return KeywordsResponse.from(keywords); + return KeywordsResponse.of(keywords, emptyMap(), emptyMap()); } public void updateKeyword( diff --git a/backend/src/main/java/wooteco/prolog/roadmap/application/RoadMapService.java b/backend/src/main/java/wooteco/prolog/roadmap/application/RoadMapService.java index d5514c3f6..bff726073 100644 --- a/backend/src/main/java/wooteco/prolog/roadmap/application/RoadMapService.java +++ b/backend/src/main/java/wooteco/prolog/roadmap/application/RoadMapService.java @@ -26,10 +26,7 @@ public KeywordsResponse findAllKeywordsWithProgress(final Long curriculumId, fin final Map totalQuizCounts = getTotalQuizCounts(); final Map answeredQuizCounts = getAnsweredQuizCounts(memberId); - final KeywordsResponse keywordsResponse = KeywordsResponse.from(keywords); - keywordsResponse.setProgress(totalQuizCounts, answeredQuizCounts); - - return keywordsResponse; + return KeywordsResponse.of(keywords, totalQuizCounts, answeredQuizCounts); } private Map getTotalQuizCounts() { diff --git a/backend/src/main/java/wooteco/prolog/roadmap/application/dto/KeywordResponse.java b/backend/src/main/java/wooteco/prolog/roadmap/application/dto/KeywordResponse.java index 0255414ec..683bf3e49 100644 --- a/backend/src/main/java/wooteco/prolog/roadmap/application/dto/KeywordResponse.java +++ b/backend/src/main/java/wooteco/prolog/roadmap/application/dto/KeywordResponse.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import wooteco.prolog.roadmap.domain.Keyword; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; @@ -20,14 +21,14 @@ public class KeywordResponse { private int order; private int importance; private int totalQuizCount; - private int doneQuizCount; + private int answeredQuizCount; private Long parentKeywordId; private List recommendedPosts; private List childrenKeywords; public KeywordResponse(final Long keywordId, final String name, final String description, final int order, final int importance, final int totalQuizCount, - final int doneQuizCount, final Long parentKeywordId, + final int answeredQuizCount, final Long parentKeywordId, final List recommendedPosts, final List childrenKeywords) { this.keywordId = keywordId; @@ -36,7 +37,7 @@ public KeywordResponse(final Long keywordId, final String name, final String des this.order = order; this.importance = importance; this.totalQuizCount = totalQuizCount; - this.doneQuizCount = doneQuizCount; + this.answeredQuizCount = answeredQuizCount; this.parentKeywordId = parentKeywordId; this.recommendedPosts = recommendedPosts; this.childrenKeywords = childrenKeywords; @@ -52,21 +53,23 @@ public static KeywordResponse createResponse(final Keyword keyword) { 0, 0, keyword.getParentIdOrNull(), createRecommendedPostResponses(keyword), - null); + Collections.emptyList()); } - public static KeywordResponse createWithAllChildResponse(final Keyword keyword) { + public static KeywordResponse createWithAllChildResponse(final Keyword keyword, + final Map totalQuizCounts, + final Map answeredQuizCounts) { return new KeywordResponse( keyword.getId(), keyword.getName(), keyword.getDescription(), keyword.getSeq(), keyword.getImportance(), - 0, - 0, + totalQuizCounts.getOrDefault(keyword.getId(), 0), + answeredQuizCounts.getOrDefault(keyword.getId(), 0), keyword.getParentIdOrNull(), createRecommendedPostResponses(keyword), - createChildren(keyword.getChildren())); + createChildren(keyword.getChildren(), totalQuizCounts, answeredQuizCounts)); } private static List createRecommendedPostResponses(final Keyword keyword) { @@ -75,16 +78,11 @@ private static List createRecommendedPostResponses(fina .collect(Collectors.toList()); } - private static List createChildren(final Set children) { + private static List createChildren(final Set children, + final Map totalQuizCounts, + final Map answeredQuizCounts) { return children.stream() - .map(KeywordResponse::createWithAllChildResponse) + .map(keyword -> createWithAllChildResponse(keyword, totalQuizCounts, answeredQuizCounts)) .collect(Collectors.toList()); } - - public void setProgress(final Map totalQuizCounts, final Map answeredQuizCounts) { - totalQuizCount = totalQuizCounts.getOrDefault(keywordId, 0); - doneQuizCount = answeredQuizCounts.getOrDefault(keywordId, 0); - - childrenKeywords.forEach(child -> child.setProgress(totalQuizCounts, answeredQuizCounts)); - } } diff --git a/backend/src/main/java/wooteco/prolog/roadmap/application/dto/KeywordsResponse.java b/backend/src/main/java/wooteco/prolog/roadmap/application/dto/KeywordsResponse.java index f4f602c00..c5927f0a1 100644 --- a/backend/src/main/java/wooteco/prolog/roadmap/application/dto/KeywordsResponse.java +++ b/backend/src/main/java/wooteco/prolog/roadmap/application/dto/KeywordsResponse.java @@ -19,16 +19,14 @@ public KeywordsResponse(final List data) { this.data = data; } - public static KeywordsResponse from(final List keywords) { + public static KeywordsResponse of(final List keywords, + final Map totalQuizCounts, + final Map answeredQuizCounts) { final List keywordsResponse = keywords.stream() .filter(Keyword::isRoot) - .map(KeywordResponse::createWithAllChildResponse) + .map(rootKeyword -> KeywordResponse.createWithAllChildResponse(rootKeyword, totalQuizCounts, answeredQuizCounts)) .collect(Collectors.toList()); return new KeywordsResponse(keywordsResponse); } - - public void setProgress(final Map totalQuizCounts, final Map answeredQuizCounts) { - data.forEach(response -> response.setProgress(totalQuizCounts, answeredQuizCounts)); - } } diff --git a/backend/src/test/java/wooteco/prolog/roadmap/application/RoadMapServiceTest.java b/backend/src/test/java/wooteco/prolog/roadmap/application/RoadMapServiceTest.java index 85c05b86a..fac37a2ff 100644 --- a/backend/src/test/java/wooteco/prolog/roadmap/application/RoadMapServiceTest.java +++ b/backend/src/test/java/wooteco/prolog/roadmap/application/RoadMapServiceTest.java @@ -153,7 +153,7 @@ void findAllKeywordsWithProgress_login() { private Consumer> containsAnsweredQuizCounts(final Integer... expected) { return keywords -> assertThat(keywords) - .map(KeywordResponse::getDoneQuizCount) + .map(KeywordResponse::getAnsweredQuizCount) .containsExactlyInAnyOrder(expected); }