-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[team-13 새리] API 구현 #27
base: team-13
Are you sure you want to change the base?
Changes from 8 commits
8adfdc9
d4c91f0
a920746
c0115da
c1b005b
aa140fb
4693684
822941c
3ab1447
cf8970d
05e8a64
c850d7b
d7f96f7
e6e88ce
d095894
0372671
0826a95
ed9dbf4
d3d8b92
0c44218
a284220
c9905f2
c9117a6
5a12ff9
217dec1
6ac4369
2c1f637
d2c2ccb
49298c8
4078a93
60b4438
4a804f0
b6eaaf1
42fcdb8
3cb87c4
0d92b45
74e8791
5de440a
b33453e
ce6730b
dee24bc
00bdd64
e627055
a49744b
0a801a8
d2f4f7f
3857555
a7e87fc
c3234a2
926d844
0481ce5
660c20d
7968604
aeb66b7
bd8aeac
5d8f748
1ac8f0a
40ad146
4c41e8a
9349f55
f6a28e0
a0448dc
a1e1620
fb3478d
dad5131
d8af350
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
package com.codesquad.baseball.DTO; | ||
|
||
import org.springframework.data.relational.core.mapping.Column; | ||
import com.codesquad.baseball.domain.Team; | ||
|
||
public class GameListDTO { | ||
|
||
@Column("gameId") | ||
private Long gameId; | ||
|
||
@Column("homeTeamName") | ||
private String homeTeamName; | ||
|
||
@Column("homeTeamId") | ||
private Long homeTeamId; | ||
|
||
@Column("awayTeamName") | ||
private String awayTeamName; | ||
|
||
@Column("awayTeamId") | ||
private Long awayTeamId; | ||
|
||
public static GameListDTO of(Long gameId, Team homeTeam, Team awayTeam) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 좋네요. |
||
return new GameListDTO( | ||
gameId, | ||
homeTeam.getName(), | ||
homeTeam.getId(), | ||
awayTeam.getName(), | ||
awayTeam.getId() | ||
); | ||
} | ||
|
||
public GameListDTO(Long gameId, String homeTeamName, Long homeTeamId, String awayTeamName, Long awayTeamId) { | ||
this.gameId = gameId; | ||
this.homeTeamName = homeTeamName; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
package com.codesquad.baseball.controller; | ||
|
||
import com.codesquad.baseball.DTO.*; | ||
import com.codesquad.baseball.DTO.GameListDTO; | ||
import com.codesquad.baseball.DTO.record.TeamRecordDTO; | ||
import com.codesquad.baseball.DTO.record.request.PlayerRecordRequest; | ||
import com.codesquad.baseball.DTO.score.GameScoreDTO; | ||
import com.codesquad.baseball.DTO.score.TeamScoreDTO; | ||
import com.codesquad.baseball.domain.Game; | ||
import com.codesquad.baseball.domain.Score; | ||
import com.codesquad.baseball.domain.Team; | ||
import com.codesquad.baseball.service.GameService; | ||
import com.codesquad.baseball.service.TeamService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
|
@@ -20,48 +16,24 @@ | |
public class GameController { | ||
|
||
private final GameService gameService; | ||
private final TeamService teamService; | ||
|
||
public GameController(GameService gameService, TeamService teamService) { | ||
public GameController(GameService gameService) { | ||
this.gameService = gameService; | ||
this.teamService = teamService; | ||
} | ||
|
||
@GetMapping("/games") | ||
public List<GameListDTO> browseGames() { | ||
return gameService.browseAllGames(); | ||
} | ||
|
||
@GetMapping("/games/{teamId}") | ||
public TeamDTO browseTeamStatus(@PathVariable Long teamId) { | ||
return teamService.browseTeamStatus(teamId); | ||
} | ||
|
||
@GetMapping("/play/{gameId}/players") | ||
@GetMapping("/game/{gameId}/players") | ||
public TeamRecordDTO browseTeamPlayers(@PathVariable Long gameId) { | ||
Game game = gameService.findGameById(gameId); | ||
Team homeTeam = teamService.browseTeamById(game.getHomeTeamId()); | ||
Team awayTeam = teamService.browseTeamById(game.getAwayTeamId()); | ||
return new TeamRecordDTO(homeTeam, awayTeam); | ||
return gameService.browseTeamPlayersByGameId(gameId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 훨씬 깔끔하네요. 이게 컨트롤러가 지향해야 할 모습입니다. |
||
} | ||
|
||
@PostMapping("/play/{teamId}/score") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void addScore(@PathVariable Long teamId, @RequestBody Score score) { | ||
teamService.addScore(teamId, score); | ||
} | ||
|
||
@GetMapping("/play/{gameId}/score") | ||
@GetMapping("/game/{gameId}/score") | ||
public GameScoreDTO browseAllScore(@PathVariable Long gameId) { | ||
Game game = gameService.findGameById(gameId); | ||
TeamScoreDTO homeTeamScoreDTO = teamService.browseTeamScore(game.getHomeTeamId()); | ||
TeamScoreDTO awayTeamScoreDTO = teamService.browseTeamScore(game.getAwayTeamId()); | ||
return GameScoreDTO.of(game, homeTeamScoreDTO, awayTeamScoreDTO); | ||
return gameService.browseGameScoreByGameId(gameId); | ||
} | ||
|
||
@PatchMapping("/play/{teamId}/record") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void updatePlayerRecord(@PathVariable Long teamId, @RequestBody PlayerRecordRequest record) { | ||
teamService.updatePlayerRecord(teamId, record); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.codesquad.baseball.controller; | ||
|
||
import com.codesquad.baseball.DTO.TeamDTO; | ||
import com.codesquad.baseball.DTO.record.request.PlayerRecordRequest; | ||
import com.codesquad.baseball.domain.Score; | ||
import com.codesquad.baseball.service.TeamService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/baseball") | ||
public class TeamController { | ||
|
||
private final TeamService teamService; | ||
|
||
public TeamController(TeamService teamService) { | ||
this.teamService = teamService; | ||
} | ||
|
||
@GetMapping("/games/{teamId}") | ||
public TeamDTO browseTeamStatus(@PathVariable Long teamId) { | ||
return teamService.browseTeamStatus(teamId); | ||
} | ||
|
||
@PostMapping("/play/{teamId}/score") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void addScore(@PathVariable Long teamId, @RequestBody Score score) { | ||
teamService.addScore(teamId, score); | ||
} | ||
|
||
@PatchMapping("/play/{teamId}/record") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void updatePlayerRecord(@PathVariable Long teamId, @RequestBody PlayerRecordRequest record) { | ||
teamService.updatePlayerRecord(teamId, record); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,64 @@ | ||
package com.codesquad.baseball.service; | ||
|
||
import com.codesquad.baseball.DTO.GameListDTO; | ||
import com.codesquad.baseball.DTO.record.TeamRecordDTO; | ||
import com.codesquad.baseball.DTO.score.GameScoreDTO; | ||
import com.codesquad.baseball.DTO.score.TeamScoreDTO; | ||
import com.codesquad.baseball.domain.Game; | ||
import com.codesquad.baseball.domain.Team; | ||
import com.codesquad.baseball.error.exception.GameNotFoundException; | ||
import com.codesquad.baseball.error.exception.TeamNotFoundException; | ||
import com.codesquad.baseball.repository.GameRepository; | ||
import com.codesquad.baseball.repository.TeamRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
public class GameService { | ||
|
||
private final GameRepository gameRepository; | ||
private final TeamRepository teamRepository; | ||
|
||
public GameService(GameRepository gameRepository) { | ||
@Autowired | ||
public GameService(GameRepository gameRepository, TeamRepository teamRepository) { | ||
this.gameRepository = gameRepository; | ||
this.teamRepository = teamRepository; | ||
} | ||
|
||
public Game save(Game game) { | ||
return gameRepository.save(game); | ||
} | ||
|
||
public List<GameListDTO> browseAllGames() { | ||
return gameRepository.browseAllGames(); | ||
Iterable<Game> games = gameRepository.findAll(); | ||
List<GameListDTO> gameList = new ArrayList<>(); | ||
for (Game game : games) { | ||
Long id = game.getId(); | ||
gameList.add(GameListDTO.of(id, findHomeTeamByGameId(id), findAwayTeamByGameId(id))); | ||
} | ||
Comment on lines
+36
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return gameList; | ||
} | ||
|
||
public Game findGameById(Long id) { | ||
return gameRepository.findById(id).orElseThrow(GameNotFoundException::new); | ||
} | ||
|
||
public Team findHomeTeamByGameId(Long id) { | ||
return teamRepository.findById(findGameById(id).getHomeTeamId()).orElseThrow(TeamNotFoundException::new); | ||
} | ||
|
||
public Team findAwayTeamByGameId(Long id) { | ||
return teamRepository.findById(findGameById(id).getAwayTeamId()).orElseThrow(TeamNotFoundException::new); | ||
} | ||
|
||
public TeamRecordDTO browseTeamPlayersByGameId(Long id) { | ||
return new TeamRecordDTO(findAwayTeamByGameId(id), findHomeTeamByGameId(id)); | ||
} | ||
|
||
public GameScoreDTO browseGameScoreByGameId(Long id) { | ||
return GameScoreDTO.of(findGameById(id), TeamScoreDTO.of(findHomeTeamByGameId(id)), TeamScoreDTO.of(findAwayTeamByGameId(id))); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DTO인데
@Column
이 있는 것은 좀 어색한데, DTO인가요? 엔티티인가요?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 이 부분도 질문으로 여쭤보려했는데 깜빡했어요! DTO입니다! GameListDTO를 받아오는 레포지토리 부분을 쿼리로 짜고 컬럼명 다 맞추고 MySQL에서 쿼리가 잘 작동하는 부분까지 확인했습니다. 하지만 json으로는 모두 null로 나왔고
@Column
을 붙이고 해결했지만 관련부분 검색해보아도 나오는 게 없었습니다ㅠㅡㅠThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@min27604 댓글이 좀 늦었네요. 죄송합니다. ㅠㅠ
DTO를 받아오는 이라고 하셨는데 어디서 받아오시는 건가요? 보통의 설계로는, 레포지토리를 통해 DB row <-> Entity class 간의 정보 교환을 하고, entity <-> DTO는 다시 한 번 정보 전파 및 가공이 이뤄지는 부분인데요. 혹시 한 단계를 빼먹은 건 아니려나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wheejuni 앗 네 맞아요 DB에서 가져올 때 game의 정보와 team의 정보가 조인으로 섞여있어 바로 DTO로 가져오게 했습니다.. 이렇게 하면 안 되는 거였군요ㅠㅡㅠ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@min27604 네 맞습니다. 서로 절대 존재를 알아차리지 못하게 해주세요. (DTO - 엔티티 간)
물론 작업하다보면 변환하는 로직이 필요해서 완전히 그렇게 고립시키기는 어렵지만, 최대한 노력해 보시는게 좋아요.