refactor: simplify user service and update routes
This commit is contained in:
parent
51540c930b
commit
3b95725d88
12 changed files with 24 additions and 115 deletions
|
@ -8,6 +8,7 @@ import de.szut.casino.deposit.dto.AmountDto;
|
|||
import de.szut.casino.deposit.dto.SessionIdDto;
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserRepository;
|
||||
import de.szut.casino.user.UserService;
|
||||
import de.szut.casino.user.dto.KeycloakUserDto;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
@ -34,23 +35,18 @@ public class DepositController {
|
|||
|
||||
private final TransactionService transactionService;
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
private UserService userService;
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
|
||||
public DepositController(TransactionService transactionService, RestTemplate restTemplate, UserRepository userRepository) {
|
||||
public DepositController(TransactionService transactionService, UserService userService) {
|
||||
this.transactionService = transactionService;
|
||||
this.restTemplate = restTemplate;
|
||||
this.userRepository = userRepository;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@PostMapping("/deposit/checkout")
|
||||
public ResponseEntity<SessionIdDto> checkout(@RequestBody @Valid AmountDto amountDto, @RequestHeader("Authorization") String token) throws StripeException {
|
||||
Stripe.apiKey = stripeKey;
|
||||
|
||||
KeycloakUserDto userData = getAuthentikUserInfo(token);
|
||||
Optional<UserEntity> optionalUserEntity = this.userRepository.findByEmail(userData.getSub());
|
||||
Optional<UserEntity> optionalUserEntity = this.userService.getCurrentUser();
|
||||
|
||||
SessionCreateParams params = SessionCreateParams.builder()
|
||||
.addLineItem(SessionCreateParams.LineItem.builder()
|
||||
|
@ -78,13 +74,5 @@ public class DepositController {
|
|||
|
||||
return ResponseEntity.ok(new SessionIdDto(session.getId()));
|
||||
}
|
||||
|
||||
private KeycloakUserDto getAuthentikUserInfo(String token) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Authorization", token);
|
||||
ResponseEntity<KeycloakUserDto> response = this.restTemplate.exchange("https://oauth.simonis.lol/application/o/userinfo/", HttpMethod.GET, new HttpEntity<>(headers), KeycloakUserDto.class);
|
||||
|
||||
return response.getBody();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import org.springframework.http.ResponseEntity;
|
|||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
@RequestMapping("/auth")
|
||||
public class AuthController {
|
||||
|
||||
@Autowired
|
||||
|
|
|
@ -65,7 +65,7 @@ public class SecurityConfig {
|
|||
.csrf(csrf -> csrf.disable())
|
||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||
.authorizeHttpRequests(auth -> {
|
||||
auth.requestMatchers("/api/auth/**", "/webhook", "/swagger/**", "/swagger-ui/**", "/health", "/error").permitAll()
|
||||
auth.requestMatchers("/auth/**", "/webhook", "/swagger/**", "/swagger-ui/**", "/health", "/error").permitAll()
|
||||
.requestMatchers(org.springframework.http.HttpMethod.OPTIONS, "/**").permitAll()
|
||||
.anyRequest().authenticated();
|
||||
})
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package de.szut.casino.user;
|
||||
|
||||
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
||||
import de.szut.casino.user.dto.GetUserDto;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -10,7 +9,7 @@ import org.springframework.web.bind.annotation.*;
|
|||
@Slf4j
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
@RequestMapping("/api/users")
|
||||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
|
@ -23,19 +22,4 @@ public class UserController {
|
|||
public ResponseEntity<GetUserDto> getCurrentUser() {
|
||||
return ResponseEntity.ok(userMappingService.mapToGetUserDto(userService.getCurrentUser().orElseThrow()));
|
||||
}
|
||||
|
||||
@GetMapping("/current")
|
||||
public ResponseEntity<GetUserDto> getCurrentUserAlternative() {
|
||||
return ResponseEntity.ok(userMappingService.mapToGetUserDto(userService.getCurrentUser().orElseThrow()));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<GetUserDto> getUserById(@PathVariable Long id) {
|
||||
return ResponseEntity.ok(userService.getUserById(id));
|
||||
}
|
||||
|
||||
@GetMapping("/username/{username}")
|
||||
public ResponseEntity<GetUserDto> getUserByUsername(@PathVariable String username) {
|
||||
return ResponseEntity.ok(userService.getUserByUsername(username));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,9 +16,6 @@ public class UserService {
|
|||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private UserMappingService mappingService;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
|
@ -35,50 +32,15 @@ public class UserService {
|
|||
createUserDto.getEmail(),
|
||||
createUserDto.getUsername(),
|
||||
passwordEncoder.encode(createUserDto.getPassword()),
|
||||
BigDecimal.valueOf(1000) // Starting balance
|
||||
BigDecimal.valueOf(10) // Starting balance
|
||||
);
|
||||
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
public GetUserDto getUserById(Long id) {
|
||||
UserEntity user = userRepository.findById(id)
|
||||
.orElseThrow(() -> new UserNotFoundException("User not found with id: " + id));
|
||||
|
||||
return mappingService.mapToGetUserDto(user);
|
||||
}
|
||||
|
||||
public GetUserDto getUserByUsername(String username) {
|
||||
UserEntity user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new UserNotFoundException("User not found with username: " + username));
|
||||
|
||||
return mappingService.mapToGetUserDto(user);
|
||||
}
|
||||
|
||||
public GetUserDto getUserByEmail(String email) {
|
||||
UserEntity user = userRepository.findByEmail(email)
|
||||
.orElseThrow(() -> new UserNotFoundException("User not found with email: " + email));
|
||||
|
||||
return mappingService.mapToGetUserDto(user);
|
||||
}
|
||||
|
||||
public Optional<UserEntity> getCurrentUser() {
|
||||
String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
|
||||
return userRepository.findByUsername(username);
|
||||
}
|
||||
|
||||
public UserEntity getCurrentUserEntity() {
|
||||
String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
return userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new UserNotFoundException("User not found with username: " + username));
|
||||
}
|
||||
|
||||
public boolean existsByUsername(String username) {
|
||||
return userRepository.existsByUsername(username);
|
||||
}
|
||||
|
||||
public boolean existsByEmail(String email) {
|
||||
return userRepository.existsByEmail(email);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue