Skip to content

Commit

Permalink
[#8] feature : 게임 목록 보여주는 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
choitree committed May 6, 2021
1 parent 6b6f038 commit de2e205
Showing 4 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.baseball.controller;

import com.example.baseball.dto.MatchedTeamDTO;
import com.example.baseball.service.GameListService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class GameListController {
private final GameListService gameListService;

public GameListController (GameListService gameListService) {
this.gameListService = gameListService;
}

@GetMapping("/matchTeam/{id}")
public MatchedTeamDTO findGame(@PathVariable Long id) {
return gameListService.findGame(id);
}

@GetMapping("/helloWorld")
public List<MatchedTeamDTO> findGameList() {
List<MatchedTeamDTO> games = gameListService.findGameList();
return games;
}

}
37 changes: 37 additions & 0 deletions BE/src/main/java/com/example/baseball/dto/MatchedTeamDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.baseball.dto;

import com.example.baseball.entity.Game;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.Id;

public class MatchedTeamDTO {

This comment has been minimized.

Copy link
@kyupid

kyupid May 6, 2021

Owner

MatchedTeam이 서로 맞붙는 팀이라는 의미에서 작성하신건가요?
아니면 서로 매칭되는 팀이라는 의미일까요?

@Id
@JsonIgnore
private Long id;

private String homeTeam;
private String awayTeam;

This comment has been minimized.

Copy link
@kyupid

kyupid May 6, 2021

Owner

MatchedTeam은 homeTeam, awayTeam 팀 이름만 있는것인가요?
이 클래스 사용목적을 서로 공유해야할것 같습니다!


public MatchedTeamDTO(Long id, String homeTeam, String awayTeam) {
this.id = id;
this.homeTeam = homeTeam;
this.awayTeam = awayTeam;
}

public Long getId() {
return id;
}

public String getHomeTeam() {
return homeTeam;
}

public String getAwayTeam() {
return awayTeam;
}

public static MatchedTeamDTO of(Game game) {
return new MatchedTeamDTO(game.getId(), game.getHomeTeam(), game.getAwayTeam());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.baseball.repository;

import com.example.baseball.entity.Game;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Optional;

public interface GameRepository extends CrudRepository<Game, Long> {
@Query("SELECT `GAME`.`ID` AS `ID`,`GAME`.`HOME_TEAM` AS `HOME_TEAM`, `GAME`.`AWAY_TEAM` AS `AWAY_TEAM` FROM `GAME` ")
@Override
List<Game> findAll();

This comment has been minimized.

Copy link
@kyupid

kyupid May 6, 2021

Owner

11번라인부터 13번라인이요.
초기화면에서 게임리스트 불러올때 findAll()을 쓰기 때문에 필요한 속성만 가지고 오려고
쿼리문에서 home,away,homeScore,awayScore를 가지고온거죠??

이렇해도 될것같긴한데, 조금 애매하다고 느낀게
DTO를 트리가 제안해주신대로 저희가 기능별로 분리해서 사용하려고 있잖아요?
그러면 DTO에서 꺼내오는 식으로 findAll()을 오버라이드해야하지 않나 싶은데,
트리는 어때요?

This comment has been minimized.

Copy link
@kyupid

kyupid May 6, 2021

Owner

적고보니까 생각이 바뀌었는데,
디비랑 직접 상호작용하는건 Entity가 해야하는게 맞다고 생각합니다
그래서 쿼리를 이렇게 넣어주는게 맞는거 같아요.
일단 제가 쓴건 지우지 않고 남겨두겠습니다.


@Query("SELECT `GAME`.`ID` AS `ID`, `GAME`.`HOME_TEAM` AS `HOME_TEAM`, `GAME`.`AWAY_TEAM` AS `AWAY_TEAM` FROM `GAME` WHERE `GAME`.`ID` = :id")
Optional<Game> findById(Long id);
}
33 changes: 33 additions & 0 deletions BE/src/main/java/com/example/baseball/service/GameListService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.baseball.service;

import com.example.baseball.entity.Game;
import com.example.baseball.dto.MatchedTeamDTO;
import com.example.baseball.repository.GameRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GameListService {

private final GameRepository gameRepository;

private GameListService(GameRepository gameRepository) {
this.gameRepository = gameRepository;
}

public MatchedTeamDTO findGame(Long id) {
Game game = gameRepository.findById(id).orElseThrow(IllegalArgumentException::new);
System.out.println(game.getId());
return MatchedTeamDTO.of(game);
}

public List<MatchedTeamDTO> findGameList() {

This comment has been minimized.

Copy link
@kyupid

kyupid May 6, 2021

Owner

제가 이전에 받은 리뷰에서 stream.map 사용해라고 했었는데
나중에 이거 어떻게 사용하는건지 좀 이해하는데 도와주시면 감사하겠습니다 ㅎㅎ

return gameRepository.findAll()
.stream()
.map(game -> findGame(game.getId()))
.collect(Collectors.toList());
}

}

0 comments on commit de2e205

Please sign in to comment.