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
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ public HistoryListResponseDTO getHistories(Long userId, int page, int size, Hist
validatePaging(page, size);

LocalDateTime cutoff = resolveCutoff(period);
Slice<Playing> slice = playingRepository.findPlayingsByUserAndStatus(
userId, PlayingStatus.COMPLETED, cutoff, PageRequest.of(page, size));
PageRequest pageRequest = PageRequest.of(page, size);
Slice<Playing> slice = cutoff == null
? playingRepository.findPlayingsByUserAndStatus(
userId, PlayingStatus.COMPLETED, pageRequest)
: playingRepository.findPlayingsByUserAndStatusSince(
userId, PlayingStatus.COMPLETED, cutoff, pageRequest);

List<Playing> playings = slice.getContent();
Map<Long, Analysis> latestByPlayingId = fetchLatestCompletedAnalyses(playings);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/mr/domain/home/service/HomeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private int sumDurationSince(List<Playing> playings, LocalDateTime since) {

private List<RecentPlaying> buildRecentPlayings(Long userId) {
Slice<Playing> slice = playingRepository.findPlayingsByUserAndStatus(
userId, PlayingStatus.COMPLETED, null, PageRequest.of(0, RECENT_PLAYINGS_LIMIT));
userId, PlayingStatus.COMPLETED, PageRequest.of(0, RECENT_PLAYINGS_LIMIT));

return slice.getContent().stream()
.map(playing -> RecentPlaying.of(playing, RelativeDateFormatter.format(playing.getEndedAt())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,24 @@ public interface PlayingRepository extends JpaRepository<Playing, Long> {
where p.user.userId = :userId
and p.status = :status
and p.deletedAt is null
and (:cutoff is null or p.endedAt >= :cutoff)
order by p.endedAt desc, p.id desc
""")
Slice<Playing> findPlayingsByUserAndStatus(
@Param("userId") Long userId,
@Param("status") PlayingStatus status,
Pageable pageable
);

@Query("""
select p from Playing p
left join fetch p.backingTrack
where p.user.userId = :userId
and p.status = :status
and p.deletedAt is null
and p.endedAt >= :cutoff
order by p.endedAt desc, p.id desc
""")
Slice<Playing> findPlayingsByUserAndStatusSince(
@Param("userId") Long userId,
@Param("status") PlayingStatus status,
@Param("cutoff") LocalDateTime cutoff,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;

import com.mr.domain.analysis.entity.Analysis;
import com.mr.domain.analysis.entity.enums.AnalysisGrade;
import com.mr.domain.analysis.entity.enums.AnalysisStatus;
import com.mr.domain.analysis.repository.AnalysisRepository;
import com.mr.domain.history.dto.req.HistoryPeriod;
import com.mr.domain.history.dto.res.HistoryDetailResponseDTO;
import com.mr.domain.history.dto.res.HistoryListResponseDTO;
import com.mr.domain.history.exception.HistoryErrorStatus;
Expand Down Expand Up @@ -83,7 +85,7 @@ private Analysis completedAnalysis(Long playingId, Integer totalScore) {
@Test
@DisplayName("getHistories - 결과가 0건이면 빈 목록을 반환하고 Analysis는 조회하지 않는다")
void getHistories_empty_returnsEmptyList() {
given(playingRepository.findPlayingsByUserAndStatus(eq(1L), eq(PlayingStatus.COMPLETED), any(), any()))
given(playingRepository.findPlayingsByUserAndStatus(eq(1L), eq(PlayingStatus.COMPLETED), any()))
.willReturn(new SliceImpl<>(List.of(), PageRequest.of(0, 10), false));

HistoryListResponseDTO response = historyService.getHistories(1L, 0, 10, null);
Expand All @@ -97,7 +99,7 @@ void getHistories_empty_returnsEmptyList() {
@DisplayName("getHistories - 최신 COMPLETED 분석이 없는 Playing은 latestAnalysisId가 null이다")
void getHistories_noCompletedAnalysis_latestAnalysisIdIsNull() {
Playing playing = mockPlaying(1L, 1L, PlayingStatus.COMPLETED, LocalDateTime.now());
given(playingRepository.findPlayingsByUserAndStatus(eq(1L), eq(PlayingStatus.COMPLETED), any(), any()))
given(playingRepository.findPlayingsByUserAndStatus(eq(1L), eq(PlayingStatus.COMPLETED), any()))
.willReturn(new SliceImpl<>(List.of(playing), PageRequest.of(0, 10), false));
given(analysisRepository.findByPlayingIdInAndStatusOrderByCreatedAtDescIdDesc(
anyList(), eq(AnalysisStatus.COMPLETED)))
Expand All @@ -115,7 +117,7 @@ void getHistories_noCompletedAnalysis_latestAnalysisIdIsNull() {
void getHistories_scoreChange_adjacentComparisonOnly() {
Playing playing1 = mockPlaying(1L, 1L, PlayingStatus.COMPLETED, LocalDateTime.now());
Playing playing2 = mockPlaying(2L, 1L, PlayingStatus.COMPLETED, LocalDateTime.now().minusDays(1));
given(playingRepository.findPlayingsByUserAndStatus(eq(1L), eq(PlayingStatus.COMPLETED), any(), any()))
given(playingRepository.findPlayingsByUserAndStatus(eq(1L), eq(PlayingStatus.COMPLETED), any()))
.willReturn(new SliceImpl<>(List.of(playing1, playing2), PageRequest.of(0, 10), false));

Analysis analysis1 = completedAnalysis(1L, 90);
Expand All @@ -130,6 +132,20 @@ void getHistories_scoreChange_adjacentComparisonOnly() {
assertThat(response.items().get(1).scoreChange()).isNull();
}

@Test
@DisplayName("getHistories - 기간 필터가 있으면 cutoff 전용 쿼리를 사용한다")
void getHistories_withPeriod_usesSinceQuery() {
given(playingRepository.findPlayingsByUserAndStatusSince(
eq(1L), eq(PlayingStatus.COMPLETED), any(LocalDateTime.class), eq(PageRequest.of(0, 10))))
.willReturn(new SliceImpl<>(List.of(), PageRequest.of(0, 10), false));

HistoryListResponseDTO response = historyService.getHistories(1L, 0, 10, HistoryPeriod.WEEKLY);

assertThat(response.items()).isEmpty();
verify(playingRepository).findPlayingsByUserAndStatusSince(
eq(1L), eq(PlayingStatus.COMPLETED), any(LocalDateTime.class), eq(PageRequest.of(0, 10)));
}

@Test
@DisplayName("getHistories - page가 음수면 400")
void getHistories_negativePage_throws400() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/mr/domain/home/service/HomeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void stubBaseline(Long userId) {
lenient().when(studentRepository.findByUser(user)).thenReturn(Optional.empty());
lenient().when(playingRepository.findDistinctEndedDatesByUserAndStatus(anyLong(), any())).thenReturn(List.of());
lenient().when(playingRepository.findByUserAndStatusSince(anyLong(), any(), any())).thenReturn(List.of());
lenient().when(playingRepository.findPlayingsByUserAndStatus(anyLong(), any(), any(), any()))
lenient().when(playingRepository.findPlayingsByUserAndStatus(anyLong(), any(), any()))
.thenReturn(new SliceImpl<>(List.of()));
lenient().when(learningService.getCurrentLearning(anyLong())).thenReturn(null);
}
Expand Down Expand Up @@ -336,7 +336,7 @@ void getHome_recentPlayings_mapsFromRepository() {
stubBaseline(1L);

Playing playing = mockPlaying(LocalDateTime.now(), 600);
given(playingRepository.findPlayingsByUserAndStatus(1L, PlayingStatus.COMPLETED, null, PageRequest.of(0, 5)))
given(playingRepository.findPlayingsByUserAndStatus(1L, PlayingStatus.COMPLETED, PageRequest.of(0, 5)))
.willReturn(new SliceImpl<>(List.of(playing)));

HomeResponseDTO response = homeService.getHome(1L);
Expand Down
Loading