Compare commits

..

3 commits

Author SHA1 Message Date
b495fdbe74
Merge pull request 'refactor: extract common code to method' (!217) from refactor-blackjack-controllers into main
All checks were successful
Release / Release (push) Successful in 1m37s
Release / Build Backend Image (push) Successful in 38s
Release / Build Frontend Image (push) Successful in 1m29s
Reviewed-on: #217
Reviewed-by: Jan K9f <jan@kjan.email>
2025-05-22 10:58:13 +00:00
Phan Huy Tran
9101e2f5db refactor: rename getter properly
All checks were successful
CI / Get Changed Files (pull_request) Successful in 11s
CI / eslint (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / Docker frontend validation (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 3m45s
CI / Docker backend validation (pull_request) Successful in 4m16s
2025-05-22 12:48:31 +02:00
Phan Huy Tran
5ad0740902 refactor: extract common code to method
All checks were successful
CI / Get Changed Files (pull_request) Successful in 39s
CI / eslint (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / Docker frontend validation (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 4m1s
CI / Docker backend validation (pull_request) Successful in 4m34s
2025-05-22 12:47:27 +02:00
4 changed files with 37 additions and 60 deletions

View file

@ -1,6 +1,7 @@
package de.szut.casino.blackjack; package de.szut.casino.blackjack;
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException; 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.exceptionHandling.exceptions.UserNotFoundException;
import de.szut.casino.shared.dto.BetDto; import de.szut.casino.shared.dto.BetDto;
import de.szut.casino.shared.service.BalanceService; import de.szut.casino.shared.service.BalanceService;
@ -30,85 +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) {
Optional<UserEntity> optionalUser = userService.getCurrentUser(); BlackJackGameEntity game = getBlackJackGame(id);
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); 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) {
Optional<UserEntity> optionalUser = userService.getCurrentUser(); BlackJackGameEntity game = getBlackJackGame(id);
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)); 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) {
Optional<UserEntity> optionalUser = userService.getCurrentUser(); BlackJackGameEntity game = getBlackJackGame(id);
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)); 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) {
Optional<UserEntity> optionalUser = userService.getCurrentUser(); BlackJackGameEntity game = getBlackJackGame(id);
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)); 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) {
Optional<UserEntity> optionalUser = userService.getCurrentUser(); BlackJackGameEntity game = getBlackJackGame(id);
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)); return ResponseEntity.ok(blackJackService.split(game));
} }
@ -129,4 +80,20 @@ public class BlackJackGameController {
return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betDto.getBetAmount())); return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betDto.getBetAmount()));
} }
private BlackJackGameEntity getBlackJackGame(Long gameId) {
Optional<UserEntity> 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;
}
} }

View file

@ -124,10 +124,6 @@ public class BlackJackService {
return blackJackGameRepository.save(game); return blackJackGameRepository.save(game);
} }
private BlackJackGameEntity refreshGameState(BlackJackGameEntity game) {
return blackJackGameRepository.findById(game.getId()).orElse(game);
}
private UserEntity getUserWithFreshData(UserEntity user) { private UserEntity getUserWithFreshData(UserEntity user) {
return userRepository.findById(user.getId()).orElse(user); return userRepository.findById(user.getId()).orElse(user);
} }
@ -210,7 +206,7 @@ public class BlackJackService {
} }
@Transactional @Transactional
private void updateUserBalance(BlackJackGameEntity game, boolean isWin) { protected void updateUserBalance(BlackJackGameEntity game, boolean isWin) {
UserEntity user = getUserWithFreshData(game.getUser()); UserEntity user = getUserWithFreshData(game.getUser());
BigDecimal totalBet = game.getBet(); BigDecimal totalBet = game.getBet();
BigDecimal balance = user.getBalance(); BigDecimal balance = user.getBalance();

View file

@ -2,6 +2,7 @@ package de.szut.casino.exceptionHandling;
import de.szut.casino.exceptionHandling.exceptions.EmailNotVerifiedException; import de.szut.casino.exceptionHandling.exceptions.EmailNotVerifiedException;
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException; 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.exceptionHandling.exceptions.UserNotFoundException;
import jakarta.persistence.EntityExistsException; import jakarta.persistence.EntityExistsException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -38,4 +39,10 @@ public class GlobalExceptionHandler {
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false)); ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.UNAUTHORIZED); 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);
}
} }

View file

@ -0,0 +1,7 @@
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));
}
}