Skip to content

Commit

Permalink
[비밥] 스프링 체스 3단계 구현 (#130)
Browse files Browse the repository at this point in the history
* [또링] 웹 기반 체스 게임 1단계 구현 (#52)

* feat : 코드 마이그레이션

* docs : README 작성

* refactor : 테이블 구조 변경 

- 단일 게임만 가능하던 구조에서 다중 게임이 가능하도록 수정

* refactor : 예외를 try-catch에서 exception handler로 관리

* refactor : unicode로 변환하는 기능을 service에서 분리

* fix : 적을 잡으면 DB에서 삭제하도록 수정

* refactor : 페이지 렌더링 방식 변경

* refactor : 게임 종료 시 alert를 통해 알림

* fix : í�폰이 대각선으로 움직이는 기능 수정

* refactor : autoBoxing 비용 절감

* refactor : SQLException을 SQLAccessException으로 처리

* refactor : SQLException 발생 시 SQLAccessException에 해당 에러 정보 전달

* refactor : DBConnector에서 에러 발생시 프로그램 종료

* refactor : PieceDao의 save() 반환타입 변경 및 사용하지 않는 메서드 제거

* refactor : 추상적인 메서드 이름을 구체적으로 변경

* docs : 2단계 README 작성

* refactor : Spring Application으로 변경

* refactor : exception handler를 위한 advice 생성

* test : controller 테스트 추가

* test : controller의 save, load 테스트 추가

* [또링] 웹 기반 체스 게임 2단계 구현 (#106)

* docs : 2단계 README 작성

* refactor : Spring Application으로 변경

* refactor : exception handler를 위한 advice 생성

* test : controller 테스트 추가

* test : controller의 save, load 테스트 추가

* docs : 3단계 README 작성

* feat : entity 생성

* feat : repository 생성

* feat : 3단계 Spring Data JDBC 적용

* refactor : 서비스 코드 개선

Co-authored-by: 또링 <jnsorn@gmail.com>
  • Loading branch information
pci2676 and jnsorn authored May 4, 2020
1 parent abbc1b9 commit cbdb1e2
Show file tree
Hide file tree
Showing 32 changed files with 269 additions and 543 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@
* @Controller@RestController를 활용하여 요청을 받아야 한다.
* 웹 UI를 적용할 때 도메인 객체의 변경을 최소화해야한다.
* 스프링 빈임을 나타내는 애너테이션(@Component, @Service 등)을 활용한다.
* 컴포넌트 스캔을 통해 빈 등록하여 사용한다.
* 컴포넌트 스캔을 통해 빈 등록하여 사용한다.

## 3단계
* Spring Data JDBC를 활용하여 기존에 사용하던 DB에 접근하기
* 엔티티 클래스를 만들어 DB 테이블과 맵핑한다.
* Spring Data JDBC에서 제공하는 Repository를 활용하여 DB에 접근한다.
28 changes: 0 additions & 28 deletions src/main/java/wooteco/chess/dao/DBConnector.java

This file was deleted.

87 changes: 0 additions & 87 deletions src/main/java/wooteco/chess/dao/GameDao.java

This file was deleted.

139 changes: 0 additions & 139 deletions src/main/java/wooteco/chess/dao/PieceDao.java

This file was deleted.

13 changes: 0 additions & 13 deletions src/main/java/wooteco/chess/dao/SQLAccessException.java

This file was deleted.

18 changes: 15 additions & 3 deletions src/main/java/wooteco/chess/domain/board/ChessGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import wooteco.chess.domain.command.MoveCommand;
import wooteco.chess.domain.entity.GamePiece;
import wooteco.chess.domain.piece.Piece;
import wooteco.chess.domain.piece.position.InvalidPositionException;
import wooteco.chess.domain.piece.position.Position;
Expand Down Expand Up @@ -48,6 +50,17 @@ public boolean isKingAlive() {
return board.isKingAlive();
}

public Piece findPieceByPosition(Position position) {
return board.findPiece(position)
.orElseThrow(IllegalArgumentException::new);
}

public List<GamePiece> toPieceEntity() {
return getPieces().stream()
.map(Piece::toEntity)
.collect(Collectors.toList());
}

public Board getBoard() {
return board;
}
Expand All @@ -71,8 +84,7 @@ public Team getTurn() {
return turn;
}

public Piece findPieceByPosition(Position position) {
return board.findPiece(position)
.orElseThrow(IllegalArgumentException::new);
public String getTurnName(){
return turn.getName();
}
}
4 changes: 4 additions & 0 deletions src/main/java/wooteco/chess/domain/command/MoveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public Position getSourcePosition() {
public Position getTargetPosition() {
return positions.get(TARGET_POSITION);
}

public String getTargetPoistionName(){
return getTargetPosition().getName();
}
}
64 changes: 64 additions & 0 deletions src/main/java/wooteco/chess/domain/entity/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package wooteco.chess.domain.entity;

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

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.MappedCollection;
import org.springframework.data.relational.core.mapping.Table;

import wooteco.chess.domain.board.ChessGame;
import wooteco.chess.domain.command.MoveCommand;
import wooteco.chess.domain.piece.Piece;
import wooteco.chess.util.PieceConverter;

@Table("GAME")
public class Game {
@Id
private Long id;
private String turn;
@MappedCollection(idColumn = "game_id", keyColumn = "id")
private List<GamePiece> gamePieces;

protected Game() {
}

public Game(String turn, List<GamePiece> gamePieces) {
this.turn = turn;
this.gamePieces = gamePieces;
}

public Game(Long id, String turn, List<GamePiece> gamePiece) {
this(turn, gamePiece);
this.id = id;
}

public Game(ChessGame chessGame) {
this(chessGame.getTurnName(), chessGame.toPieceEntity());
}

public void move(MoveCommand moveCommand) {
ChessGame chessGame = getChessGame();
chessGame.move(moveCommand);
this.turn = chessGame.getTurnName();
this.gamePieces = chessGame.toPieceEntity();
}

private List<Piece> toPieces() {
return this.gamePieces.stream()
.map(piece -> PieceConverter.of(piece.getSymbol(), piece.getPosition()))
.collect(Collectors.toList());
}

public ChessGame getChessGame() {
return new ChessGame(toPieces(), turn);
}

public Long getId() {
return id;
}

public String getTurn() {
return turn;
}
}
Loading

0 comments on commit cbdb1e2

Please sign in to comment.