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) {