feat: Validate bet amount
This commit is contained in:
parent
1a4b3f073f
commit
b2b0bb2f44
4 changed files with 26 additions and 10 deletions
|
@ -3,5 +3,5 @@ Authorization: Bearer {{token}}
|
|||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"betAmount": 999
|
||||
"betAmount": -1
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package de.szut.casino.blackjack;
|
||||
|
||||
import de.szut.casino.blackjack.dto.CreateBlackJackGameDto;
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserService;
|
||||
import de.szut.casino.user.dto.CreateUserDto;
|
||||
import de.szut.casino.user.dto.GetUserDto;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -13,6 +15,10 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class BlackJackGameController {
|
||||
|
@ -21,15 +27,24 @@ public class BlackJackGameController {
|
|||
private UserService userService;
|
||||
|
||||
@PostMapping("/blackjack/start")
|
||||
public ResponseEntity<?> createBlackJackGame(@RequestBody @Valid CreateBlackJackGameDto gameData, @RequestHeader("Authorization") String token) {
|
||||
if (gameData.getBetAmount() <= 0 || gameData.getBetAmount() > userService.getCurrentUser(token).getBalance().intValue()) {
|
||||
return ResponseEntity.badRequest().body("Invalid bet amount");
|
||||
public ResponseEntity<?> createBlackJackGame(@RequestBody @Valid CreateBlackJackGameDto createBlackJackGameDto, @RequestHeader("Authorization") String token) {
|
||||
GetUserDto getUserDto = userService.getCurrentUser(token);
|
||||
BigDecimal balance = getUserDto.getBalance();
|
||||
|
||||
if (createBlackJackGameDto.getBetAmount().compareTo(BigDecimal.ZERO) <= 0) {
|
||||
Map<String, String> errorResponse = new HashMap<>();
|
||||
errorResponse.put("error", "Invalid bet amount");
|
||||
return ResponseEntity.badRequest().body(errorResponse);
|
||||
}
|
||||
|
||||
BlackJackGameEntity game = new BlackJackGameEntity();
|
||||
game.setBet(gameData.getBetAmount());
|
||||
System.out.println("Balls: ");
|
||||
System.out.println(game.getBet());
|
||||
if (createBlackJackGameDto.getBetAmount().compareTo(balance) > 0) {
|
||||
Map<String, String> errorResponse = new HashMap<>();
|
||||
errorResponse.put("error", "Insufficient funds");
|
||||
return ResponseEntity.badRequest().body(errorResponse);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,10 +5,12 @@ import lombok.Getter;
|
|||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class CreateBlackJackGameDto {
|
||||
private int betAmount;
|
||||
private BigDecimal betAmount;
|
||||
}
|
||||
|
|
|
@ -40,7 +40,6 @@ public class UserService {
|
|||
|
||||
public GetUserDto getCurrentUser(String token) {
|
||||
KeycloakUserDto userData = getKeycloakUserInfo(token);
|
||||
|
||||
if (userData == null) {
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue