diff --git a/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java b/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java index c780ca9..d4c0e6e 100644 --- a/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java +++ b/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java @@ -1,7 +1,6 @@ package de.szut.casino.blackjack; import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException; -import de.szut.casino.exceptionHandling.exceptions.UserBlackJackGameMismatchException; import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException; import de.szut.casino.shared.dto.BetDto; import de.szut.casino.shared.service.BalanceService; @@ -31,35 +30,85 @@ public class BlackJackGameController { @GetMapping("/blackjack/{id}") public ResponseEntity getGame(@PathVariable Long id) { - BlackJackGameEntity game = getBlackJackGame(id); + Optional optionalUser = userService.getCurrentUser(); + + if (optionalUser.isEmpty()) { + throw new UserNotFoundException(); + } + + UserEntity user = optionalUser.get(); + BlackJackGameEntity game = blackJackService.getBlackJackGame(id); + if (game == null || !Objects.equals(game.getUserId(), user.getId())) { + return ResponseEntity.notFound().build(); + } return ResponseEntity.ok(game); } @PostMapping("/blackjack/{id}/hit") public ResponseEntity hit(@PathVariable Long id) { - BlackJackGameEntity game = getBlackJackGame(id); + Optional optionalUser = userService.getCurrentUser(); + + if (optionalUser.isEmpty()) { + throw new UserNotFoundException(); + } + + UserEntity user = optionalUser.get(); + BlackJackGameEntity game = blackJackService.getBlackJackGame(id); + if (game == null || !Objects.equals(game.getUserId(), user.getId())) { + return ResponseEntity.notFound().build(); + } return ResponseEntity.ok(blackJackService.hit(game)); } @PostMapping("/blackjack/{id}/stand") public ResponseEntity stand(@PathVariable Long id) { - BlackJackGameEntity game = getBlackJackGame(id); + Optional optionalUser = userService.getCurrentUser(); + + if (optionalUser.isEmpty()) { + throw new UserNotFoundException(); + } + + UserEntity user = optionalUser.get(); + BlackJackGameEntity game = blackJackService.getBlackJackGame(id); + if (game == null || !Objects.equals(game.getUserId(), user.getId())) { + return ResponseEntity.notFound().build(); + } return ResponseEntity.ok(blackJackService.stand(game)); } @PostMapping("/blackjack/{id}/doubleDown") public ResponseEntity doubleDown(@PathVariable Long id) { - BlackJackGameEntity game = getBlackJackGame(id); + Optional optionalUser = userService.getCurrentUser(); + + if (optionalUser.isEmpty()) { + throw new UserNotFoundException(); + } + + UserEntity user = optionalUser.get(); + BlackJackGameEntity game = blackJackService.getBlackJackGame(id); + if (game == null || !Objects.equals(game.getUserId(), user.getId())) { + return ResponseEntity.notFound().build(); + } return ResponseEntity.ok(blackJackService.doubleDown(game)); } @PostMapping("/blackjack/{id}/split") public ResponseEntity split(@PathVariable Long id) { - BlackJackGameEntity game = getBlackJackGame(id); + Optional optionalUser = userService.getCurrentUser(); + + if (optionalUser.isEmpty()) { + throw new UserNotFoundException(); + } + + UserEntity user = optionalUser.get(); + BlackJackGameEntity game = blackJackService.getBlackJackGame(id); + if (game == null || !Objects.equals(game.getUserId(), user.getId())) { + return ResponseEntity.notFound().build(); + } return ResponseEntity.ok(blackJackService.split(game)); } @@ -80,20 +129,4 @@ public class BlackJackGameController { return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betDto.getBetAmount())); } - - private BlackJackGameEntity getBlackJackGame(Long gameId) { - Optional optionalUser = userService.getCurrentUser(); - - if (optionalUser.isEmpty()) { - throw new UserNotFoundException(); - } - - UserEntity user = optionalUser.get(); - BlackJackGameEntity game = blackJackService.getBlackJackGame(gameId); - if (game == null || !Objects.equals(game.getUserId(), user.getId())) { - throw new UserBlackJackGameMismatchException(gameId); - } - - return game; - } } diff --git a/backend/src/main/java/de/szut/casino/blackjack/BlackJackService.java b/backend/src/main/java/de/szut/casino/blackjack/BlackJackService.java index 5e54efe..f791d19 100644 --- a/backend/src/main/java/de/szut/casino/blackjack/BlackJackService.java +++ b/backend/src/main/java/de/szut/casino/blackjack/BlackJackService.java @@ -124,6 +124,10 @@ public class BlackJackService { return blackJackGameRepository.save(game); } + private BlackJackGameEntity refreshGameState(BlackJackGameEntity game) { + return blackJackGameRepository.findById(game.getId()).orElse(game); + } + private UserEntity getUserWithFreshData(UserEntity user) { return userRepository.findById(user.getId()).orElse(user); } @@ -206,7 +210,7 @@ public class BlackJackService { } @Transactional - protected void updateUserBalance(BlackJackGameEntity game, boolean isWin) { + private void updateUserBalance(BlackJackGameEntity game, boolean isWin) { UserEntity user = getUserWithFreshData(game.getUser()); BigDecimal totalBet = game.getBet(); BigDecimal balance = user.getBalance(); diff --git a/backend/src/main/java/de/szut/casino/exceptionHandling/GlobalExceptionHandler.java b/backend/src/main/java/de/szut/casino/exceptionHandling/GlobalExceptionHandler.java index f07c03a..9c185a8 100644 --- a/backend/src/main/java/de/szut/casino/exceptionHandling/GlobalExceptionHandler.java +++ b/backend/src/main/java/de/szut/casino/exceptionHandling/GlobalExceptionHandler.java @@ -2,7 +2,6 @@ package de.szut.casino.exceptionHandling; import de.szut.casino.exceptionHandling.exceptions.EmailNotVerifiedException; import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException; -import de.szut.casino.exceptionHandling.exceptions.UserBlackJackGameMismatchException; import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException; import jakarta.persistence.EntityExistsException; import org.springframework.http.HttpStatus; @@ -39,10 +38,4 @@ public class GlobalExceptionHandler { ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false)); return new ResponseEntity<>(errorDetails, HttpStatus.UNAUTHORIZED); } - - @ExceptionHandler(UserBlackJackGameMismatchException.class) - public ResponseEntity handleUserBlackJackGameMismatchException(UserBlackJackGameMismatchException ex, WebRequest request) { - ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false)); - return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND); - } } diff --git a/backend/src/main/java/de/szut/casino/exceptionHandling/exceptions/UserBlackJackGameMismatchException.java b/backend/src/main/java/de/szut/casino/exceptionHandling/exceptions/UserBlackJackGameMismatchException.java deleted file mode 100644 index 22a93cc..0000000 --- a/backend/src/main/java/de/szut/casino/exceptionHandling/exceptions/UserBlackJackGameMismatchException.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.szut.casino.exceptionHandling.exceptions; - -public class UserBlackJackGameMismatchException extends RuntimeException { - public UserBlackJackGameMismatchException(Long gameId) { - super(String.format("Blackjack game with ID %d not found or does not belong to the current user.", gameId)); - } -}