Skip to content
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

[빙봉] 체스 스프링 3단계 미션 제출합니다. #142

Merged
merged 12 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions src/main/java/wooteco/chess/SparkChessApplication.java

This file was deleted.

1 change: 0 additions & 1 deletion src/main/java/wooteco/chess/SpringChessApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@SpringBootApplication
public class SpringChessApplication {

public static void main(String[] args) {
SpringApplication.run(SpringChessApplication.class, args);
}
Expand Down
67 changes: 33 additions & 34 deletions src/main/java/wooteco/chess/controller/SpringGameController.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package wooteco.chess.controller;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import wooteco.chess.domain.Color;
import wooteco.chess.dto.MoveResponseDTO;
import wooteco.chess.dto.GameStatusDTO;
import wooteco.chess.dto.MoveRequestDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import wooteco.chess.dto.GameRequestDto;
import wooteco.chess.dto.GameResponseDto;
import wooteco.chess.dto.GameStatusDto;
import wooteco.chess.dto.MoveRequestDto;
import wooteco.chess.service.SpringGameService;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/game")
public class SpringGameController {
Expand All @@ -24,37 +25,35 @@ public SpringGameController(final SpringGameService gameService) {
this.gameService = gameService;
}

@GetMapping("/init")
public MoveResponseDTO init(@RequestParam Integer roomId) throws SQLException {
return gameService.initialize(roomId);
@PostMapping("/init")
public GameResponseDto init(@RequestBody GameRequestDto gameRequestDto) {
return gameService.initialize(gameRequestDto);
}

@PostMapping("/move")
public ResponseEntity<MoveResponseDTO> move(@RequestBody MoveRequestDTO requestDTO) throws SQLException {
try {
return ResponseEntity.status(HttpStatus.OK).body(gameService.move(requestDTO));
} catch (IllegalArgumentException e) {
MoveResponseDTO moveResponseDTO = gameService.createMoveResponseDTO(requestDTO.getRoomId());
moveResponseDTO.setErrorMessage(e.getMessage());
return ResponseEntity.status(HttpStatus.OK).body(moveResponseDTO);
}
@PostMapping("/status")
public GameStatusDto showStatus(@RequestBody GameRequestDto gameRequestDto) {
return new GameStatusDto(gameService.getScore(gameRequestDto), gameService.getScore(gameRequestDto));
}

@GetMapping("/status")
public GameStatusDTO showStatus(@RequestParam Integer roomId) throws SQLException {
return new GameStatusDTO(gameService.getScore(roomId, Color.WHITE), gameService.getScore(roomId, Color.BLACK));
@PostMapping("/path")
public List<String> getMovablePositions(@RequestBody MoveRequestDto moveRequestDto) {
System.out.println(moveRequestDto);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

서버상에서 무엇을 보여주는 건가요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

움직일 수 있는 Position을 가지고 오도록 하는 건데 좀 더 읽기 쉽게 수정해보도록 하겠습니다!

return gameService.getMovablePositions(moveRequestDto);
}

@GetMapping("/load")
public MoveResponseDTO load(@RequestParam Integer roomId) throws SQLException {
return gameService.createMoveResponseDTO(roomId);
@PostMapping("/move")
public ResponseEntity<GameResponseDto> move(@RequestBody MoveRequestDto moveRequestDto) {
try {
return ResponseEntity.status(HttpStatus.OK).body(gameService.move(moveRequestDto));
} catch (IllegalArgumentException e) {
GameResponseDto gameResponseDTO = gameService.findAllPieces(moveRequestDto.getId());
gameResponseDTO.setErrorMessage(e.getMessage());
return ResponseEntity.status(HttpStatus.OK).body(gameResponseDTO);
}
}

@GetMapping("/get")
public List<String> getMovablePositions(final HttpServletRequest request) throws SQLException {
int roomId = Integer.parseInt(request.getParameter("roomId"));
String sourcePosition = request.getParameter("sourcePosition");

return gameService.getMovablePositions(roomId, sourcePosition);
@PostMapping("/load")
public GameResponseDto load(@RequestBody GameRequestDto gameRequestDto) {
return gameService.findAllPieces(gameRequestDto.getId());
}
}
58 changes: 35 additions & 23 deletions src/main/java/wooteco/chess/controller/SpringRoomController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package wooteco.chess.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import wooteco.chess.domain.room.Room;
import wooteco.chess.dto.RoomName;
import wooteco.chess.service.SpringRoomService;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.sql.SQLException;
import java.util.List;
import wooteco.chess.dto.AuthorizeDto;
import wooteco.chess.dto.RoomRequestDto;
import wooteco.chess.dto.RoomResponseDto;
import wooteco.chess.service.SpringRoomService;

@Controller
@RequestMapping("/rooms")
Expand All @@ -24,34 +30,40 @@ private SpringRoomController(final SpringRoomService roomService) {
this.roomService = roomService;
}

// TODO: 2020/04/30 Room 목록을 동기방식이 아닌 비동기 방식으로 수정해야됨
@GetMapping
public String getAllRooms(Model model) throws SQLException {
List<Room> rooms = roomService.findAllRoom();
public String getAllRooms(Model model) {
List<RoomResponseDto> rooms = roomService.findAllRoom();
model.addAttribute("rooms", rooms);

return "index";
}

@GetMapping("/enter")
public String enterRoom(@RequestParam(value = "roomId") Integer roomId, Model model) {
model.addAttribute("roomId", roomId);
@GetMapping("/enter/{id}")
public String enterRoom(@PathVariable Long id, Model model) {
model.addAttribute("id", id);
return "game";
}

// TODO: 2020/04/22 valid 에러페이지 이동 문제
@GetMapping("/create")
public String createRoom(@Valid RoomName roomName, Errors errors, Model model) throws SQLException {
if (errors.hasErrors()) {
model.addAttribute("errors", errors);
@PostMapping("/create")
public String createRoom(@Valid RoomRequestDto roomRequestDto, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("nameError", bindingResult.getFieldError("name"));
model.addAttribute("passwordError", bindingResult.getFieldError("password"));
return getAllRooms(model);
}
roomService.addRoom(roomName.getRoomName());
roomService.addRoom(roomRequestDto);
return "redirect:/rooms";
}

@GetMapping("/remove")
public String removeRoom(@RequestParam(value = "roomId") Integer roomId) throws SQLException {
roomService.removeRoom(roomId);
return "redirect:/rooms";
@DeleteMapping("/remove/{id}")
public void removeRoom(@PathVariable Long id) {
roomService.removeRoom(id);
}

@PostMapping("/authorize")
@ResponseBody
public boolean authorize(@Valid @RequestBody AuthorizeDto authorizeDto) {
return roomService.authorize(authorizeDto.getId(), authorizeDto.getPassword());
}
}
116 changes: 0 additions & 116 deletions src/main/java/wooteco/chess/dao/GameDAO.java

This file was deleted.

40 changes: 0 additions & 40 deletions src/main/java/wooteco/chess/dao/JdbcTemplate.java

This file was deleted.

8 changes: 0 additions & 8 deletions src/main/java/wooteco/chess/dao/PreparedStatementSetter.java

This file was deleted.

Loading