-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Spring 체스 - 3단계] 다니(이다은) 미션 제출합니다. #299
Changes from 12 commits
5f3ad77
b7c61ef
eb7622c
e11a219
1aeb2c0
95c5b86
5a02315
10a6bfc
0fd737e
046c090
0339eb5
d3452e6
0401795
08ce4a1
dde125f
41c447b
cfcb45a
d5431c3
fede792
cd90bec
eece951
13b02f1
d222c24
d1e6aac
5bc1ba5
7db4006
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
package chess.controller; | ||
|
||
import chess.dto.ChessBoardDto; | ||
import chess.dto.request.MoveRequestDto; | ||
import chess.dto.request.TurnChangeRequestDto; | ||
import chess.dto.response.MoveResponseDto; | ||
import chess.service.ChessService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
public class ChessApiController { | ||
|
@@ -19,13 +18,23 @@ public ChessApiController(final ChessService chessService) { | |
this.chessService = chessService; | ||
} | ||
|
||
@PostMapping("/room") | ||
public Long start(@RequestBody final String req) { | ||
return chessService.addRoom(req.split(":")[1].split("\"")[1]); | ||
} | ||
|
||
@GetMapping("/board/{roomId}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. url이 깔끔해졌네요 👍 |
||
public ChessBoardDto chess(@PathVariable final Long roomId) { | ||
return chessService.chessBoardFromDB(roomId); | ||
} | ||
|
||
@PostMapping(value = "/move", produces = MediaType.APPLICATION_JSON_VALUE) | ||
public MoveResponseDto move(@RequestBody MoveRequestDto moveRequestDto) { | ||
public MoveResponseDto move(@RequestBody final MoveRequestDto moveRequestDto) { | ||
return chessService.move(moveRequestDto); | ||
} | ||
|
||
@PostMapping(value = "/turn", produces = MediaType.APPLICATION_JSON_VALUE) | ||
public ResponseEntity<Void> turn(@RequestBody TurnChangeRequestDto turnChangeRequestDto) { | ||
public ResponseEntity<Void> turn(@RequestBody final TurnChangeRequestDto turnChangeRequestDto) { | ||
chessService.changeTurn(turnChangeRequestDto); | ||
return new ResponseEntity<>(HttpStatus.OK); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
package chess.controller; | ||
|
||
import chess.dto.ChessBoardDto; | ||
import chess.dto.StringChessBoardDto; | ||
import chess.dto.response.RoomResponseDto; | ||
import chess.dto.response.ScoreResponseDto; | ||
import chess.service.ChessService; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
|
||
import java.util.List; | ||
|
||
@Controller | ||
public class ChessController { | ||
|
@@ -17,34 +18,18 @@ public ChessController(final ChessService chessService) { | |
this.chessService = chessService; | ||
} | ||
|
||
@GetMapping("/start") | ||
public String start() { | ||
chessService.makeRound(); | ||
return makeNewGame(); | ||
} | ||
|
||
@GetMapping("/reset") | ||
public String reset() { | ||
chessService.resetRound(); | ||
return makeNewGame(); | ||
@GetMapping("/room-list") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 음 제가 사용하는 url pattern 방식은 아래와 같이 api의 경우 앞에 api를 명시해주어 일반적인 컨트롤러와 분리합니다 |
||
public String roomList(final Model model) { | ||
List<RoomResponseDto> rooms = chessService.rooms(); | ||
model.addAttribute("rooms", rooms); | ||
return "room-list"; | ||
} | ||
|
||
@GetMapping("/chess") | ||
public String chess(final Model model) throws JsonProcessingException { | ||
ChessBoardDto chessBoard = chessService.chessBoardFromDB(); | ||
String jsonFormatChessBoard = chessService.jsonFormatChessBoard(chessBoard); | ||
model.addAttribute("jsonFormatChessBoard", jsonFormatChessBoard); | ||
String currentTurn = chessService.currentTurn(); | ||
model.addAttribute("currentTurn", currentTurn); | ||
chessService.updateRound(chessBoard, currentTurn); | ||
|
||
ScoreResponseDto scoreResponseDto = chessService.scoreResponseDto(); | ||
@GetMapping("/chess/{roomId}") | ||
public String chess(@PathVariable final Long roomId, final Model model) { | ||
ScoreResponseDto scoreResponseDto = chessService.scoreResponseDto(roomId); | ||
model.addAttribute("roomId", roomId); | ||
model.addAttribute("score", scoreResponseDto); | ||
return "chess"; | ||
} | ||
|
||
private String makeNewGame() { | ||
chessService.initialize(); | ||
return "redirect:/chess"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,45 @@ | ||
package chess.dao; | ||
|
||
import chess.dao.setting.DBConnection; | ||
import chess.dto.request.MoveRequestDto; | ||
import chess.dto.response.ChessResponseDto; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Repository | ||
public class PieceDao extends DBConnection { | ||
@Autowired | ||
public class PieceDao { | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public PieceDao(DataSource dataSource) { | ||
public PieceDao(final DataSource dataSource) { | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
} | ||
|
||
public void initializePieceStatus(final String pieceName, final String piecePosition) { | ||
String query = "INSERT INTO piece (piece_name, piece_position) VALUE (?, ?)"; | ||
try { | ||
jdbcTemplate.update(query, pieceName, piecePosition); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
public void initializePieceStatus(final String pieceName, final String piecePosition, Long roomId) { | ||
String query = "INSERT INTO piece (piece_name, piece_position, room_id) VALUE (?, ?, ?)"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 시간이 되신다면 batch insert도 학습해보시면 도움되실 것 같아요 |
||
jdbcTemplate.update(query, pieceName, piecePosition, roomId); | ||
} | ||
|
||
public List<ChessResponseDto> showAllPieces() { | ||
List<ChessResponseDto> pieces = new ArrayList<>(); | ||
String query = "SELECT * FROM piece"; | ||
|
||
try { | ||
pieces = jdbcTemplate.query( | ||
query, (rs, rowNum) -> new ChessResponseDto( | ||
rs.getLong("id"), | ||
rs.getString("piece_name"), | ||
rs.getString("piece_position")) | ||
); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
public List<ChessResponseDto> showAllPieces(final Long roomId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DTO, entity의 차이를 짚고 넘어가시면 미션 진행하는데 도움이 되실거에요 ^_^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 키워드 감사합니다~! 개인 블로그에 정리하고 학습 로그에 기록했어요! 🙌 |
||
List<ChessResponseDto> pieces; | ||
String query = "SELECT * FROM piece WHERE room_id = ?"; | ||
pieces = jdbcTemplate.query( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 바로 return 해줘도 좋겠네요~ |
||
query, (rs, rowNum) -> new ChessResponseDto( | ||
rs.getLong("id"), | ||
rs.getString("piece_name"), | ||
rs.getString("piece_position")), roomId | ||
); | ||
return pieces; | ||
} | ||
|
||
public void movePiece(final MoveRequestDto moveRequestDto) { | ||
String query = "UPDATE piece SET piece_position=? WHERE piece_position=?"; | ||
try { | ||
jdbcTemplate.update(query, moveRequestDto.getTarget(), moveRequestDto.getSource()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void removeAllPieces() { | ||
String query = "DELETE FROM piece"; | ||
try { | ||
jdbcTemplate.update(query); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
jdbcTemplate.update(query, moveRequestDto.getTarget(), moveRequestDto.getSource()); | ||
} | ||
|
||
public void removePiece(final MoveRequestDto moveRequestDto) { | ||
String query = "DELETE FROM piece WHERE piece_position=?"; | ||
try { | ||
jdbcTemplate.update(query, moveRequestDto.getTarget()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
jdbcTemplate.update(query, moveRequestDto.getTarget()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package chess.dao; | ||
|
||
import chess.dto.request.TurnChangeRequestDto; | ||
import chess.dto.response.RoomResponseDto; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.support.GeneratedKeyHolder; | ||
import org.springframework.jdbc.support.KeyHolder; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.PreparedStatement; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
@Repository | ||
public class RoomDao { | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
public RoomDao(final DataSource dataSource) { | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
} | ||
|
||
public Long addRoom(final String roomName) { | ||
KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
String query = "INSERT INTO room (room_name, current_turn) VALUES (?, ?)"; | ||
jdbcTemplate.update(connection -> { | ||
PreparedStatement ps = connection.prepareStatement(query, new String[]{"room_id"}); | ||
ps.setString(1, roomName); | ||
ps.setString(2, "white"); | ||
return ps; | ||
}, keyHolder); | ||
return Objects.requireNonNull(keyHolder.getKey()).longValue(); | ||
} | ||
|
||
public List<RoomResponseDto> showAllRooms() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 취향차이일 수도 있지만 일반적으론 find, select, update, insert와 같은 키워드를 많이 사용합니다 ^_^ |
||
List<RoomResponseDto> rooms; | ||
String query = "SELECT * FROM room"; | ||
rooms = jdbcTemplate.query( | ||
query, (rs, rowName) -> new RoomResponseDto( | ||
rs.getLong("room_id"), | ||
rs.getString("room_name"), | ||
rs.getString("current_turn")) | ||
); | ||
return rooms; | ||
} | ||
|
||
public String showCurrentTurn(final Long roomId) { | ||
String query = "SELECT current_turn FROM room WHERE room_id=?"; | ||
return jdbcTemplate.queryForObject(query, String.class, roomId); | ||
} | ||
|
||
public void changeTurn(final TurnChangeRequestDto turnChangeRequestDto) { | ||
String query = "UPDATE room SET current_turn=? WHERE room_id=?"; | ||
jdbcTemplate.update(query, turnChangeRequestDto.getNextTurn(), turnChangeRequestDto.getRoomId()); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아래 MoveRequestDto처럼 dto를 만들어서 받는 방식이 더 괜찮아보이네요ㅎㅎ
내려줄 때도 마찬가지로 dto를 만들어서 내려주면 클라이언트에서 받기 더 용이할 것 같아요~