-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
5f3ad77
refactor(dao): DBConnection 삭제
da-nyee b7c61ef
refactor(dao): @Autowired 삭제
da-nyee eb7622c
refactor(dao): try-catch 삭제
da-nyee e11a219
docs(readme): 요구사항 정리
da-nyee 1aeb2c0
docs(readme): DB DDL 수정
da-nyee 95c5b86
feat(room): 체스방 목록 조회 기능 추가
da-nyee 5a02315
docs(readme): DB DDL 수정
da-nyee 10a6bfc
feat(room): 체스방 생성 기능 추가
da-nyee 0fd737e
feat(room): 체스방 참여 기능 추가
da-nyee 046c090
feat(resources): 홈 화면 버튼 추가
da-nyee 0339eb5
fix(chess): 서버를 재시작했을 때 이전 게임이 진행되지 않던 버그 해결
da-nyee d3452e6
fix(chess): King을 잡아도 게임이 종료되지 않던 버그 해결
da-nyee 0401795
style(dao): 코드 포맷 변경
da-nyee 08ce4a1
refactor(player): 사용하지 않는 메소드 삭제
da-nyee dde125f
refactor(controller): 게임 시작(방 생성) 요청 및 응답 DTO 생성
da-nyee 41c447b
fix(chess): 객체 직렬화 버그 해결
da-nyee cfcb45a
test(api-controller): 테스트 작성
da-nyee d5431c3
refactor(chess): View-Controller 사이 DTO 범위 수정
da-nyee fede792
refactor(chess): Repository-DAO 사이 DTO 범위 수정
da-nyee cd90bec
refactor(chess): Service 내 DTO 삭제
da-nyee eece951
feat(resources): schema.sql 추가
da-nyee 13b02f1
refactor(chess): 메소드명, 변수명, 파라미터명 변경
da-nyee d222c24
refactor(chess): 메소드명 변경
da-nyee d1e6aac
refactor(chess): url-pattern 방식 변경
da-nyee 5bc1ba5
refactor(js): window.location.href 주소 수정
da-nyee 7db4006
refactor(dto): 클래스명 변경
da-nyee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 chess.dao.dto.response.ChessResponseDto; | ||
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, | ||
final 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(); | ||
} | ||
return pieces; | ||
public List<ChessResponseDto> findAllPieces(final Long roomId) { | ||
String query = "SELECT * FROM piece WHERE room_id=?"; | ||
return jdbcTemplate.query( | ||
query, | ||
(rs, rowNum) -> new ChessResponseDto( | ||
rs.getLong("id"), | ||
rs.getString("piece_name"), | ||
rs.getString("piece_position")), | ||
roomId | ||
); | ||
} | ||
|
||
public void movePiece(final MoveRequestDto moveRequestDto) { | ||
public void movePiece(final String source, final String target) { | ||
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, target, source); | ||
} | ||
|
||
public void removePiece(final MoveRequestDto moveRequestDto) { | ||
public void removePiece(final String target) { | ||
String query = "DELETE FROM piece WHERE piece_position=?"; | ||
try { | ||
jdbcTemplate.update(query, moveRequestDto.getTarget()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
jdbcTemplate.update(query, target); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package chess.dao; | ||
|
||
import chess.dao.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> findAllRooms() { | ||
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 findCurrentTurn(final Long roomId) { | ||
String query = "SELECT current_turn FROM room WHERE room_id=?"; | ||
return jdbcTemplate.queryForObject(query, String.class, roomId); | ||
} | ||
|
||
public void changeTurn(final String nextTurn, final Long roomId) { | ||
String query = "UPDATE room SET current_turn=? WHERE room_id=?"; | ||
jdbcTemplate.update(query, nextTurn, roomId); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
컨트롤러에서 서비스 호출이 처음보다 줄긴 했지만 요런 부분도 줄여볼 수 있을 것 같아요
서비스 메서드에 roomId를 넘기면 ScoreResponseDto를 반환해주는 느낌으로요 :)
(왠지 DTO때문에 이렇게 하신 것 같네요ㅎㅎ)