refactor: remove redundant user not found message handling

This commit is contained in:
Constantin Simonis 2025-05-07 15:02:48 +02:00 committed by Jan-Marlon Leibl
commit 45551551cc
Signed by: jleibl
GPG key ID: 300B2F906DC6F1D5
13 changed files with 26 additions and 37 deletions

View file

@ -29,11 +29,11 @@ public class BlackJackGameController {
}
@GetMapping("/blackjack/{id}")
public ResponseEntity<Object> getGame(@PathVariable Long id, @RequestHeader("Authorization") String token) {
public ResponseEntity<Object> getGame(@PathVariable Long id) {
Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found");
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();
@ -46,11 +46,11 @@ public class BlackJackGameController {
}
@PostMapping("/blackjack/{id}/hit")
public ResponseEntity<Object> hit(@PathVariable Long id, @RequestHeader("Authorization") String token) {
public ResponseEntity<Object> hit(@PathVariable Long id) {
Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found");
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();
@ -63,11 +63,11 @@ public class BlackJackGameController {
}
@PostMapping("/blackjack/{id}/stand")
public ResponseEntity<Object> stand(@PathVariable Long id, @RequestHeader("Authorization") String token) {
public ResponseEntity<Object> stand(@PathVariable Long id) {
Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found");
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();
@ -80,11 +80,11 @@ public class BlackJackGameController {
}
@PostMapping("/blackjack/{id}/doubleDown")
public ResponseEntity<Object> doubleDown(@PathVariable Long id, @RequestHeader("Authorization") String token) {
public ResponseEntity<Object> doubleDown(@PathVariable Long id) {
Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found");
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();
@ -97,11 +97,11 @@ public class BlackJackGameController {
}
@PostMapping("/blackjack/{id}/split")
public ResponseEntity<Object> split(@PathVariable Long id, @RequestHeader("Authorization") String token) {
public ResponseEntity<Object> split(@PathVariable Long id) {
Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found");
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();
@ -114,11 +114,11 @@ public class BlackJackGameController {
}
@PostMapping("/blackjack/start")
public ResponseEntity<Object> createBlackJackGame(@RequestBody @Valid BetDto betDto, @RequestHeader("Authorization") String token) {
public ResponseEntity<Object> createBlackJackGame(@RequestBody @Valid BetDto betDto) {
Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found");
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();

View file

@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
public UserNotFoundException() {
super("User not found");
}
}

View file

@ -17,7 +17,7 @@ public class LootBoxController {
private final UserService userService;
private final LootBoxService lootBoxService;
public LootBoxController(LootBoxRepository lootBoxRepository, UserRepository userRepository, UserService userService, LootBoxService lootBoxService) {
public LootBoxController(LootBoxRepository lootBoxRepository, UserService userService, LootBoxService lootBoxService) {
this.lootBoxRepository = lootBoxRepository;
this.userService = userService;
this.lootBoxService = lootBoxService;
@ -39,7 +39,7 @@ public class LootBoxController {
Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found");
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();

View file

@ -34,15 +34,4 @@ public class UserDetailsServiceImpl implements UserDetailsService {
userEntity.getPassword(),
new ArrayList<>());
}
public UserEntity getUserByUsernameOrEmail(String usernameOrEmail) {
Optional<UserEntity> user = userRepository.findByUsername(usernameOrEmail);
if (user.isEmpty()) {
user = userRepository.findByEmail(usernameOrEmail);
}
return user.orElseThrow(() ->
new UserNotFoundException("User not found with username or email: " + usernameOrEmail));
}
}

View file

@ -33,7 +33,7 @@ public class SlotController {
Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found");
throw new UserNotFoundException();
}
UserEntity user = optionalUser.get();