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}") @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(); Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) { if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found"); throw new UserNotFoundException();
} }
UserEntity user = optionalUser.get(); UserEntity user = optionalUser.get();
@ -46,11 +46,11 @@ public class BlackJackGameController {
} }
@PostMapping("/blackjack/{id}/hit") @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(); Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) { if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found"); throw new UserNotFoundException();
} }
UserEntity user = optionalUser.get(); UserEntity user = optionalUser.get();
@ -63,11 +63,11 @@ public class BlackJackGameController {
} }
@PostMapping("/blackjack/{id}/stand") @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(); Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) { if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found"); throw new UserNotFoundException();
} }
UserEntity user = optionalUser.get(); UserEntity user = optionalUser.get();
@ -80,11 +80,11 @@ public class BlackJackGameController {
} }
@PostMapping("/blackjack/{id}/doubleDown") @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(); Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) { if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found"); throw new UserNotFoundException();
} }
UserEntity user = optionalUser.get(); UserEntity user = optionalUser.get();
@ -97,11 +97,11 @@ public class BlackJackGameController {
} }
@PostMapping("/blackjack/{id}/split") @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(); Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) { if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found"); throw new UserNotFoundException();
} }
UserEntity user = optionalUser.get(); UserEntity user = optionalUser.get();
@ -114,11 +114,11 @@ public class BlackJackGameController {
} }
@PostMapping("/blackjack/start") @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(); Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) { if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found"); throw new UserNotFoundException();
} }
UserEntity user = optionalUser.get(); UserEntity user = optionalUser.get();

View file

@ -27,4 +27,4 @@ public class WebConfig {
} }
}; };
} }
} }

View file

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

View file

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

View file

@ -28,4 +28,4 @@ public class AuthController {
GetUserDto response = authService.register(signUpRequest); GetUserDto response = authService.register(signUpRequest);
return ResponseEntity.ok(response); return ResponseEntity.ok(response);
} }
} }

View file

@ -39,4 +39,4 @@ public class CorsFilter implements Filter {
chain.doFilter(req, res); chain.doFilter(req, res);
} }
} }

View file

@ -16,4 +16,4 @@ public class AuthResponseDto {
public AuthResponseDto(String token) { public AuthResponseDto(String token) {
this.token = token; this.token = token;
} }
} }

View file

@ -16,4 +16,4 @@ public class LoginRequestDto {
@NotBlank(message = "Password is required") @NotBlank(message = "Password is required")
private String password; private String password;
} }

View file

@ -61,4 +61,4 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
return null; return null;
} }
} }

View file

@ -80,4 +80,4 @@ public class JwtUtils {
final String username = extractUsername(token); final String username = extractUsername(token);
return (username.equals(userDetails.getUsername()) && !isTokenExpired(token)); return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));
} }
} }

View file

@ -47,4 +47,4 @@ public class AuthService {
user.getBalance() user.getBalance()
); );
} }
} }

View file

@ -34,15 +34,4 @@ public class UserDetailsServiceImpl implements UserDetailsService {
userEntity.getPassword(), userEntity.getPassword(),
new ArrayList<>()); 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(); Optional<UserEntity> optionalUser = userService.getCurrentUser();
if (optionalUser.isEmpty()) { if (optionalUser.isEmpty()) {
throw new UserNotFoundException("User not found"); throw new UserNotFoundException();
} }
UserEntity user = optionalUser.get(); UserEntity user = optionalUser.get();