Skip to content

Commit

Permalink
fix: Click history 계산 부분 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Seungpang committed Jul 4, 2024
1 parent 808b631 commit 024cd55
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClickCountHistory> histories = clickCountHistoryRepository
.findClickCountHistoryByNameAndDateBetweenOrderByDate(name, startDate, endDate);

final Map<LocalDate, Long> dateToClickCount = mapHistoriesToClickCount(histories);
List<ClickCountHistoryResponse> responses = buildResponse(startDate, dateToClickCount);
final List<ClickCountHistoryResponse> responses = buildResponse(startDate, endDate, dateToClickCount);

return new ClickCountHistoriesResponse(responses);
}

private static Map<LocalDate, Long> mapHistoriesToClickCount(final List<ClickCountHistory> histories) {
private Map<LocalDate, Long> mapHistoriesToClickCount(final List<ClickCountHistory> histories) {
return histories.stream()
.collect(Collectors.toMap(
ClickCountHistory::getDate,
ClickCountHistory::getClickCount)
);
ClickCountHistory::getClickCount
));
}

private static List<ClickCountHistoryResponse> buildResponse(final LocalDate startDate, final Map<LocalDate, Long> dateToClickCount) {
return IntStream.rangeClosed(0, 6)
.mapToObj(startDate::plusDays)
private List<ClickCountHistoryResponse> buildResponse(
final LocalDate startDate,
final LocalDate endDate,
final Map<LocalDate, Long> dateToClickCount
) {
return startDate.datesUntil(endDate.plusDays(1))
.map(date -> new ClickCountHistoryResponse(date, dateToClickCount.getOrDefault(date, 0L)))
.toList();
}
Expand Down

0 comments on commit 024cd55

Please sign in to comment.