Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE-103] : 캘린더 통계 조회 API 응답 로직 리팩토링 #105

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class GetStatisticsResponseDto {
private long totalPayment; // 받은 총 금액(냈어요)
private long totalReceiveAmount; // 낸 총금액(받았어요)
private String mostEventCategory; // 가장 많은 비용을 지출한 경조사
private List<String> mostEventCategory; // 가장 많은 비용을 지출한 경조사
private Long mostEventPayAmount; // 가장 많은 비용을 지출한 경조사 비용
private Map<String, List<StatisticsListResponse>> statisticsListResponse;
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,38 @@ public GetStatisticsResponseDto getStatistics(String time) {
.map(AcEvent::toStatisticsListResponseDto)
.forEach(responseDtoList::add);

if (responseDtoList.isEmpty()) {
return null;
}

// 2. EventAt 최신순 정렬(내림차순)
responseDtoList.sort(Comparator.comparing(StatisticsListResponse::getEventAt).reversed());

// 3. Map 으로 날짜별 그룹화
Map<String, List<StatisticsListResponse>> resultList = responseDtoList.stream()
.collect(Collectors.groupingBy(dto -> dto.getEventAt().toString()));


// 4 - 1. acEvents 에서 가장 많은 비용을 지출한 eventCategory
Map<EventCategory, Long> totalPayByCategory = acEvents.stream()
.collect(Collectors.groupingBy(AcEvent::getEventCategory,
Collectors.summingLong(AcEvent::getPayAmount)));

// 4 - 2. 가장 많은 비용을 지출한 EventCategory를 찾습니다.
Optional<Map.Entry<EventCategory, Long>> maxEntry = totalPayByCategory.entrySet().stream()
.max(Map.Entry.comparingByValue());
OptionalLong maxPayAmount = totalPayByCategory.values().stream().mapToLong(Long::longValue).max();

List<String> maxPayCategories = totalPayByCategory.entrySet().stream()
.filter(entry -> entry.getValue().equals(maxPayAmount.orElseThrow()))
.map(entry -> entry.getKey().getCategoryName())
.collect(Collectors.toList());

// 5. GetStatisticsResponseDto 반환
return GetStatisticsResponseDto.builder()
.totalPayment(myEvents.stream().mapToLong(MyEvent::getTotalReceiveAmount).sum())
.totalPayment(acEvents.stream().mapToLong(AcEvent::getPayAmount).sum())
.statisticsListResponse(resultList)
.mostEventCategory(maxEntry.orElse(null).getKey().getCategoryName())
.mostEventPayAmount(maxEntry.orElse(null).getValue())
.mostEventCategory(maxPayCategories)
.mostEventPayAmount(maxPayAmount.orElse(0L))
.build();
}

Expand Down
Loading