From 8cb2c4c564489378366e4ace49099e9c88484b5d Mon Sep 17 00:00:00 2001 From: rlacksgus97 Date: Mon, 30 Sep 2024 22:59:32 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20legend=EC=97=90=EC=84=9C=20option=20text?= =?UTF-8?q?,=20value=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/BinaryDashboardComponent.java | 15 ++++++++++++--- .../dashboard/model/RatioDashboardComponent.java | 16 ++++++++++++---- .../namuiwiki/domain/statistic/model/Legend.java | 2 -- .../domain/statistic/model/RatioStatistic.java | 4 ++-- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java index 07d07571..4d6be5a3 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/BinaryDashboardComponent.java @@ -3,12 +3,15 @@ import com.dnd.namuiwiki.common.exception.ApplicationErrorException; import com.dnd.namuiwiki.common.exception.ApplicationErrorType; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.option.entity.Option; import com.dnd.namuiwiki.domain.question.entity.Question; import com.dnd.namuiwiki.domain.statistic.model.Legend; import com.dnd.namuiwiki.domain.statistic.model.RatioStatistic; import com.dnd.namuiwiki.domain.statistic.model.Statistic; import lombok.Getter; +import java.util.Map; + @Getter public class BinaryDashboardComponent extends DashboardComponentV2 { private final int percentage; @@ -22,15 +25,21 @@ public BinaryDashboardComponent(Statistic statistic, Question question) { RatioStatistic ratioStatistic = (RatioStatistic) statistic; - this.percentage = getPercentage(ratioStatistic); + this.percentage = getPercentage(ratioStatistic, question); } - private int getPercentage(RatioStatistic ratioStatistic) { + private int getPercentage(RatioStatistic ratioStatistic, Question question) { Legend trueLegend = ratioStatistic.getLegends().stream() - .filter(legend -> (boolean) legend.getValue()) + .filter(legend -> (boolean) getValue(question, legend)) .findFirst() .orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.INTERNAL_ERROR)); return (int) (trueLegend.getCount() * 100 / ratioStatistic.getTotalCount()); } + private Object getValue(Question question, Legend legend) { + Map options = question.getOptions(); + Option option = options.get(legend.getOptionId()); + return option.getValue(); + } + } diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java index a42773f7..cf8921e8 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/RatioDashboardComponent.java @@ -2,6 +2,7 @@ import com.dnd.namuiwiki.domain.dashboard.model.dto.RatioDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; +import com.dnd.namuiwiki.domain.option.entity.Option; import com.dnd.namuiwiki.domain.question.entity.Question; import com.dnd.namuiwiki.domain.statistic.model.Legend; import com.dnd.namuiwiki.domain.statistic.model.RatioStatistic; @@ -10,6 +11,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; @Getter @@ -23,10 +25,10 @@ public RatioDashboardComponent(DashboardType dashboardType, Statistic statistic, throw new IllegalArgumentException("Required RatioDashboardType"); } - calculate((RatioStatistic) statistic); + calculate((RatioStatistic) statistic, question); } - private void calculate(RatioStatistic statistic) { + private void calculate(RatioStatistic statistic, Question question) { Long totalCount = statistic.getTotalCount(); List legends = statistic.getLegends(); @@ -36,12 +38,12 @@ private void calculate(RatioStatistic statistic) { for (Legend legend : legends) { if (totalCount == 0) { - rank.add(new RatioDto(legend.getText(), 0)); + rank.add(new RatioDto(getText(question, legend), 0)); continue; } int percentage = (int) (legend.getCount() * 100 / totalCount); optionPercentage += percentage; - rank.add(new RatioDto(legend.getText(), percentage)); + rank.add(new RatioDto(getText(question, legend), percentage)); } // 직접입력인 legend 인거 찾아서 새로 업데이트 @@ -55,4 +57,10 @@ private void updateManualLegendPercentage(int optionPercentage) { manualLegend.ifPresent(ratioDto -> ratioDto.setPercentage(100 - optionPercentage)); } + private String getText(Question question, Legend legend) { + Map options = question.getOptions(); + Option option = options.get(legend.getOptionId()); + return option.getText(); + } + } diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/Legend.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/Legend.java index dd75f858..071e115b 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/Legend.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/Legend.java @@ -7,8 +7,6 @@ @AllArgsConstructor public class Legend { private String optionId; - private String text; - private Object value; private Long count; public Long increaseCount() { diff --git a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/RatioStatistic.java b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/RatioStatistic.java index f7f04b7c..131d1705 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/statistic/model/RatioStatistic.java +++ b/src/main/java/com/dnd/namuiwiki/domain/statistic/model/RatioStatistic.java @@ -37,7 +37,7 @@ public List getLegends() { public static RatioStatistic create(Question question) { Map legends = new HashMap<>(); - question.getOptions().forEach((key, value) -> legends.put(key, new Legend(key, value.getText(), value.getValue(), 0L))); + question.getOptions().forEach((key, value) -> legends.put(key, new Legend(key, 0L))); return new RatioStatistic( question.getId(), question.getName(), @@ -61,7 +61,7 @@ private void increaseOptionCount(Answer answer) { Legend legend = getLegend(optionId) .orElseGet(() -> { Option option = question.getOption(optionId); - return new Legend(option.getId(), option.getText(), option.getValue(), 0L); + return new Legend(option.getId(), 0L); }); legend.increaseCount(); }