-
Notifications
You must be signed in to change notification settings - Fork 122
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
[2단계 - 웹 자동차 경주] 제이미(임정수) 미션 제출합니다. #183
Changes from all commits
5d4c6c0
dc95ea0
eec0887
a8e5efe
ea1c77e
05e9802
b457b72
372aa11
ff4645b
4e460d0
ddd3a5a
3fbcf65
4994fdc
1475dcc
f998022
604966f
ab3dd50
ce5e165
00f45a5
72a9705
82f8a15
a170c7b
be898cb
58dfcf1
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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
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); | ||
} | ||
} |
This file was deleted.
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()); | ||
} | ||
Comment on lines
+33
to
+36
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. 1단계 피드백주신 내용에 따라 ExceptionHandler를 추가해보았습니다. 그런데 html 코드를 살펴보니 다음과 같이 예외가 발생하면 무조건 Error()를 던지게 되어 있습니다. .then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("Network response was not ok.");
}
}) 이 경우 Controller에서 예외를 반환해 주어야 하는 걸까요? 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. 제 생각을 정리해 보겠습니다.
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. 저라면, 아래처럼 구현할 것으로 생각됩니다. 한번 생각한 대로 정리해 볼게요.
혹시 이해가 가지 않는 부분이 있다면 코멘트 부탁드립니다. 👍 |
||
} |
This file was deleted.
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; | ||
} | ||
} |
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; | ||
} | ||
} |
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); | ||
} | ||
} |
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.
콘솔과 웹 게임의 중복 코드를 제거해 주기 위해 ConsoleController에서 사용하는 메서드들을 public으로 바꿔주었습니다.
이와 같은 방법이 맞는 것인지 아직 감이 오지 않는 상태입니다.
카프카의 의견이 궁금합니다...!
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.
사실 보통의 상황이라면 이렇게 콘솔과 웹을 동시에 구현하지는 않을듯해요. 그러나 미션 요구사항이므로...특수한 상황임을 감안하면 이해가 되는 부분입니다.
일반적인 구조라면, Controller는 요청에 대한 라우팅만 수행하는 것이 맞기는 합니다. 위의 runGame 메소드같은 경우도, 요청이 들어올때 Service에서 수행해줘야 하지요. 일단 RacingCarWebController에서 사양에 맞게 잘 구현되어 있으므로, 이 부분에 대해서는 감안하겠습니다.
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.
RacingCarConsoleController 클래스의 runGame()에 존재하는 로직은 모두 View를 위한 로직인데 그래도 Service에 있어야 하는 것일까요?
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.
View를 위한 로직이라는 말이 참 애매한데... 현재 보면 컨트롤러가 View 레이어와 강한 의존을 가지고 있다고 느껴져요.
만약 완벽하게 리팩토링을 하고 싶다면, outputview와 inputview를 json DTO를 통해 통신하도록 완벽히 구현해서 WebController와 동일한 서비스를 사용하도록 해볼 수 있어 보이긴 합니다. 다만 이미 WebController 부분의 구현에서 해당 스펙을 구현했으니, 이번 미션에서 그 정도까지의 작업을 할 필요는 없다고 판단했습니다.
MVC 구조에서 서비스가 하는 역할에 대해 잘 이해하고 있다면, 크게 문제가 되어 보이지는 않습니다.