Skip to content

Commit

Permalink
MATE-146 : [FIX] 배너 경기 데이터 조회 SQL 수정 (#130)
Browse files Browse the repository at this point in the history
* MATE-146 : [FIX] 배너 경기 데이터 조회 SQL 수정
- 1. Native Query 대신 JPQL Pageable 사용

* MATE-146 : [CHORE] 주석 삭제
  • Loading branch information
juchan204 authored Jan 8, 2025
1 parent 09ffe8d commit f7fc9e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.mate.domain.match.entity.Match;
import com.example.mate.domain.match.entity.MatchStatus;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -13,16 +14,18 @@

@Repository
public interface MatchRepository extends JpaRepository<Match, Long> {
@Query(value = "SELECT * FROM \"match\" m " +
"WHERE m.match_time > :now " +
"ORDER BY m.match_time ASC LIMIT 5", nativeQuery = true)
List<Match> findMainBannerMatches(@Param("now") LocalDateTime now);
@Query("SELECT m FROM Match m WHERE m.matchTime > :now ORDER BY m.matchTime ASC")
List<Match> findMainBannerMatches(@Param("now") LocalDateTime now, Pageable pageable);

@Query(value = "SELECT * FROM \"match\" m " +
"WHERE (m.home_team_id = :teamId OR m.away_team_id = :teamId) " +
"AND m.match_time > :now " +
"ORDER BY m.match_time ASC LIMIT 3", nativeQuery = true)
List<Match> findTop3TeamMatchesAfterNow(@Param("teamId") Long teamId, @Param("now") LocalDateTime now);
@Query("SELECT m FROM Match m " +
"WHERE (m.homeTeamId = :teamId OR m.awayTeamId = :teamId) " +
"AND m.matchTime > :now " +
"ORDER BY m.matchTime ASC")
List<Match> findTop3TeamMatchesAfterNow(
@Param("teamId") Long teamId,
@Param("now") LocalDateTime now,
Pageable pageable
);

@Query("SELECT m FROM Match m " +
"WHERE (m.status = :status1 AND m.homeTeamId = :homeTeamId) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.example.mate.domain.match.repository.MatchRepository;
import com.example.mate.domain.match.util.WeekCalculator;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
Expand All @@ -27,16 +28,15 @@ public class MatchService {
private static final int WEEKS_TO_FETCH = 4;

public List<MatchResponse> getMainBannerMatches() {
return matchRepository.findMainBannerMatches(LocalDateTime.now())
return matchRepository.findMainBannerMatches(LocalDateTime.now(), PageRequest.of(0, 5))
.stream()
.map(match -> MatchResponse.from(match, null))
.collect(Collectors.toList());
}


public List<MatchResponse> getTeamMatches(Long teamId) {
TeamInfo.getById(teamId);
return matchRepository.findTop3TeamMatchesAfterNow(teamId, LocalDateTime.now())
return matchRepository.findTop3TeamMatchesAfterNow(teamId, LocalDateTime.now(), PageRequest.of(0, 3))
.stream()
.map(match -> MatchResponse.from(match, teamId))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -45,14 +47,18 @@ void getMainBannerMatches_ShouldReturnTop5Matches() {
// Given
LocalDateTime now = LocalDateTime.now();
List<Match> matches = createTestMatches();
when(matchRepository.findMainBannerMatches(any(LocalDateTime.class))).thenReturn(matches);
when(matchRepository.findMainBannerMatches(any(LocalDateTime.class), any(Pageable.class)))
.thenReturn(matches);

// When
List<MatchResponse> result = matchService.getMainBannerMatches();

// Then
assertThat(result).hasSize(5);
verify(matchRepository).findMainBannerMatches(any(LocalDateTime.class));
verify(matchRepository).findMainBannerMatches(
any(LocalDateTime.class),
eq(PageRequest.of(0, 5))
);
}

@Test
Expand All @@ -61,15 +67,22 @@ void getTeamMatches_ShouldReturnTop3MatchesForTeam() {
// Given
Long teamId = 1L;
List<Match> matches = createTestMatches().subList(0, 3);
when(matchRepository.findTop3TeamMatchesAfterNow(eq(teamId), any(LocalDateTime.class)))
.thenReturn(matches);
when(matchRepository.findTop3TeamMatchesAfterNow(
eq(teamId),
any(LocalDateTime.class),
any(Pageable.class)
)).thenReturn(matches);

// When
List<MatchResponse> result = matchService.getTeamMatches(teamId);

// Then
assertThat(result).hasSize(3);
verify(matchRepository).findTop3TeamMatchesAfterNow(eq(teamId), any(LocalDateTime.class));
verify(matchRepository).findTop3TeamMatchesAfterNow(
eq(teamId),
any(LocalDateTime.class),
eq(PageRequest.of(0, 3))
);
}

@Test
Expand Down

0 comments on commit f7fc9e2

Please sign in to comment.