Skip to content

Commit

Permalink
[무늬] 체스 스프링 4단계 미션 제출합니다. (#186)
Browse files Browse the repository at this point in the history
* [스티치] 체스 스프링 실습 1단계 미션 제출입니다 (#37)

* feat : 기존 Chess 코드 추가

* refactor : 기존 Chess 코드의 Application과 Controller 명명 수정

* refactor : JdbcTemplate 명명 수정 및 Spring Bean 등록

- 기존 JdbcTemplate 라이브러리와 충돌로 인한 명명 수정
- Dao, Service, ConnectionManager를 Spring Bean 등록

* feat : SpringChessController 추가

* feat : SpringChessController 구현

* refactor : controller 피드백 리팩토링

* refactor : database package 리팩토링

- ConnectionManager를 DataSource로 명명 수정.
- CustomJdbcTemplate을 JdbcTemplate으로 명명 수정, 별칭 지정.
- MySqlConnectionManager를 MySqlDataSource로 명명 수정, 별칭 지정.

* refactor : 접근 지정자 수정 및 상수 추출

* refactor : BlackPawnStrategy와 WhitePawnStrategy를 PawnStrategy로 추상화

* [스티치] 체스 스프링 실습 2단계 미션 제출입니다 (#87)

* feat : Spring Data JDBC 적용

- Spring Data JDBC 적용을 위한 GameRoomRepository 구현
- schema.sql 구현

* feat : GameHistory, GameRoom Entity 구현

* refactor : 게임방 생성과 새로운 방을 생성할 수 있도록 index.hbs 수정

* refactor : 종료된 게임 표시 및 왕이 잡히면 게임이 종료 되도록 수정

* refactor : ConsoleApplication 관련 패키지 및 클래스 삭제

* refactor : Service 흐름에 따라 메서드 순서 변경

* feat : DB에 GameRoom이 존재하지 않는 경우 예외 발생 기능 구현

* feat: MockMvc를 활용한 컨트롤러 테스트

* chore : 불필요한 todo 주석 제거

Co-authored-by: Junyoung Lee <[email protected]>
  • Loading branch information
JinJuMoon and lxxjn0 authored May 8, 2020
1 parent 94c1e84 commit a75c655
Show file tree
Hide file tree
Showing 26 changed files with 431 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SpringChessController(final ChessService chessService) {

@GetMapping("/")
public String index(Model model) {
model.addAttribute("gameNames", chessService.showAllGames());
model.addAttribute("games", chessService.showAllGames());
return "index";
}

Expand Down Expand Up @@ -93,4 +93,5 @@ private String renderResult(final ChessGameDto chessGameDto, final Model model)
model.addAttribute("status", chessStatusDtos.getChessStatusDtos());
return "result";
}

}
8 changes: 5 additions & 3 deletions src/main/java/wooteco/chess/database/GameRoomRepository.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package wooteco.chess.database;

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

import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import wooteco.chess.entity.GameRoom;

import java.util.List;
import wooteco.chess.entity.GameRoom;

public interface GameRoomRepository extends CrudRepository<GameRoom, Long> {

@Override
List<GameRoom> findAll();

@Query("SELECT * FROM game_room WHERE name = :name")
GameRoom findByName(@Param("name") final String name);
Optional<GameRoom> findByName(@Param("name") final String name);

}
58 changes: 29 additions & 29 deletions src/main/java/wooteco/chess/service/ChessService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package wooteco.chess.service;

import static java.util.stream.Collectors.*;

import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;

import org.springframework.stereotype.Service;

import wooteco.chess.database.GameRoomRepository;
import wooteco.chess.domain.chessBoard.ChessBoard;
import wooteco.chess.domain.chessBoard.ChessBoardInitializer;
Expand All @@ -9,13 +17,7 @@
import wooteco.chess.entity.GameHistory;
import wooteco.chess.entity.GameRoom;
import wooteco.chess.service.dto.ChessGameDto;

import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;

import static java.util.stream.Collectors.toList;
import wooteco.chess.service.dto.GameRoomDto;

@Service
public class ChessService {
Expand All @@ -28,9 +30,17 @@ public ChessService(final GameRoomRepository gameRoomRepository) {
this.gameRoomRepository = gameRoomRepository;
}

public ChessGameDto loadChessGameByName(String name) {
GameRoom gameRoom = gameRoomRepository.findByName(name);
return ChessGameDto.of(gameRoom.getId(), initChessGameOf(gameRoom));
public List<GameRoomDto> showAllGames() {
List<GameRoom> gameRooms = gameRoomRepository.findAll();

return gameRooms.stream()
.map(GameRoomDto::of)
.collect(toList());
}

public ChessGameDto createChessGame(final String name) {
GameRoom savedGameRoom = gameRoomRepository.save(new GameRoom(name));
return ChessGameDto.of(savedGameRoom.getId(), initChessGameOf(savedGameRoom));
}

private ChessGame initChessGameOf(final GameRoom gameRoom) {
Expand All @@ -44,39 +54,37 @@ private ChessGame initChessGameOf(final GameRoom gameRoom) {
return chessGame;
}

public ChessGameDto loadChessGameByName(String name) {
GameRoom gameRoom = gameRoomRepository.findByName(name)
.orElseThrow(() -> new NoSuchElementException("해당 이름을 가진 게임방이 존재하지 않습니다."));
return ChessGameDto.of(gameRoom.getId(), initChessGameOf(gameRoom));
}

public ChessGameDto playChessGame(final Long gameId, final String sourcePosition, final String targetPosition) {
Objects.requireNonNull(sourcePosition, "소스 위치가 null입니다.");
Objects.requireNonNull(targetPosition, "타겟 위치가 null입니다.");
return moveChessPiece(gameId, sourcePosition, targetPosition);
}

private ChessGameDto moveChessPiece(final Long gameId, final String sourcePosition, final String targetPosition) {
GameRoom gameRoom = gameRoomRepository.findById(gameId)
final GameRoom gameRoom = gameRoomRepository.findById(gameId)
.orElseThrow(() -> new NoSuchElementException("게임이 존재하지 않습니다."));

final ChessGame chessGame = initChessGameOf(gameRoom);
final ChessCommand chessCommand = ChessCommand.of(Arrays.asList(MOVE_COMMAND, sourcePosition, targetPosition));

chessGame.move(chessCommand);
gameRoom.addGameHistory(new GameHistory(sourcePosition, targetPosition, gameId));

gameRoomRepository.save(gameRoom);

gameRoomRepository.save(new GameRoom(gameRoom, chessGame.isEndState()));
return ChessGameDto.of(gameRoom.getId(), chessGame);
}

public ChessGameDto createChessGame(final String name) {
GameRoom savedGameRoom = gameRoomRepository.save(new GameRoom(name));
return ChessGameDto.of(savedGameRoom.getId(), initChessGameOf(savedGameRoom));
}

public ChessGameDto endChessGame(final Long gameId) {
GameRoom gameRoom = gameRoomRepository.findById(gameId)
.orElseThrow(() -> new NoSuchElementException("게임이 존재하지 않습니다."));
final ChessGame chessGame = initChessGameOf(gameRoom);

chessGame.end();
gameRoomRepository.save(new GameRoom(gameRoom, true));
gameRoomRepository.save(new GameRoom(gameRoom, chessGame.isEndState()));
return ChessGameDto.of(gameRoom.getId(), chessGame);
}

Expand All @@ -86,12 +94,4 @@ public boolean isEndGame(final Long gameId) {
return gameRoom.getState();
}

public List<String> showAllGames() {
List<GameRoom> gameRooms = gameRoomRepository.findAll();

return gameRooms.stream()
.map(GameRoom::getName)
.collect(toList());
}

}
22 changes: 11 additions & 11 deletions src/main/java/wooteco/chess/service/dto/ChessGameDto.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package wooteco.chess.service.dto;

import wooteco.chess.domain.chessGame.ChessGame;

import java.util.Objects;

import wooteco.chess.domain.chessGame.ChessGame;

public class ChessGameDto {

private final Long id;
Expand All @@ -23,17 +23,17 @@ private ChessGameDto(final Long id, final ChessBoardDto chessBoardDto, final Pie
this.isKingCaught = isKingCaught;
}

public static ChessGameDto of(final Long id, final ChessGame chessGame) {
Objects.requireNonNull(chessGame, "체스 게임이 null입니다.");
public static ChessGameDto of(final Long id, final ChessGame chessGame) {
Objects.requireNonNull(chessGame, "체스 게임이 null입니다.");

final ChessBoardDto chessBoardDto = ChessBoardDto.of(chessGame.getChessBoard());
final PieceColorDto pieceColorDto = PieceColorDto.of(chessGame.getCurrentPieceColor());
final ChessStatusDtos chessStatusDtos = ChessStatusDtos.of(chessGame.getChessGameStatus());
final boolean isEndStatus = chessGame.isEndState();
final boolean isKingCaught = chessGame.isKingCaught();
final ChessBoardDto chessBoardDto = ChessBoardDto.of(chessGame.getChessBoard());
final PieceColorDto pieceColorDto = PieceColorDto.of(chessGame.getCurrentPieceColor());
final ChessStatusDtos chessStatusDtos = ChessStatusDtos.of(chessGame.getChessGameStatus());
final boolean isEndStatus = chessGame.isEndState();
final boolean isKingCaught = chessGame.isKingCaught();

return new ChessGameDto(id, chessBoardDto, pieceColorDto, chessStatusDtos, isEndStatus, isKingCaught);
}
return new ChessGameDto(id, chessBoardDto, pieceColorDto, chessStatusDtos, isEndStatus, isKingCaught);
}

public Long getId() {
return id;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/wooteco/chess/service/dto/GameRoomDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package wooteco.chess.service.dto;

import wooteco.chess.entity.GameRoom;

import java.util.Objects;

public class GameRoomDto {

private Long id;
private String name;
private Boolean state;

private GameRoomDto(final Long id, final String name, final Boolean state) {
this.id = id;
this.name = name;
this.state = state;
}

public static GameRoomDto of(final GameRoom gameRoom) {
Objects.requireNonNull(gameRoom, "게임이 null입니다.");
return new GameRoomDto(gameRoom.getId(), gameRoom.getName(), gameRoom.getState());
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Boolean getState() {
return state;
}

}
2 changes: 1 addition & 1 deletion src/main/java/wooteco/chess/web/PieceNameConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum PieceNameConverter {
BLACK_PAWN("P", "black-pawn"),
WHITE_PAWN("p", "white-pawn");

private static final String IMAGE_SOURCE_FORMAT = "<img class=\"chessboard\" src=\"./images/%s.png\">";
private static final String IMAGE_SOURCE_FORMAT = "<img class=\"chessboard\" src=\"/images/%s.png\">";

private final String chessPieceName;
private final String imageFileName;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/black-king.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/black-knight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/black-pawn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/black-queen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/black-rook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/white-bishop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/white-king.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/white-knight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/white-pawn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/white-queen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/static/images/white-rook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a75c655

Please sign in to comment.