From 846c01e9707a35aa2ca6d267dc324dc4265662c5 Mon Sep 17 00:00:00 2001 From: eun-seong Date: Wed, 6 Mar 2024 21:05:42 +0900 Subject: [PATCH] =?UTF-8?q?rename:=20Dashboard=EB=A5=BC=20=EC=9C=A0?= =?UTF-8?q?=EC=A0=80=EC=99=80=20=EC=A0=84=EC=B2=B4=EB=A1=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EC=97=AC=20=EC=A1=B0=ED=9A=8C=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95(#104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/dashboard/DashboardService.java | 16 +++++++++++--- .../domain/dashboard/model/Statistics.java | 6 ++++++ .../dashboard/model/entity/Dashboard.java | 21 +++++++++++-------- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java index 11b16dd..dd306bc 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/DashboardService.java @@ -2,8 +2,10 @@ import com.dnd.namuiwiki.common.exception.ApplicationErrorException; import com.dnd.namuiwiki.common.exception.ApplicationErrorType; +import com.dnd.namuiwiki.domain.dashboard.model.DashboardComponent; import com.dnd.namuiwiki.domain.dashboard.model.dto.DashboardDto; import com.dnd.namuiwiki.domain.dashboard.model.entity.Dashboard; +import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; import com.dnd.namuiwiki.domain.jwt.dto.TokenUserInfoDto; import com.dnd.namuiwiki.domain.question.type.QuestionName; import com.dnd.namuiwiki.domain.statistic.StatisticsService; @@ -17,6 +19,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -31,13 +34,20 @@ public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, Period perio validateFilterCategory(period, relation); User user = findByWikiId(tokenUserInfoDto.getWikiId()); - Optional dashboard = dashboardRepository.findByUserAndPeriodAndRelation(user, period, relation); - if (dashboard.isEmpty()) { + Optional optionalDashboard = dashboardRepository.findByUserAndPeriodAndRelation(user, period, relation); + if (optionalDashboard.isEmpty()) { return null; } + Dashboard dashboard = optionalDashboard.get(); + List userDashboards = dashboard.getUserDashboards(); PopulationStatistic populationStatistic = statisticsService.getPopulationStatistic(period, relation, QuestionName.BORROWING_LIMIT); - return dashboard.get().convertDashboardDto(populationStatistic); + DashboardComponent populationDashboard = dashboard.getPopulationDashboard(populationStatistic, DashboardType.MONEY); + + List components = new ArrayList<>(userDashboards); + components.add(populationDashboard); + + return new DashboardDto(components); } private void validateFilterCategory(Period period, Relation relation) { diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java index 7527c11..1b14c37 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/Statistics.java @@ -37,6 +37,12 @@ public void updateStatistics(List answers) { }); } + public List getStatisticsByDashboardType(DashboardType dashboardType) { + return statistics.values().stream() + .filter(statistic -> statistic.getDashboardType().equals(dashboardType)) + .toList(); + } + public Map> mapStatisticsByDashboardType() { Map> statistics = new HashMap<>(); this.statistics.values().forEach(statistic -> { diff --git a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java index 89d1474..1c0de6b 100644 --- a/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java +++ b/src/main/java/com/dnd/namuiwiki/domain/dashboard/model/entity/Dashboard.java @@ -3,6 +3,7 @@ import com.dnd.namuiwiki.common.model.BaseTimeEntity; import com.dnd.namuiwiki.domain.dashboard.model.DashboardComponent; import com.dnd.namuiwiki.domain.dashboard.model.DashboardFactory; +import com.dnd.namuiwiki.domain.dashboard.model.Statistic; import com.dnd.namuiwiki.domain.dashboard.model.Statistics; import com.dnd.namuiwiki.domain.dashboard.model.dto.DashboardDto; import com.dnd.namuiwiki.domain.dashboard.type.DashboardType; @@ -40,17 +41,19 @@ public void updateStatistics(List answer) { statistics.updateStatistics(answer); } - public DashboardDto convertDashboardDto(PopulationStatistic populationStatistic) { + public List getUserDashboards() { var dashboardTypeListMap = statistics.mapStatisticsByDashboardType(); - List dashboardComponents = List.of( - DashboardFactory.create(DashboardType.BEST_WORTH, dashboardTypeListMap.get(DashboardType.BEST_WORTH)), - DashboardFactory.create(DashboardType.SAD, dashboardTypeListMap.get(DashboardType.SAD)), - DashboardFactory.create(DashboardType.HAPPY, dashboardTypeListMap.get(DashboardType.HAPPY)), - DashboardFactory.create(DashboardType.CHARACTER, dashboardTypeListMap.get(DashboardType.CHARACTER)), - DashboardFactory.create(DashboardType.MONEY, dashboardTypeListMap.get(DashboardType.MONEY), populationStatistic) - ); - return new DashboardDto(dashboardComponents); + return dashboardTypeListMap.entrySet().stream() + .filter(entry -> entry.getKey().getAnalysisType().isUser()) + .map(entry -> DashboardFactory.create(entry.getKey(), entry.getValue())) + .toList(); + } + + public DashboardComponent getPopulationDashboard(PopulationStatistic populationStatistic, DashboardType dashboardType) { + List statisticsByDashboardType = statistics.getStatisticsByDashboardType(dashboardType); + + return DashboardFactory.create(dashboardType, statisticsByDashboardType, populationStatistic); } public static Dashboard createNew(User owner, Period period, Relation relation, List answers) {