Skip to content

Commit

Permalink
MATE-144 : [FIX] 배너 경기 데이터 조회 수정 (#129)
Browse files Browse the repository at this point in the history
- 1. JPQL 변경 및 Native Query 사용
  • Loading branch information
juchan204 authored Jan 8, 2025
1 parent 98f0e73 commit 09ffe8d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
15 changes: 8 additions & 7 deletions http/Match.http
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@

### 전체 경기 조회 ###
### 메인 배너용 경기 조회(5개) (상단 배너)
GET http://localhost:8000/api/matches/main
GET http://localhost:8080/api/matches/main
Content-Type: application/json

### 팀별 경기 조회 (3개) (상단 배너)
GET http://localhost:8000/api/matches/team/1
GET http://localhost:8080/api/matches/team/1
Content-Type: application/json

### 팀별 완료된 경기 전적 조회
### KIA 팀 완료된 경기 전적 조회
GET http://localhost:8000/api/matches/team/1/completed
GET http://localhost:8080/api/matches/team/1/completed
Content-Type: application/json

### 팀별 주차별 경기 일정 조회 (KIA) - 날짜 지정
GET http://localhost:8000/api/matches/team/1/weekly?startDate=2024-11-15
Accept: application/json
GET http://localhost:8080/api/matches/team/1/weekly?startDate=2024-11-15
Content-Type: application/json

### 팀별 주차별 경기 일정 조회 (KIA) - 오늘 날짜 기준
GET http://localhost:8000/api/matches/team/1/weekly
Accept: application/json
GET http://localhost:8080/api/matches/team/2/weekly
Content-Type: application/json
Authorization: Bearer

### [Team API] ###
#################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ public List<TeamRecord> crawlTeamRankings() {
teamName, rank, gamesPlayed, wins, losses, draws, gamesBehind);

TeamRecord record = TeamRecord.builder()
.teamId(team.id) // teamId로 변경
.teamId(team.id)
.rank(rank)
.gamesPlayed(gamesPlayed)
.totalGames(144) // KBO 정규시즌 경기 수
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@

@Repository
public interface MatchRepository extends JpaRepository<Match, Long> {
List<Match> findTop5ByOrderByMatchTimeDesc();
@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);

List<Match> findTop3ByHomeTeamIdOrAwayTeamIdOrderByMatchTimeDesc(Long homeTeamId, Long awayTeamId);
@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.status = :status1 AND m.homeTeamId = :homeTeamId) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ public class MatchService {
private static final int WEEKS_TO_FETCH = 4;

public List<MatchResponse> getMainBannerMatches() {
return matchRepository.findTop5ByOrderByMatchTimeDesc().stream()
.filter(match -> match.getMatchTime().isAfter(LocalDateTime.now()))
return matchRepository.findMainBannerMatches(LocalDateTime.now())
.stream()
.map(match -> MatchResponse.from(match, null))
.collect(Collectors.toList());
}


public List<MatchResponse> getTeamMatches(Long teamId) {
TeamInfo.getById(teamId);

return matchRepository.findTop3ByHomeTeamIdOrAwayTeamIdOrderByMatchTimeDesc(teamId, teamId).stream()
.filter(match -> match.getMatchTime().isAfter(LocalDateTime.now()))
return matchRepository.findTop3TeamMatchesAfterNow(teamId, LocalDateTime.now())
.stream()
.map(match -> MatchResponse.from(match, teamId))
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,19 @@ void getMainBannerMatches_Success() throws Exception {
matchRepository.saveAll(Arrays.asList(pastMatch, futureMatch1, futureMatch2));

// when
ResultActions result = mockMvc.perform(get("/api/matches/main")
.accept(MediaType.APPLICATION_JSON));
MvcResult mvcResult = mockMvc.perform(get("/api/matches/main")
.accept(MediaType.APPLICATION_JSON))
.andReturn();

// then
result.andExpect(status().isOk())
mockMvc.perform(get("/api/matches/main")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("SUCCESS"))
.andExpect(jsonPath("$.data").isArray())
.andExpect(jsonPath("$.data.length()").value(2))
.andExpect(jsonPath("$.data[0].homeTeam.id").value(TeamInfo.SSG.id))
.andExpect(jsonPath("$.data[1].homeTeam.id").value(TeamInfo.LG.id));
.andExpect(jsonPath("$.data[0].homeTeam.id").value(TeamInfo.LG.id))
.andExpect(jsonPath("$.data[1].homeTeam.id").value(TeamInfo.SSG.id));
}

@Test
Expand All @@ -126,8 +129,8 @@ void getTeamMatches_Success() throws Exception {
.andExpect(jsonPath("$.status").value("SUCCESS"))
.andExpect(jsonPath("$.data").isArray())
.andExpect(jsonPath("$.data.length()").value(2))
.andExpect(jsonPath("$.data[0].awayTeam.id").value(TeamInfo.NC.id))
.andExpect(jsonPath("$.data[1].awayTeam.id").value(TeamInfo.KT.id));
.andExpect(jsonPath("$.data[0].awayTeam.id").value(TeamInfo.KT.id))
.andExpect(jsonPath("$.data[1].awayTeam.id").value(TeamInfo.NC.id));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -42,15 +43,16 @@ class MatchServiceTest {
@DisplayName("메인 배너에 노출할 상위 5개 경기를 조회")
void getMainBannerMatches_ShouldReturnTop5Matches() {
// Given
LocalDateTime now = LocalDateTime.now();
List<Match> matches = createTestMatches();
when(matchRepository.findTop5ByOrderByMatchTimeDesc()).thenReturn(matches);
when(matchRepository.findMainBannerMatches(any(LocalDateTime.class))).thenReturn(matches);

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

// Then
assertThat(result).hasSize(5);
verify(matchRepository).findTop5ByOrderByMatchTimeDesc();
verify(matchRepository).findMainBannerMatches(any(LocalDateTime.class));
}

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

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

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

@Test
Expand Down

0 comments on commit 09ffe8d

Please sign in to comment.