refactor: Extract game creation logic to service
All checks were successful
CI / Get Changed Files (pull_request) Successful in 29s
CI / eslint (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
CI / Checkstyle Main (pull_request) Successful in 1m19s

This commit is contained in:
Phan Huy Tran 2025-03-26 14:31:32 +01:00
parent 1b9bc90920
commit ca87c684b1
2 changed files with 37 additions and 27 deletions

View file

@ -2,11 +2,9 @@ package de.szut.casino.blackjack;
import de.szut.casino.blackjack.dto.CreateBlackJackGameDto;
import de.szut.casino.user.UserEntity;
import de.szut.casino.user.UserRepository;
import de.szut.casino.user.UserService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@ -24,14 +22,10 @@ public class BlackJackGameController {
private final UserService userService;
private final BlackJackService blackJackService;
private final BlackJackGameRepository blackJackGameRepository;
private final UserRepository userRepository;
public BlackJackGameController(UserService userService, BlackJackService blackJackService, BlackJackGameRepository blackJackGameRepository, UserRepository userRepository) {
this.userService = userService;
public BlackJackGameController(UserService userService, BlackJackService blackJackService) {
this.blackJackService = blackJackService;
this.blackJackGameRepository = blackJackGameRepository;
this.userRepository = userRepository;
this.userService = userService;
}
@PostMapping("/blackjack/start")
@ -58,23 +52,6 @@ public class BlackJackGameController {
return ResponseEntity.badRequest().body(errorResponse);
}
BlackJackGameEntity game = new BlackJackGameEntity();
game.setUser(user);
game.setBet(betAmount);
for (int i = 0; i < 2; i++) {
CardEntity playerCard = blackJackService.createRandomCard(game);
game.getPlayerCards().add(playerCard);
}
CardEntity dealerCard = blackJackService.createRandomCard(game);
game.getDealerCards().add(dealerCard);
user.setBalance(user.getBalance().subtract(betAmount));
userRepository.save(user);
blackJackGameRepository.save(game);
return ResponseEntity.ok(game);
return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betAmount));
}
}

View file

@ -1,13 +1,46 @@
package de.szut.casino.blackjack;
import de.szut.casino.user.UserEntity;
import de.szut.casino.user.UserRepository;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Random;
@Service
public class BlackJackService {
private final BlackJackGameRepository blackJackGameRepository;
private final UserRepository userRepository;
public BlackJackService(BlackJackGameRepository blackJackGameRepository, UserRepository userRepository) {
this.blackJackGameRepository = blackJackGameRepository;
this.userRepository = userRepository;
}
private final Random random = new Random();
public CardEntity createRandomCard(BlackJackGameEntity game) {
public BlackJackGameEntity createBlackJackGame(UserEntity user, BigDecimal betAmount) {
BlackJackGameEntity game = new BlackJackGameEntity();
game.setUser(user);
game.setBet(betAmount);
for (int i = 0; i < 2; i++) {
CardEntity playerCard = createRandomCard(game);
game.getPlayerCards().add(playerCard);
}
CardEntity dealerCard = createRandomCard(game);
game.getDealerCards().add(dealerCard);
user.setBalance(user.getBalance().subtract(betAmount));
userRepository.save(user);
blackJackGameRepository.save(game);
return game;
}
private CardEntity createRandomCard(BlackJackGameEntity game) {
CardEntity card = new CardEntity();
card.setGame(game);
card.setSuit(Suit.values()[random.nextInt(Suit.values().length)]);