44 lines
1.6 KiB
Java
44 lines
1.6 KiB
Java
package de.szut.casino.dice;
|
|
|
|
import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
|
|
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
|
import de.szut.casino.shared.service.BalanceService;
|
|
import de.szut.casino.user.UserEntity;
|
|
import de.szut.casino.user.UserService;
|
|
import jakarta.validation.Valid;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import java.util.Optional;
|
|
|
|
@RestController
|
|
public class DiceController {
|
|
private final UserService userService;
|
|
private final BalanceService balanceService;
|
|
private final DiceService diceService;
|
|
|
|
public DiceController(UserService userService, BalanceService balanceService, DiceService diceService) {
|
|
this.userService = userService;
|
|
this.balanceService = balanceService;
|
|
this.diceService = diceService;
|
|
}
|
|
|
|
@PostMapping("/dice")
|
|
public ResponseEntity<Object> rollDice(@RequestBody @Valid DiceDto diceDto) {
|
|
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
|
|
|
if (optionalUser.isEmpty()) {
|
|
throw new UserNotFoundException();
|
|
}
|
|
|
|
UserEntity user = optionalUser.get();
|
|
|
|
if (!this.balanceService.hasFunds(user, diceDto)) {
|
|
throw new InsufficientFundsException();
|
|
}
|
|
|
|
return ResponseEntity.ok(diceService.play(user, diceDto));
|
|
}
|
|
}
|