-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2단계 - 웹 자동차 경주] 제이미(임정수) 미션 제출합니다. (#183)
* refactor: 반환 타입 수정 ResponseEntity -> 객체 * fix: 테이블 생성 충돌 문제 해결 테이블이 이미 존재한다면 Drop 하도록 함 * feat: console 게임 코드 추가 * refactor: 비즈니스 로직을 service로 이동 * refactor: 테스트 필요한 bean 범위에 따른 annotation 수정 * docs: 기능 목록 추가 2단계 미션에 대한 기능 목록 추가 * feat: 전체 아이디 불러오기 기능 추가 * feat: 아이디에 따른 우승자 가져오기 기능 추가 * feat: 아이디에 따른 레이싱카 정보 가져오 기능 추가 * feat: 전체 게임 결과 목록 가져오기 기능 추가 * feat: 웹 컨트롤러 핸들러 메서드 매핑 * refactor: 중복 코드 제거 * rename: 패키지 명 수정 및 분리 dao -> repository dto 패키지 domain과 분리 * fix: 서비스 생성자 자동주입 오류 문제 해결 * fix: 랜덤 숫자 생성기 주입 위치 수정 * docs: 기능 목록 완료 체크 * feat: exceptionHandler 추가 * refactor: Mapping 경로 중복 제거 * fix: 잘못된 테스트 빈 생성 문제 해결 * fix: id 초기화가 되지 않는 문제 해결 * feat: Entity 객체 추가 * refactor: dto 클래스명 수정 * refactor: assertion들을 asserTaht으로 묶음 * refactor: 변수 네이밍 수정
- Loading branch information
Showing
22 changed files
with
585 additions
and
148 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package racingcar; | ||
|
||
import racingcar.controller.RacingCarConsoleController; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
RacingCarConsoleController racingCarConsoleController = new RacingCarConsoleController(); | ||
racingCarConsoleController.runGame(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/racingcar/controller/RacingCarConsoleController.java
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,49 @@ | ||
package racingcar.controller; | ||
|
||
import racingcar.domain.Cars; | ||
import racingcar.service.RacingCarService; | ||
import racingcar.util.RandomNumberGenerator; | ||
import racingcar.view.InputView; | ||
import racingcar.view.OutputView; | ||
|
||
public class RacingCarConsoleController { | ||
private final InputView inputView = new InputView(); | ||
private final OutputView outputView = new OutputView(); | ||
private final RacingCarService racingCarService = new RacingCarService(); | ||
private final RandomNumberGenerator randomNumberGenerator = new RandomNumberGenerator(); | ||
|
||
public void runGame() { | ||
Cars cars = initCarData(); | ||
movePerRounds(cars, setTryCount()); | ||
outputView.printWinner(cars); | ||
} | ||
|
||
private Cars initCarData() { | ||
outputView.printCarNameMessage(); | ||
|
||
try { | ||
String carNames = inputView.inputCarNames(); | ||
return racingCarService.getCars(carNames); | ||
} catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
return initCarData(); | ||
} | ||
} | ||
|
||
private int setTryCount() { | ||
outputView.printTryCountMessage(); | ||
|
||
try { | ||
int tryCount = inputView.inputTryCount(); | ||
return racingCarService.getTrialCount(tryCount); | ||
} catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
return setTryCount(); | ||
} | ||
} | ||
|
||
private void movePerRounds(Cars cars, int tryCount) { | ||
outputView.printResultMessage(); | ||
racingCarService.playGame(cars, tryCount, randomNumberGenerator); | ||
} | ||
} |
64 changes: 0 additions & 64 deletions
64
src/main/java/racingcar/controller/RacingCarController.java
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
src/main/java/racingcar/controller/RacingCarWebController.java
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,37 @@ | ||
package racingcar.controller; | ||
|
||
import org.springframework.web.bind.annotation.*; | ||
import racingcar.dto.RacingCarResponse; | ||
import racingcar.dto.RacingGameRequest; | ||
import racingcar.service.RacingCarService; | ||
import racingcar.util.NumberGenerator; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("plays") | ||
public class RacingCarWebController { | ||
|
||
private final RacingCarService racingCarService; | ||
private final NumberGenerator numberGenerator; | ||
|
||
public RacingCarWebController(RacingCarService racingCarService, NumberGenerator numberGenerator) { | ||
this.racingCarService = racingCarService; | ||
this.numberGenerator = numberGenerator; | ||
} | ||
|
||
@PostMapping | ||
public RacingCarResponse createGame(@RequestBody RacingGameRequest racingGameRequest) { | ||
return racingCarService.play(racingGameRequest, numberGenerator); | ||
} | ||
|
||
@GetMapping | ||
public List<RacingCarResponse> loadAllGame() { | ||
return racingCarService.findAllGame(); | ||
} | ||
|
||
@ExceptionHandler | ||
public void handle(Exception exception) { | ||
System.out.println(exception.getMessage()); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
...n/java/racingcar/domain/RacingCarDto.java → ...ain/java/racingcar/dto/RacingCarData.java
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
6 changes: 3 additions & 3 deletions
6
.../racingcar/domain/GameInforamtionDto.java → ...java/racingcar/dto/RacingGameRequest.java
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package racingcar.entity; | ||
|
||
public class RacingCar { | ||
|
||
private final long id; | ||
private final String name; | ||
private final int position; | ||
private final long resultId; | ||
|
||
public RacingCar(long id, String name, int position, long resultId) { | ||
this.id = id; | ||
this.name = name; | ||
this.position = position; | ||
this.resultId = resultId; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
|
||
public long getResultId() { | ||
return resultId; | ||
} | ||
} |
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,34 @@ | ||
package racingcar.entity; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class Result { | ||
|
||
private final long id; | ||
private final int trialCount; | ||
private final String winners; | ||
private final LocalDateTime createdAt; | ||
|
||
public Result(long id, int trialCount, String winners, LocalDateTime createdAt) { | ||
this.id = id; | ||
this.trialCount = trialCount; | ||
this.winners = winners; | ||
this.createdAt = createdAt; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public int getTrialCount() { | ||
return trialCount; | ||
} | ||
|
||
public String getWinners() { | ||
return winners; | ||
} | ||
|
||
public LocalDateTime getCreatedAt() { | ||
return createdAt; | ||
} | ||
} |
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,39 @@ | ||
package racingcar.repository; | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowMapper; | ||
import org.springframework.stereotype.Repository; | ||
import racingcar.domain.Car; | ||
import racingcar.entity.RacingCar; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public class RacingCarDao { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
|
||
public RacingCarDao(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public void insert(Car car, long resultId) { | ||
String sql = "insert into racing_cars (name, position, result_id) values (?, ?, ?)"; | ||
jdbcTemplate.update(sql, car.getName(), car.getLocation(), resultId); | ||
} | ||
|
||
private final RowMapper<RacingCar> actorRowMapper = (resultSet, rowNum) -> { | ||
RacingCar racingCar = new RacingCar( | ||
resultSet.getLong("id"), | ||
resultSet.getString("name"), | ||
resultSet.getInt("position"), | ||
resultSet.getLong("result_id") | ||
); | ||
return racingCar; | ||
}; | ||
|
||
public List<RacingCar> findBy(long resultId) { | ||
String sql = "select * from racing_cars where result_id = ?"; | ||
return jdbcTemplate.query(sql, actorRowMapper, resultId); | ||
} | ||
} |
Oops, something went wrong.