[๐ ์ฌ์ดํด2 - ๋ฏธ์ (๊ธฐ๋ฌผ ํ์ฅ + DB ์ ์ฉ)] ์๋ฒ ๋ฏธ์ ์ ์ถํฉ๋๋ค.#344
[๐ ์ฌ์ดํด2 - ๋ฏธ์
(๊ธฐ๋ฌผ ํ์ฅ + DB ์ ์ฉ)] ์๋ฒ ๋ฏธ์
์ ์ถํฉ๋๋ค.#344Chocoding1 wants to merge 25 commits intowoowacourse:chocoding1from
Conversation
Arachneee
left a comment
There was a problem hiding this comment.
์๋ ํ์ธ์ ์๋ฒ!
์ฌ์ดํด2 ์ ์์ฑํด์ฃผ์ จ๋ค์! ๊ณ ์ํ์ จ์ต๋๋ค!
์ฝ๋ฉํธ ๋จ๊ฒผ์ด์ ํ์ธํด์ฃผ์ธ์!
๊ฐ์ฒด์ ํ๋๋ก id๋ฅผ ๋ ๊ฒ
๊ฐ์ฒด์ ํ๋๋ก id๋ฅผ ๋๋ ๊ฒ์ ๊ฐ์ฒด๋ฅผ ์๋ณํ๊ธฐ ์ํด ํ์ํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
๊ทธ๋ ๋ค๋ฉด id๋ ํด๋น ๊ฐ์ฒด์ ์๋ณ์๋ฅผ ์๋ฏธํ๋ ๊ฒ์ด๊ณ ๋๋ฑ์ฑ๋ id๋ก ๋ณด์ฅํด์ผํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
id ์๋ ๊ฐ์ฒด(Turn) ์์ฑ ๋ฐ ๋ฐํ -> DAO๋ฅผ ์์กดํ๋ ์์ ํด๋์ค์์ ํด๋น ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ DB์ ์ ์ฅ ํ id ํ๋ -> ํ๋ํ id๋ฅผ ๊ฐ์ง๊ณ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ๋ค์ ์์ฑ ๋ฐ ๋ฐํ
id ์์ฑ์ DB์ ์์กดํ๊ธฐ ๋๋ฌธ์ ๋์ค๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐํด์. DB์ ์์กดํ์ง ์๊ณ id๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ด๋ค ๊ฒ์ด ์์๊น์?
|
|
||
| import static janggi.domain.game.GameStatus.IN_PROGRESS; | ||
|
|
||
| public class GameDao { |
There was a problem hiding this comment.
DAO๋ Data Access Object๋ก ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ ๊ฐ์ฒด์
๋๋ค.
ํ
์ด๋ธ๊ณผ ์ง์ ์ํตํ๋ฉฐ SQL ์ฟผ๋ฆฌ๋ฅผ ์คํํ๊ณ JDBC๋ฅผ ์ง์ ๋ค๋ฃฌ๋ค๋ ์ , ๊ฐ์ฒด๋ฅผ ์ง์ ๋ค๋ฃจ์ง ์๊ณ ํน์ ํ๋๋ง ํฌํจํ๋ DTO๋ฅผ ๋ค๋ฃฌ๋ค๋ ์ ์์ DAO๋ผ๋ ์ญํ ์ ์ฃผ์์ต๋๋ค.
| public enum GameResult { | ||
|
|
||
| RUNNING, | ||
| WIN, | ||
| LOSE, | ||
| } |
There was a problem hiding this comment.
RUNNING๋ ๊ฒ์ ๊ฒฐ๊ณผ์ธ๊ฐ์? ๊ฒ์์ ์ํ์ ๊ฒ์์ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌ๋ถํ๋ฉด ์ข์ ๊ฒ ๊ฐ์์. GameStatus๋ผ๋๊ฒ ์ด๋ฏธ ์๋ค์. ๊ทธ๋ผ RUNNING์ ๊ผญ ํ์ํ ๊ฑธ๊น์?
There was a problem hiding this comment.
์ฒ์์ Team ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ GameResult๋ ์ด๊ธฐํ๋ฅผ ํด์ค์ผ ํ๋๋ฐ, WIN์ด๋ LOSE๊ฐ ์๋ '์์ง ์นํจ๊ฐ ์ ํด์ง์ง ์์'์ ํํํ๊ณ ์ RUNNING์ด๋ผ๊ณ ์ด๋ฆ์ ์ง์์ต๋๋ค. ๋ค๋ง RUNNING์ด๋ผ๋ ์ด๋ฆ์ด ์ด๋ฏธ GameStatus์์ ์ฌ์ฉ๋๊ณ ์๊ณ , ์ด๋ฆ์ด ์ค๋ณต๋ ๊ฒฝ์ฐ ํท๊ฐ๋ฆด ์๋ ์์ผ๋ ์์ง ๊ฒฐ๊ณผ๊ฐ ์์์ ๋ํ๋ด๋ NONE์ผ๋ก ์์ ํ์ต๋๋ค.
ํน์ ์ ๊ฐ ์๋ชป ์ ๊ทผํ ๊ฒ์ด๋ฉด ์ถ๊ฐ์ ์ธ ์กฐ์ธ ๋ถํ๋๋ฆฝ๋๋ค!
|
|
||
| public Long save() { | ||
| return gameDao.save(GameDto.forSave()); | ||
| } |
There was a problem hiding this comment.
์๋ฌด๊ฒ๋ ์์ด save๋ฅผ ํ๋ ๊ฑด๊ฐ์?? ์๋๋ฉด ์ฒ์ ๊ฒ์์ ์ธํ ํ๋ ๊ฒ์ผ๊น์? init์ด๋ start๊ฐ์ ๋ฉ์๋๋ช ์ด ๋ ์ด์ธ๋ฆฌ๋ ๊ฒ ๊ฐ์์.
GameDto.forSave()๋ init์ด๋ start๊ฐ ๋ ์ด์ธ๋ฆฌ๋ ๊ฒ ๊ฐ์์.
There was a problem hiding this comment.
์ด๊ธฐ JanggiGame ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉด์ ์ ์ฅํ๋ GameDto์ด๊ธฐ ๋๋ฌธ์ init์ด๋ผ๋ ๋ฉ์๋๋ช ์ด ๋ ์ ํฉํ ๊ฒ ๊ฐ์ ์์ ํ์ต๋๋ค!
|
|
||
| import java.util.List; | ||
|
|
||
| public class GameService { |
There was a problem hiding this comment.
JanggiGameService์ GameService๋ ์ด๋ค ๋ค๋ฅธ ์ญํ ์ ๊ฐ์ง๊ณ ์๋์?
There was a problem hiding this comment.
JanggiGameService๋ JanggiGame ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ JanggiGame ๊ฐ์ฒด์ ๊ทธ ํ์์ ์๋ Turn, Piece ๊ฐ์ฒด๋ค์ ์์ํํ๋ ๋ก์ง๋ค์ ์ํํฉ๋๋ค.
๋๋ฌธ์ JanggiGameService์์ ๋ชจ๋ Service๋ฅผ ์์กดํ๋๋ก ์ค๊ณํ์ต๋๋ค. ์ฆ ๋ชจ๋ ๊ฐ์ฒด์ DB๋ฅผ ์์กดํ๋ TotalService ์ญํ ์ด๋ผ๊ณ ๋ณด์๋ฉด ๋ฉ๋๋ค.
TurnService์ ๊ฒฝ์ฐ ๋จ์ํ DAO๋ฅผ ํธ์ถํ๋ ๊ฒ์ด ์๋ ๋น์ฆ๋์ค ๋ก์ง์ด ํฌํจ๋์ด ์์ด ํ์ํ ๊ฐ์ฒด๋ผ๊ณ ์๊ฐ์ด ๋ค์์ต๋๋ค.
๊ทธ๋ฌ๋ GameService, PieceService๋ ๋จ์ํ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฐ์ DAO ๊ฐ์ฒด์ ์์ฒญํ๋ ์ญํ ์ด๊ธฐ ๋๋ฌธ์ GameService์ PieceService๋ ๊ตณ์ด Service ๊ณ์ธต์ ๋ง๋ค ํ์๋ ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
๊ทธ๋ฌ๋ JanggiGameService์์ Turn๋ง Service๋ฅผ ์์กดํ๊ณ Game๊ณผ Piece๋ Dao๋ฅผ ์์กดํ๋ค๋ฉด ๊ณ์ธต์ด ํต์ผ๋์ง ์๊ณ ํผ์กํด๋ณด์ฌ Game๊ณผ Piece๋ Service ๊ณ์ธต์ ๋ง๋ค์ด ํต์ผ์์ผฐ์ต๋๋ค.
ํน์ ์ถ๊ฐ์ ์ธ ์กฐ์ธ์ด ์๋ค๋ฉด ๋ง์ํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค!
| if (dxDelta.getDy() < 0) { | ||
| throw new IllegalArgumentException("์ด๋ํ ์ ์๋ ์์น์ ๋๋ค."); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (dxDelta.getDy() > 0) { | ||
| throw new IllegalArgumentException("์ด๋ํ ์ ์๋ ์์น์ ๋๋ค."); | ||
| } |
There was a problem hiding this comment.
Delta์๊ฒ ์ ๋ฐฉํฅ์ธ์ง ์๋ ๋ฐฉํฅ์ธ์ง ๋ฌผ์ด๋ด๋ ์ข์ ๊ฒ ๊ฐ์์.
| public static Piece create(PieceType pieceType, TeamType teamType) { | ||
| return pieceType.create(teamType); | ||
| } | ||
| } |
There was a problem hiding this comment.
๋๋ฌด ๊ฐ๋จํ ๋ก์ง๊ฐ์๋ฐ Factory ๊ฐ์ฒด๊ฐ ๊ผญ ํ์ํ๊ฐ์? ์ ๋ Piece์ ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋๋ก ๊ตฌํํด๋ ์ถฉ๋ถํด๋ณด์ด๋๋ฐ ์ด๋ป๊ฒ ์๊ฐํ์ธ์?
There was a problem hiding this comment.
์ ๋ ํฉํ ๋ฆฌ ํด๋์ค๋ฅผ ์์ฑํ๋ฉด์ ์ฐ์ฐํ ๊ฐ์ด ์์๋๋ฐ, ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ์๊ฐํ์ง ๋ชป ํ๋ค์..
Piece ์์ฑ ์ฑ
์์ Piece์ ๋๋ ๊ฒ์ด ๋ ๊ฐ์ฒด์งํฅ์ ์ด๋ผ๋ ์๊ฐ์ด ๋ญ๋๋ค!
| public abstract class Team { | ||
|
|
||
| private final Map<Position, Piece> pieces; | ||
| private final GameResult gameResult; |
There was a problem hiding this comment.
GameResult๋ฅผ Team์ด ๊ฐ์ง๊ณ ๊ด๋ฆฌํด์ผํ ๊น์? ๊ฒ์ ๊ฒฐ๊ณผ๋ ๋ ํ์ด ๊ฒ์์ ํด์ผ๋ง ๋์ค๋ ๊ฐ์ด๋ผ๊ณ ์๊ฐํ๋๋ฐ ํ์ด ์จ์ ํ ์์ ์ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ง๊ณ ์์ ์ ์์๊น์?
๋ ํ ์ฌ์ด์ ๊ฒ์ ๊ฒฐ๊ณผ์ ๋ํ ์ ํฉ์ฑ์ ๋๊ฐ ๊ด๋ฆฌํด์ผํ ๊น์?
| public static Delta scaleDown(Delta delta) { | ||
| return new Delta(Integer.signum(delta.dx), Integer.signum(delta.dy)); | ||
| } |
There was a problem hiding this comment.
์ด๊ฑด ์ ๊ฐ ์ ๋ฒ์ ๋์ณค๊ตฐ์. static ๋ฉ์๋๊ฐ ์๋๋ผ non-static ๋ฉ์๋๋ก ๋ง๋ค๋ฉด ์ข์ ๊ฒ ๊ฐ์์.
Delta unitDelta = delta.scaleDown()
| public boolean matches(Delta delta) { | ||
| return totalDelta().equals(delta); | ||
| } | ||
|
|
||
| public boolean matchesDirection(int dx, int dy) { | ||
| public boolean matchesDirection(Delta dxDelta) { |
| import janggi.domain.turn.Turn; | ||
| import janggi.domain.team.TeamType; | ||
|
|
||
| public record TurnDto(Long id, Long gameId, TeamType currentTurnTeam) { |
There was a problem hiding this comment.
์ ์ฅ์ ์ํ ๊ฐ์ฒด์ ๋๋ฉ์ธ ๊ฐ์ฒด๋ฅผ ๋ถ๋ฆฌํ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค.
์ฒดํฌ ๋ฆฌ์คํธ
test๋ฅผ ์คํํ์ ๋, ๋ชจ๋ ํ ์คํธ๊ฐ ์ ์์ ์ผ๋ก ํต๊ณผํ๋์?์ด๋ค ๋ถ๋ถ์ ์ง์คํ์ฌ ๋ฆฌ๋ทฐํด์ผ ํ ๊น์?
์๋ ํ์ธ์ ๋ฐฑํธ~
์ฌ์ดํด2๋ ์ ๋ถํ๋๋ฆฝ๋๋ค!
๊ฐ์ฒด์ ํ๋๋ก id๋ฅผ ๋ ๊ฒ
์ฒ์์ dao๋ฅผ ๋์ ํ๋ฉด์ ์ ์ฅ ๊ฐ์ฒด๋ก DTO๋ฅผ ๋ง๋ค์์ต๋๋ค.
DTO๋ ๋จ์ํ ์ ์ฅ์ฉ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ๋น์ฆ๋์ค ๋ก์ง์ ๋ ์ด์ ๊ฐ ์์์ต๋๋ค.
๊ทธ๋ฌ๋ ๋น์ฆ๋์ค ๋ก์ง์ด ์๋ ์ง์ง ๊ฐ์ฒด๋ค์ด ํ๋ก๊ทธ๋จ์ ๊ตฌ์ฑํ๋๋ฐ, ๊ทธ ๊ณผ์ ์์ DTO์ ํ๋๋ก ์๋ id(key)๊ฐ ํ์ํ์ต๋๋ค.
๊ทธ๋์ ์ด์ฉ ์ ์์ด ์ง์ง ๊ฐ์ฒด๋ค์๊ฒ๋ id๋ฅผ ํ๋๋ก ์ฃผ์๋๋ฐ, ๊ทธ๋ฌ๋ค ๋ณด๋ db์ ์ ์ฅํ๊ธฐ ์ ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ฒฝ์ฐ id๋ฅผ null๋ก ์ค์ ํ์ฌ ์์ฑํด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ์๊ฒผ์ต๋๋ค.
๊ทธ๋์
ํ๋ ์์๋ก ์ฝ๋๋ฅผ ์งฐ์ต๋๋ค.
ํจ์จ์ ์ธ ์ฝ๋๋ผ๊ณ ๋ ์๊ฐ์ด ๋ค์ง ์์์ ์ด ๋ถ๋ถ์ ๋ํด์ ํผ๋๋ฐฑํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค:)