diff --git a/click-service/src/main/java/clickme/clickme/history/application/ClickCountHistoryService.java b/click-service/src/main/java/clickme/clickme/history/application/ClickCountHistoryService.java index 2c433ff..c758ab3 100644 --- a/click-service/src/main/java/clickme/clickme/history/application/ClickCountHistoryService.java +++ b/click-service/src/main/java/clickme/clickme/history/application/ClickCountHistoryService.java @@ -11,36 +11,40 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import java.util.stream.IntStream; @Service @RequiredArgsConstructor public class ClickCountHistoryService { private final ClickCountHistoryRepository clickCountHistoryRepository; + public ClickCountHistoriesResponse findClickCountHistoryByNameAndDateBetween(final String name) { final LocalDate endDate = LocalDate.now().minusDays(1); final LocalDate startDate = endDate.minusDays(6); final List histories = clickCountHistoryRepository .findClickCountHistoryByNameAndDateBetweenOrderByDate(name, startDate, endDate); + final Map dateToClickCount = mapHistoriesToClickCount(histories); - List responses = buildResponse(startDate, dateToClickCount); + final List responses = buildResponse(startDate, endDate, dateToClickCount); return new ClickCountHistoriesResponse(responses); } - private static Map mapHistoriesToClickCount(final List histories) { + private Map mapHistoriesToClickCount(final List histories) { return histories.stream() .collect(Collectors.toMap( ClickCountHistory::getDate, - ClickCountHistory::getClickCount) - ); + ClickCountHistory::getClickCount + )); } - private static List buildResponse(final LocalDate startDate, final Map dateToClickCount) { - return IntStream.rangeClosed(0, 6) - .mapToObj(startDate::plusDays) + private List buildResponse( + final LocalDate startDate, + final LocalDate endDate, + final Map dateToClickCount + ) { + return startDate.datesUntil(endDate.plusDays(1)) .map(date -> new ClickCountHistoryResponse(date, dateToClickCount.getOrDefault(date, 0L))) .toList(); }