refactor: remove redundant user not found message handling
This commit is contained in:
		
					parent
					
						
							
								29a53ea781
							
						
					
				
			
			
				commit
				
					
						b1e173f44b
					
				
			
		
					 13 changed files with 26 additions and 37 deletions
				
			
		| 
						 | 
				
			
			@ -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();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,4 +27,4 @@ public class WebConfig {
 | 
			
		|||
            }
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,4 +28,4 @@ public class AuthController {
 | 
			
		|||
        GetUserDto response = authService.register(signUpRequest);
 | 
			
		||||
        return ResponseEntity.ok(response);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -39,4 +39,4 @@ public class CorsFilter implements Filter {
 | 
			
		|||
        
 | 
			
		||||
        chain.doFilter(req, res);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,4 +16,4 @@ public class AuthResponseDto {
 | 
			
		|||
    public AuthResponseDto(String token) {
 | 
			
		||||
        this.token = token;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,4 +16,4 @@ public class LoginRequestDto {
 | 
			
		|||
    
 | 
			
		||||
    @NotBlank(message = "Password is required")
 | 
			
		||||
    private String password;
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -61,4 +61,4 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
 | 
			
		|||
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -80,4 +80,4 @@ public class JwtUtils {
 | 
			
		|||
        final String username = extractUsername(token);
 | 
			
		||||
        return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -47,4 +47,4 @@ public class AuthService {
 | 
			
		|||
                user.getBalance()
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue