Skip to content

Commit

Permalink
refactor: 불필요 메서드 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
brorae committed May 4, 2022
1 parent 976edbe commit 2bbcf46
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/main/java/chess/dao/BoardDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class BoardDaoImpl implements BoardDao {

private JdbcTemplate jdbcTemplate;

private RowMapper<Square> squareRowMapper = (rs, rowNum) ->
private RowMapper<Square> rowMapper = (rs, rowNum) ->
new Square(
rs.getString("position"),
rs.getString("symbol"),
Expand Down Expand Up @@ -41,7 +41,7 @@ public void save(List<Square> squares) {
@Override
public List<Square> findById(int id) {
String sql = "select position, symbol, color from board where game_id = ?";
return jdbcTemplate.query(sql, squareRowMapper, id);
return jdbcTemplate.query(sql, rowMapper, id);
}

@Override
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/chess/dao/GameDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package chess.dao;

import chess.domain.state.State;
import chess.entity.Game;
import java.util.List;

Expand All @@ -12,8 +11,6 @@ public interface GameDao {

Game findById(int id);

State findState(int id);

int update(Game game);

int delete(int id);
Expand Down
17 changes: 3 additions & 14 deletions src/main/java/chess/dao/GameDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package chess.dao;

import chess.domain.state.State;
import chess.entity.Game;
import java.util.HashMap;
import java.util.List;
Expand All @@ -15,18 +14,14 @@ public class GameDaoImpl implements GameDao {

private JdbcTemplate jdbcTemplate;

private RowMapper<Game> gameRowMapper = (rs, rowNum) ->
private RowMapper<Game> rowMapper = (rs, rowNum) ->
new Game(
rs.getInt("id"),
rs.getString("title"),
rs.getString("password"),
rs.getString("state")
);

private RowMapper<State> stateRowMapper = (rs, rowNum) ->
State.of(rs.getString("state"));


public GameDaoImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
Expand All @@ -46,19 +41,13 @@ public int save(Game game) {
@Override
public List<Game> findAll() {
String sql = "select * from game";
return jdbcTemplate.query(sql, gameRowMapper);
return jdbcTemplate.query(sql, rowMapper);
}

@Override
public Game findById(int id) {
String sql = "select * from game where id = ?";
return jdbcTemplate.queryForObject(sql, gameRowMapper, id);
}

@Override
public State findState(int id) {
String sql = "select state from game where id = ?";
return jdbcTemplate.queryForObject(sql, stateRowMapper, id);
return jdbcTemplate.queryForObject(sql, rowMapper, id);
}

@Override
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/chess/service/ChessService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import chess.dto.BoardDto;
import chess.dto.GameDto;
import chess.dto.RoomDto;
import chess.entity.Square;
import chess.dto.StatusDto;
import chess.entity.Game;
import chess.entity.Square;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -47,8 +47,8 @@ public List<GameDto> findGame() {
.collect(Collectors.toList());
}

public BoardDto selectBoard(int id) {
return BoardDto.of(boardDao.findById(id));
public BoardDto selectBoard(int gameId) {
return BoardDto.of(boardDao.findById(gameId));
}

public String selectWinner(int gameId) {
Expand All @@ -61,22 +61,25 @@ public String selectWinner(int gameId) {
}

public String selectState(int gameId) {
return gameDao.findState(gameId).getValue();
Game game = gameDao.findById(gameId);
return game.getState();
}

public StatusDto selectStatus(int gameId) {
Game game = gameDao.findById(gameId);
ChessBoard chessBoard = Square.toBoard((boardDao.findById(gameId)));

ChessGame chessGame = new ChessGame(gameDao.findState(gameId), chessBoard);
ChessGame chessGame = new ChessGame(State.of(game.getState()), chessBoard);
Map<Color, Double> scores = chessGame.calculateScore();

return new StatusDto(scores);
}

@Transactional
public void movePiece(int gameId, String from, String to) {
Game game = gameDao.findById(gameId);
ChessBoard chessBoard = Square.toBoard((boardDao.findById(gameId)));
ChessGame chessGame = new ChessGame(gameDao.findState(gameId), chessBoard);
ChessGame chessGame = new ChessGame(State.of(game.getState()), chessBoard);
playChessGame(from, to, chessGame);

gameDao.update(new Game(chessGame.getState().getValue(), gameId));
Expand All @@ -97,7 +100,7 @@ public void endGame(int gameId) {
@Transactional
public void deleteGame(int gameId, String password) {
Game game = gameDao.findById(gameId);
State state = gameDao.findState(gameId);
State state = State.of(game.getState());
validateState(state);
if (game.isSamePassword(password)) {
gameDao.delete(gameId);
Expand Down
7 changes: 0 additions & 7 deletions src/test/java/chess/dao/FakeGameDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ public Game findById(int id) {
return game.get(id);
}

@Override
public State findState(int id) {
Game game = this.game.get(id);
String state = game.getState();
return State.of(state);
}

@Override
public int update(Game game) {
Game oldGame = this.game.get(game.getId());
Expand Down
11 changes: 2 additions & 9 deletions src/test/java/chess/dao/GameDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,12 @@ void findById() {
assertThat(game.getPassword()).isEqualTo("password");
}

@Test
@DisplayName("게임의 상태를 조회할 수 있다.")
void findState() {
String state = gameDao.findState(gameId).getValue();

assertThat(state).isEqualTo("WhiteTurn");
}

@Test
@DisplayName("게임의 상태를 업데이트할 수 있다.")
void update() {
gameDao.update(new Game("BlackTurn", gameId));
String state = gameDao.findState(gameId).getValue();
Game game = gameDao.findById(gameId);
String state = game.getState();

assertThat(state).isEqualTo("BlackTurn");
}
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/chess/service/ChessServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import chess.dto.PieceDto;
import chess.dto.RoomDto;
import chess.dto.StatusDto;
import chess.entity.Game;
import chess.entity.Square;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -124,7 +125,8 @@ void movePiece() {
@DisplayName("게임을 종료시킬 수 있다.")
void endGame() {
chessService.endGame(id);
String state = gameDao.findState(id).getValue();
Game game = gameDao.findById(id);
String state = game.getState();

assertThat(state).isEqualTo("Finish");
}
Expand Down Expand Up @@ -162,7 +164,8 @@ void deleteGame() {
void restartGame() {
chessService.endGame(id);
chessService.restartGame(id);
String state = gameDao.findState(id).getValue();
Game game = gameDao.findById(id);
String state = game.getState();

assertThat(state).isEqualTo("WhiteTurn");
}
Expand Down

0 comments on commit 2bbcf46

Please sign in to comment.