Skip to content

Commit

Permalink
refactor: 진도율 setter 제거 후 dto 내부에서 매핑하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Nov 22, 2023
1 parent d94a493 commit e9cc339
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -84,14 +85,14 @@ public KeywordsResponse findSessionIncludeRootKeywords(final Long sessionId) {

List<Keyword> keywords = keywordRepository.findBySessionIdAndParentIsNull(sessionId);

return KeywordsResponse.from(keywords);
return KeywordsResponse.of(keywords, emptyMap(), emptyMap());
}

@Transactional(readOnly = true)
public KeywordsResponse newFindSessionIncludeRootKeywords() {
List<Keyword> keywords = keywordRepository.newFindByParentIsNull();

return KeywordsResponse.from(keywords);
return KeywordsResponse.of(keywords, emptyMap(), emptyMap());
}

public void updateKeyword(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public KeywordsResponse findAllKeywordsWithProgress(final Long curriculumId, fin
final Map<Long, Integer> totalQuizCounts = getTotalQuizCounts();
final Map<Long, Integer> answeredQuizCounts = getAnsweredQuizCounts(memberId);

final KeywordsResponse keywordsResponse = KeywordsResponse.from(keywords);
keywordsResponse.setProgress(totalQuizCounts, answeredQuizCounts);

return keywordsResponse;
return KeywordsResponse.of(keywords, totalQuizCounts, answeredQuizCounts);
}

private Map<Long, Integer> getTotalQuizCounts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RecommendedPostResponse> recommendedPosts;
private List<KeywordResponse> 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<RecommendedPostResponse> recommendedPosts,
final List<KeywordResponse> childrenKeywords) {
this.keywordId = keywordId;
Expand All @@ -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;
Expand All @@ -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<Long, Integer> totalQuizCounts,
final Map<Long, Integer> 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<RecommendedPostResponse> createRecommendedPostResponses(final Keyword keyword) {
Expand All @@ -75,16 +78,11 @@ private static List<RecommendedPostResponse> createRecommendedPostResponses(fina
.collect(Collectors.toList());
}

private static List<KeywordResponse> createChildren(final Set<Keyword> children) {
private static List<KeywordResponse> createChildren(final Set<Keyword> children,
final Map<Long, Integer> totalQuizCounts,
final Map<Long, Integer> answeredQuizCounts) {
return children.stream()
.map(KeywordResponse::createWithAllChildResponse)
.map(keyword -> createWithAllChildResponse(keyword, totalQuizCounts, answeredQuizCounts))
.collect(Collectors.toList());
}

public void setProgress(final Map<Long, Integer> totalQuizCounts, final Map<Long, Integer> answeredQuizCounts) {
totalQuizCount = totalQuizCounts.getOrDefault(keywordId, 0);
doneQuizCount = answeredQuizCounts.getOrDefault(keywordId, 0);

childrenKeywords.forEach(child -> child.setProgress(totalQuizCounts, answeredQuizCounts));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ public KeywordsResponse(final List<KeywordResponse> data) {
this.data = data;
}

public static KeywordsResponse from(final List<Keyword> keywords) {
public static KeywordsResponse of(final List<Keyword> keywords,
final Map<Long, Integer> totalQuizCounts,
final Map<Long, Integer> answeredQuizCounts) {
final List<KeywordResponse> 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<Long, Integer> totalQuizCounts, final Map<Long, Integer> answeredQuizCounts) {
data.forEach(response -> response.setProgress(totalQuizCounts, answeredQuizCounts));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void findAllKeywordsWithProgress_login() {

private Consumer<List<? extends KeywordResponse>> containsAnsweredQuizCounts(final Integer... expected) {
return keywords -> assertThat(keywords)
.map(KeywordResponse::getDoneQuizCount)
.map(KeywordResponse::getAnsweredQuizCount)
.containsExactlyInAnyOrder(expected);
}

Expand Down

0 comments on commit e9cc339

Please sign in to comment.