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