refactor: throw and handle unsufficient funds exception
All checks were successful
CI / Get Changed Files (pull_request) Successful in 6s
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 40s

This commit is contained in:
Phan Huy Tran 2025-04-24 14:48:30 +02:00
commit a22c127500
5 changed files with 24 additions and 5 deletions

View file

@ -1,5 +1,6 @@
package de.szut.casino.blackjack; package de.szut.casino.blackjack;
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
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;
@ -123,7 +124,7 @@ public class BlackJackGameController {
UserEntity user = optionalUser.get(); UserEntity user = optionalUser.get();
if (!this.balanceService.hasFunds(user, betDto)) { if (!this.balanceService.hasFunds(user, betDto)) {
return ResponseEntity.badRequest().body(Collections.singletonMap("error", "Insufficient funds")); throw new InsufficientFundsException();
} }
return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betDto.getBetAmount())); return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betDto.getBetAmount()));

View file

@ -1,5 +1,6 @@
package de.szut.casino.exceptionHandling; package de.szut.casino.exceptionHandling;
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException; import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -17,4 +18,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.NOT_FOUND); return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
} }
@ExceptionHandler(InsufficientFundsException.class)
public ResponseEntity<?> handleInsufficientFundsException(InsufficientFundsException ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
}
} }

View file

@ -0,0 +1,11 @@
package de.szut.casino.exceptionHandling.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public class InsufficientFundsException extends RuntimeException {
public InsufficientFundsException() {
super("insufficient funds");
}
}

View file

@ -1,5 +1,6 @@
package de.szut.casino.lootboxes; package de.szut.casino.lootboxes;
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException; import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import de.szut.casino.user.UserEntity; import de.szut.casino.user.UserEntity;
import de.szut.casino.user.UserRepository; import de.szut.casino.user.UserRepository;
@ -44,9 +45,7 @@ public class LootBoxController {
UserEntity user = optionalUser.get(); UserEntity user = optionalUser.get();
if (lootBoxService.hasSufficientBalance(user, lootBox.getPrice())) { if (lootBoxService.hasSufficientBalance(user, lootBox.getPrice())) {
Map<String, String> errorResponse = new HashMap<>(); throw new InsufficientFundsException();
errorResponse.put("error", "Insufficient balance");
return ResponseEntity.badRequest().body(errorResponse);
} }
RewardEntity reward = lootBoxService.determineReward(lootBox); RewardEntity reward = lootBoxService.determineReward(lootBox);

View file

@ -1,5 +1,6 @@
package de.szut.casino.slots; package de.szut.casino.slots;
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
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;
@ -38,7 +39,7 @@ public class SlotController {
UserEntity user = optionalUser.get(); UserEntity user = optionalUser.get();
if (!this.balanceService.hasFunds(user, betDto)) { if (!this.balanceService.hasFunds(user, betDto)) {
return ResponseEntity.badRequest().body(Collections.singletonMap("error", "Insufficient funds")); throw new InsufficientFundsException();
} }
SpinResult spinResult = this.slotService.spin( SpinResult spinResult = this.slotService.spin(