refactor: extract common code to method #217

Merged
ptran merged 2 commits from refactor-blackjack-controllers into main 2025-05-22 10:58:14 +00:00
Showing only changes of commit 9101e2f5db - Show all commits

View file

@ -31,35 +31,35 @@ public class BlackJackGameController {
@GetMapping("/blackjack/{id}") @GetMapping("/blackjack/{id}")
public ResponseEntity<Object> getGame(@PathVariable Long id) { public ResponseEntity<Object> getGame(@PathVariable Long id) {
BlackJackGameEntity game = getUserGame(id); BlackJackGameEntity game = getBlackJackGame(id);
return ResponseEntity.ok(game); return ResponseEntity.ok(game);
} }
@PostMapping("/blackjack/{id}/hit") @PostMapping("/blackjack/{id}/hit")
public ResponseEntity<Object> hit(@PathVariable Long id) { public ResponseEntity<Object> hit(@PathVariable Long id) {
BlackJackGameEntity game = getUserGame(id); BlackJackGameEntity game = getBlackJackGame(id);
return ResponseEntity.ok(blackJackService.hit(game)); return ResponseEntity.ok(blackJackService.hit(game));
} }
@PostMapping("/blackjack/{id}/stand") @PostMapping("/blackjack/{id}/stand")
public ResponseEntity<Object> stand(@PathVariable Long id) { public ResponseEntity<Object> stand(@PathVariable Long id) {
BlackJackGameEntity game = getUserGame(id); BlackJackGameEntity game = getBlackJackGame(id);
return ResponseEntity.ok(blackJackService.stand(game)); return ResponseEntity.ok(blackJackService.stand(game));
} }
@PostMapping("/blackjack/{id}/doubleDown") @PostMapping("/blackjack/{id}/doubleDown")
public ResponseEntity<Object> doubleDown(@PathVariable Long id) { public ResponseEntity<Object> doubleDown(@PathVariable Long id) {
BlackJackGameEntity game = getUserGame(id); BlackJackGameEntity game = getBlackJackGame(id);
return ResponseEntity.ok(blackJackService.doubleDown(game)); return ResponseEntity.ok(blackJackService.doubleDown(game));
} }
@PostMapping("/blackjack/{id}/split") @PostMapping("/blackjack/{id}/split")
public ResponseEntity<Object> split(@PathVariable Long id) { public ResponseEntity<Object> split(@PathVariable Long id) {
BlackJackGameEntity game = getUserGame(id); BlackJackGameEntity game = getBlackJackGame(id);
return ResponseEntity.ok(blackJackService.split(game)); return ResponseEntity.ok(blackJackService.split(game));
} }
@ -81,7 +81,7 @@ public class BlackJackGameController {
return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betDto.getBetAmount())); return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betDto.getBetAmount()));
} }
private BlackJackGameEntity getUserGame(Long gameId) { private BlackJackGameEntity getBlackJackGame(Long gameId) {
Optional<UserEntity> optionalUser = userService.getCurrentUser(); Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) { if (optionalUser.isEmpty()) {