Skip to content

Commit

Permalink
[스티치] 체스 스프링 실습 3단계 미션 제출입니다 (#122)
Browse files Browse the repository at this point in the history
* feat : Spring Data JDBC 적용

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

* feat : GameHistory, GameRoom Entity 구현
  • Loading branch information
lxxjn0 authored May 3, 2020
1 parent 4834662 commit 9275ae7
Show file tree
Hide file tree
Showing 31 changed files with 311 additions and 1,269 deletions.
36 changes: 0 additions & 36 deletions src/main/java/wooteco/chess/SparkChessApplication.java

This file was deleted.

86 changes: 0 additions & 86 deletions src/main/java/wooteco/chess/controller/SparkChessController.java

This file was deleted.

50 changes: 35 additions & 15 deletions src/main/java/wooteco/chess/controller/SpringChessController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package wooteco.chess.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -21,36 +25,52 @@ public SpringChessController(final ChessService chessService) {
}

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

@GetMapping("/chess")
public String loadChessGame(final Model model) {
final ChessGameDto chessGameDto = chessService.loadChessGame();
@PostMapping("/game/new")
public String createGame(@RequestParam("name") final String name, final Model model, HttpServletResponse response) {
ChessGameDto chessGameDto = chessService.createChessGame(name);
Cookie cookie = new Cookie("gameId", String.valueOf(chessGameDto.getId()));

cookie.setPath("/");
response.addCookie(cookie);
return renderGame(chessGameDto, model);
}

@PostMapping("/chess_play")
public String playChessGame(@RequestParam final String sourcePosition, @RequestParam final String targetPosition,
final Model model) {
final ChessGameDto chessGameDto = chessService.playChessGame(sourcePosition.trim(), targetPosition.trim());
@PostMapping("/game")
public String showGame(@RequestParam("name") final String name, final Model model, HttpServletResponse response) {
ChessGameDto chessGameDto = chessService.loadChessGameByName(name);
Long gameId = chessGameDto.getId();

if (chessGameDto.isEndState()) {
if (chessService.isEndGame(gameId)) {
return renderResult(chessGameDto, model);
}

Cookie cookie = new Cookie("gameId", String.valueOf(gameId));
cookie.setPath("/");
response.addCookie(cookie);
return renderGame(chessGameDto, model);
}

@PostMapping("/chess_new")
public String newChessGame(final Model model) {
return renderGame(chessService.createChessGame(), model);
@PostMapping("/game/play")
public String playChessGame(@CookieValue("gameId") Cookie gameIdCookie, @RequestParam final String sourcePosition,
@RequestParam final String targetPosition, final Model model) {
final Long gameId = Long.parseLong(gameIdCookie.getValue());
final ChessGameDto chessGameDto = chessService.playChessGame(gameId, sourcePosition.trim(),
targetPosition.trim());

if (chessGameDto.isEndState()) {
return renderResult(chessGameDto, model);
}
return renderGame(chessGameDto, model);
}

@PostMapping("/chess_end")
public String endChessGame(final Model model) {
final ChessGameDto chessGameDto = chessService.endChessGame();
@PostMapping("/game/end")
public String endChessGame(@CookieValue("gameId") Long gameId, final Model model) {
final ChessGameDto chessGameDto = chessService.endChessGame(gameId);
return renderResult(chessGameDto, model);
}

Expand Down
15 changes: 0 additions & 15 deletions src/main/java/wooteco/chess/dao/ChessGameDao.java

This file was deleted.

15 changes: 0 additions & 15 deletions src/main/java/wooteco/chess/dao/ChessHistoryDao.java

This file was deleted.

62 changes: 0 additions & 62 deletions src/main/java/wooteco/chess/dao/MySqlChessGameDao.java

This file was deleted.

66 changes: 0 additions & 66 deletions src/main/java/wooteco/chess/dao/MySqlChessHistoryDao.java

This file was deleted.

9 changes: 0 additions & 9 deletions src/main/java/wooteco/chess/database/DataSource.java

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/java/wooteco/chess/database/GameRoomRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package wooteco.chess.database;

import java.util.List;

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;

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);

}
Loading

0 comments on commit 9275ae7

Please sign in to comment.