Skip to content

Commit

Permalink
rename: Dashboard를 유저와 전체로 분리하여 조회하도록 수정(#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
eun-seong committed Mar 6, 2024
1 parent 35418a0 commit 846c01e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +19,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -31,13 +34,20 @@ public DashboardDto getDashboard(TokenUserInfoDto tokenUserInfoDto, Period perio
validateFilterCategory(period, relation);

User user = findByWikiId(tokenUserInfoDto.getWikiId());
Optional<Dashboard> dashboard = dashboardRepository.findByUserAndPeriodAndRelation(user, period, relation);
if (dashboard.isEmpty()) {
Optional<Dashboard> optionalDashboard = dashboardRepository.findByUserAndPeriodAndRelation(user, period, relation);
if (optionalDashboard.isEmpty()) {
return null;
}
Dashboard dashboard = optionalDashboard.get();

List<DashboardComponent> 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<DashboardComponent> components = new ArrayList<>(userDashboards);
components.add(populationDashboard);

return new DashboardDto(components);
}

private void validateFilterCategory(Period period, Relation relation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public void updateStatistics(List<Answer> answers) {
});
}

public List<Statistic> getStatisticsByDashboardType(DashboardType dashboardType) {
return statistics.values().stream()
.filter(statistic -> statistic.getDashboardType().equals(dashboardType))
.toList();
}

public Map<DashboardType, List<Statistic>> mapStatisticsByDashboardType() {
Map<DashboardType, List<Statistic>> statistics = new HashMap<>();
this.statistics.values().forEach(statistic -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,17 +41,19 @@ public void updateStatistics(List<Answer> answer) {
statistics.updateStatistics(answer);
}

public DashboardDto convertDashboardDto(PopulationStatistic populationStatistic) {
public List<DashboardComponent> getUserDashboards() {
var dashboardTypeListMap = statistics.mapStatisticsByDashboardType();

List<DashboardComponent> 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<Statistic> statisticsByDashboardType = statistics.getStatisticsByDashboardType(dashboardType);

return DashboardFactory.create(dashboardType, statisticsByDashboardType, populationStatistic);
}

public static Dashboard createNew(User owner, Period period, Relation relation, List<Answer> answers) {
Expand Down

0 comments on commit 846c01e

Please sign in to comment.