Skip to content
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
8 changes: 5 additions & 3 deletions src/main/java/com/mr/domain/home/service/HomeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
import com.mr.domain.user.repository.UserRepository;
import com.mr.global.apipayload.exception.GeneralException;
import com.mr.global.util.RelativeDateFormatter;
import java.sql.Date;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
Expand Down Expand Up @@ -85,8 +86,9 @@ private List<RecommendedLearning> buildRecommendedLearnings(

// 연속 출석일수는 기간 상한이 없어야 하므로 별도로 전체 기간 날짜만 조회
private Set<LocalDate> fetchPracticeDates(Long userId) {
return new HashSet<>(
playingRepository.findDistinctEndedDatesByUserAndStatus(userId, PlayingStatus.COMPLETED));
return playingRepository.findDistinctEndedDatesByUserAndStatus(userId, PlayingStatus.COMPLETED).stream()
.map(Date::toLocalDate)
.collect(Collectors.toSet());
}

private UserSummary buildUserSummary(User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.mr.domain.playing.entity.Playing;
import com.mr.domain.playing.entity.enums.PlayingStatus;
import jakarta.persistence.LockModeType;
import java.time.LocalDate;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -85,7 +85,7 @@ select distinct function('date', p.endedAt) from Playing p
and p.deletedAt is null
and p.endedAt is not null
""")
List<LocalDate> findDistinctEndedDatesByUserAndStatus(
List<Date> findDistinctEndedDatesByUserAndStatus(
@Param("userId") Long userId,
@Param("status") PlayingStatus status
);
Expand Down
14 changes: 10 additions & 4 deletions src/test/java/com/mr/domain/home/service/HomeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.mr.domain.user.repository.StudentRepository;
import com.mr.domain.user.repository.UserRepository;
import com.mr.global.apipayload.exception.GeneralException;
import java.sql.Date;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -90,6 +91,10 @@ private Playing mockPlaying(LocalDateTime endedAt, Integer durationSec) {
return playing;
}

private Date sqlDate(LocalDate date) {
return Date.valueOf(date);
}

@Test
@DisplayName("getHome - 존재하지 않는 사용자면 404")
void getHome_userNotFound_throws404() {
Expand Down Expand Up @@ -140,7 +145,7 @@ void getHome_threeConsecutiveDaysIncludingToday_currentDaysIsThree() {

LocalDate today = LocalDate.now();
given(playingRepository.findDistinctEndedDatesByUserAndStatus(1L, PlayingStatus.COMPLETED)).willReturn(List.of(
today, today.minusDays(1), today.minusDays(2), today.minusDays(5) // 연속 끊김
sqlDate(today), sqlDate(today.minusDays(1)), sqlDate(today.minusDays(2)), sqlDate(today.minusDays(5))
));

HomeResponseDTO response = homeService.getHome(1L);
Expand All @@ -154,8 +159,9 @@ void getHome_longStreak_notCappedByLookbackWindow() {
stubBaseline(1L);

LocalDate today = LocalDate.now();
List<LocalDate> endedDates = java.util.stream.IntStream.range(0, 65)
List<Date> endedDates = java.util.stream.IntStream.range(0, 65)
.mapToObj(today::minusDays)
.map(this::sqlDate)
.toList();
given(playingRepository.findDistinctEndedDatesByUserAndStatus(1L, PlayingStatus.COMPLETED)).willReturn(endedDates);

Expand All @@ -171,7 +177,7 @@ void getHome_noPracticeToday_countsFromYesterday() {

LocalDate yesterday = LocalDate.now().minusDays(1);
given(playingRepository.findDistinctEndedDatesByUserAndStatus(1L, PlayingStatus.COMPLETED))
.willReturn(List.of(yesterday));
.willReturn(List.of(sqlDate(yesterday)));

HomeResponseDTO response = homeService.getHome(1L);

Expand All @@ -185,7 +191,7 @@ void getHome_duplicateDateFromRepository_countsOnce() {

LocalDate today = LocalDate.now();
given(playingRepository.findDistinctEndedDatesByUserAndStatus(1L, PlayingStatus.COMPLETED))
.willReturn(List.of(today, today, today));
.willReturn(List.of(sqlDate(today), sqlDate(today), sqlDate(today)));

HomeResponseDTO response = homeService.getHome(1L);

Expand Down
Loading