Skip to content

Commit eb8f238

Browse files
authored
Merge pull request #26 from Team-NewsLetter/fix/#24
fix : 연속일 수정
2 parents b387914 + 244ac69 commit eb8f238

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

src/main/java/com/swacademy/newsletter/repository/user/UserNewsHistoryQueryRepository.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ public List<UserInfoResponseDto.DailyNewsCheckDto> getDailyNewsCheck(Long userId
3939
return IntStream.range(0, 7)
4040
.mapToObj(i -> {
4141
LocalDate date = monday.plusDays(i);
42-
LocalDateTime start = date.atStartOfDay(zone).toLocalDateTime();
43-
LocalDateTime end = date.atTime(LocalTime.MAX).atZone(zone).toLocalDateTime();
42+
43+
ZonedDateTime zonedStart = date.atStartOfDay(zone);
44+
ZonedDateTime zonedEnd = date.atTime(LocalTime.MAX).atZone(zone);
45+
46+
LocalDateTime start = zonedStart.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
47+
LocalDateTime end = zonedEnd.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
4448

4549
boolean checked = query
4650
.selectOne()
@@ -49,15 +53,31 @@ public List<UserInfoResponseDto.DailyNewsCheckDto> getDailyNewsCheck(Long userId
4953
.and(newsHistory.readAt.between(start, end)))
5054
.fetchFirst() != null;
5155

52-
return new UserInfoResponseDto.DailyNewsCheckDto(date.getDayOfMonth(), checked);
56+
return new UserInfoResponseDto.DailyNewsCheckDto(date.getDayOfMonth(), checked, date);
5357
})
5458
.toList();
5559
}
5660

5761
public int countConsecutiveDays(List<UserInfoResponseDto.DailyNewsCheckDto> checks) {
58-
return (int) checks.stream()
59-
.sorted(Comparator.comparingInt(UserInfoResponseDto.DailyNewsCheckDto::getDay))
60-
.takeWhile(UserInfoResponseDto.DailyNewsCheckDto::getChecked)
61-
.count();
62+
List<UserInfoResponseDto.DailyNewsCheckDto> sorted = checks.stream()
63+
.filter(check -> check.getDate() != null)
64+
.sorted(Comparator.comparing(UserInfoResponseDto.DailyNewsCheckDto::getDate))
65+
.toList();
66+
67+
int count = 0;
68+
LocalDate expected = null;
69+
70+
for (UserInfoResponseDto.DailyNewsCheckDto check : sorted) {
71+
if (!Boolean.TRUE.equals(check.getChecked())) break;
72+
if (expected == null) {
73+
expected = check.getDate();
74+
} else if (!check.getDate().equals(expected)) {
75+
break;
76+
}
77+
count++;
78+
expected = expected.plusDays(1);
79+
}
80+
81+
return count;
6282
}
6383
}

src/main/java/com/swacademy/newsletter/web/dto/response/user/UserInfoResponseDto.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.swacademy.newsletter.web.dto.response.user;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import lombok.*;
5+
6+
import java.time.LocalDate;
47
import java.util.List;
58

69
@Getter
@@ -23,6 +26,9 @@ public class UserInfoResponseDto {
2326
public static class DailyNewsCheckDto {
2427
private Integer day; // 예: 1, 2
2528
private Boolean checked; // 예: true, false
29+
30+
@JsonIgnore
31+
private LocalDate date; // 내부 연산용
2632
}
2733

2834
@Getter

0 commit comments

Comments
 (0)