package de.szut.casino.coinflip; 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 CoinflipController { private final UserService userService; private final BalanceService balanceService; private final CoinflipService coinflipService; public CoinflipController(UserService userService, BalanceService balanceService, CoinflipService coinflipService) { this.userService = userService; this.balanceService = balanceService; this.coinflipService = coinflipService; } @PostMapping("/coinflip") public ResponseEntity coinFlip(@RequestBody @Valid CoinflipDto coinflipDto) { UserEntity user = userService.getCurrentUser(); if (!this.balanceService.hasFunds(user, coinflipDto)) { throw new InsufficientFundsException(); } return ResponseEntity.ok(coinflipService.play(user, coinflipDto)); } }