Merge pull request 'refactor: handle optional user inside userservice, replace @autowire' (!227) from refactor-get-current-user into main
All checks were successful
Release / Release (push) Successful in 1m2s
Release / Build Frontend Image (push) Successful in 25s
Release / Build Backend Image (push) Successful in 32s
CI / Get Changed Files (pull_request) Successful in 11s
CI / eslint (pull_request) Has been skipped
CI / Checkstyle Main (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / Docker frontend validation (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / Docker backend validation (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
Setup Gitea Tea CLI / setup-and-login (pull_request) Successful in 1m11s
All checks were successful
Release / Release (push) Successful in 1m2s
Release / Build Frontend Image (push) Successful in 25s
Release / Build Backend Image (push) Successful in 32s
CI / Get Changed Files (pull_request) Successful in 11s
CI / eslint (pull_request) Has been skipped
CI / Checkstyle Main (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / Docker frontend validation (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / Docker backend validation (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
Setup Gitea Tea CLI / setup-and-login (pull_request) Successful in 1m11s
Reviewed-on: #227 Reviewed-by: Jan K9f <jan@kjan.email>
This commit is contained in:
commit
7be0fc97bc
21 changed files with 127 additions and 151 deletions
|
@ -59,13 +59,7 @@ public class BlackJackGameController {
|
|||
|
||||
@PostMapping("/blackjack/start")
|
||||
public ResponseEntity<Object> createBlackJackGame(@RequestBody @Valid BetDto betDto) {
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
||||
|
||||
if (optionalUser.isEmpty()) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
UserEntity user = optionalUser.get();
|
||||
UserEntity user = this.userService.getCurrentUser();
|
||||
|
||||
if (!this.balanceService.hasFunds(user, betDto)) {
|
||||
throw new InsufficientFundsException();
|
||||
|
@ -75,13 +69,7 @@ public class BlackJackGameController {
|
|||
}
|
||||
|
||||
private BlackJackGameEntity getBlackJackGame(Long gameId) {
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
||||
|
||||
if (optionalUser.isEmpty()) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
UserEntity user = optionalUser.get();
|
||||
UserEntity user = userService.getCurrentUser();
|
||||
BlackJackGameEntity game = blackJackService.getBlackJackGame(gameId);
|
||||
if (game == null || !Objects.equals(game.getUserId(), user.getId())) {
|
||||
throw new UserBlackJackGameMismatchException(gameId);
|
||||
|
|
|
@ -28,13 +28,7 @@ public class CoinflipController {
|
|||
|
||||
@PostMapping("/coinflip")
|
||||
public ResponseEntity<Object> coinFlip(@RequestBody @Valid CoinflipDto coinflipDto) {
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
||||
|
||||
if (optionalUser.isEmpty()) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
UserEntity user = optionalUser.get();
|
||||
UserEntity user = userService.getCurrentUser();
|
||||
|
||||
if (!this.balanceService.hasFunds(user, coinflipDto)) {
|
||||
throw new InsufficientFundsException();
|
||||
|
|
|
@ -16,8 +16,6 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
public class DepositController {
|
||||
|
||||
|
@ -29,7 +27,7 @@ public class DepositController {
|
|||
|
||||
private final TransactionService transactionService;
|
||||
|
||||
private UserService userService;
|
||||
private final UserService userService;
|
||||
|
||||
public DepositController(TransactionService transactionService, UserService userService) {
|
||||
this.transactionService = transactionService;
|
||||
|
@ -40,7 +38,7 @@ public class DepositController {
|
|||
public ResponseEntity<SessionIdDto> checkout(@RequestBody @Valid AmountDto amountDto, @RequestHeader("Authorization") String token) throws StripeException {
|
||||
Stripe.apiKey = stripeKey;
|
||||
|
||||
Optional<UserEntity> optionalUserEntity = this.userService.getCurrentUser();
|
||||
UserEntity user = userService.getCurrentUser();
|
||||
|
||||
SessionCreateParams params = SessionCreateParams.builder()
|
||||
.addLineItem(SessionCreateParams.LineItem.builder()
|
||||
|
@ -60,11 +58,7 @@ public class DepositController {
|
|||
|
||||
Session session = Session.create(params);
|
||||
|
||||
if (optionalUserEntity.isEmpty()) {
|
||||
throw new RuntimeException("User doesnt exist");
|
||||
}
|
||||
|
||||
transactionService.createTransaction(optionalUserEntity.get(), session.getId(), amountDto.getAmount());
|
||||
transactionService.createTransaction(user, session.getId(), amountDto.getAmount());
|
||||
|
||||
return ResponseEntity.ok(new SessionIdDto(session.getId()));
|
||||
}
|
||||
|
|
|
@ -27,13 +27,7 @@ public class DiceController {
|
|||
|
||||
@PostMapping("/dice")
|
||||
public ResponseEntity<Object> rollDice(@RequestBody @Valid DiceDto diceDto) {
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
||||
|
||||
if (optionalUser.isEmpty()) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
UserEntity user = optionalUser.get();
|
||||
UserEntity user = userService.getCurrentUser();
|
||||
|
||||
if (!this.balanceService.hasFunds(user, diceDto)) {
|
||||
throw new InsufficientFundsException();
|
||||
|
|
|
@ -38,13 +38,7 @@ public class LootBoxController {
|
|||
}
|
||||
|
||||
LootBoxEntity lootBox = optionalLootBox.get();
|
||||
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
||||
if (optionalUser.isEmpty()) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
UserEntity user = optionalUser.get();
|
||||
UserEntity user = userService.getCurrentUser();
|
||||
|
||||
if (lootBoxService.hasSufficientBalance(user, lootBox.getPrice())) {
|
||||
throw new InsufficientFundsException();
|
||||
|
|
|
@ -9,7 +9,6 @@ import de.szut.casino.user.dto.CreateUserDto;
|
|||
import de.szut.casino.user.dto.GetUserDto;
|
||||
import jakarta.mail.MessagingException;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
@ -20,8 +19,11 @@ import java.io.IOException;
|
|||
public class AuthController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private AuthService authService;
|
||||
private final AuthService authService;
|
||||
|
||||
public AuthController(AuthService authService) {
|
||||
this.authService = authService;
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<AuthResponseDto> authenticateUser(@Valid @RequestBody LoginRequestDto loginRequest) throws EmailNotVerifiedException {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package de.szut.casino.security;
|
||||
|
||||
import de.szut.casino.security.jwt.JwtAuthenticationFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
@ -32,11 +31,13 @@ public class SecurityConfig {
|
|||
@Value("${app.frontend-host}")
|
||||
private String frontendHost;
|
||||
|
||||
@Autowired
|
||||
private UserDetailsService userDetailsService;
|
||||
private final UserDetailsService userDetailsService;
|
||||
private final JwtAuthenticationFilter jwtAuthenticationFilter;
|
||||
|
||||
@Autowired
|
||||
private JwtAuthenticationFilter jwtAuthenticationFilter;
|
||||
public SecurityConfig(UserDetailsService userDetailsService, JwtAuthenticationFilter jwtAuthenticationFilter) {
|
||||
this.userDetailsService = userDetailsService;
|
||||
this.jwtAuthenticationFilter = jwtAuthenticationFilter;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
|
|
|
@ -4,7 +4,6 @@ import jakarta.servlet.FilterChain;
|
|||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
@ -19,11 +18,13 @@ import java.io.IOException;
|
|||
@Component
|
||||
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||
|
||||
@Autowired
|
||||
private JwtUtils jwtUtils;
|
||||
private final JwtUtils jwtUtils;
|
||||
private final UserDetailsService userDetailsService;
|
||||
|
||||
@Autowired
|
||||
private UserDetailsService userDetailsService;
|
||||
public JwtAuthenticationFilter(JwtUtils jwtUtils, UserDetailsService userDetailsService) {
|
||||
this.jwtUtils = jwtUtils;
|
||||
this.userDetailsService = userDetailsService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||
|
|
|
@ -4,7 +4,6 @@ import de.szut.casino.exceptionHandling.exceptions.OAuth2AuthenticationProcessin
|
|||
import de.szut.casino.user.AuthProvider;
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.InternalAuthenticationServiceException;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
@ -22,11 +21,13 @@ import java.util.UUID;
|
|||
@Service
|
||||
public class CustomOAuth2UserService extends DefaultOAuth2UserService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final PasswordEncoder oauth2PasswordEncoder;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder oauth2PasswordEncoder;
|
||||
public CustomOAuth2UserService(UserRepository userRepository, PasswordEncoder oauth2PasswordEncoder) {
|
||||
this.userRepository = userRepository;
|
||||
this.oauth2PasswordEncoder = oauth2PasswordEncoder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OAuth2User loadUser(OAuth2UserRequest oAuth2UserRequest) throws OAuth2AuthenticationException {
|
||||
|
|
|
@ -5,7 +5,6 @@ import jakarta.servlet.http.HttpServletRequest;
|
|||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
|
||||
|
@ -21,8 +20,11 @@ public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS
|
|||
@Value("${app.oauth2.authorizedRedirectUris}")
|
||||
private String redirectUri;
|
||||
|
||||
@Autowired
|
||||
private JwtUtils jwtUtils;
|
||||
private final JwtUtils jwtUtils;
|
||||
|
||||
public OAuth2AuthenticationSuccessHandler(JwtUtils jwtUtils) {
|
||||
this.jwtUtils = jwtUtils;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
|
||||
|
|
|
@ -3,7 +3,6 @@ package de.szut.casino.security.oauth2.github;
|
|||
import de.szut.casino.security.dto.AuthResponseDto;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
@ -23,8 +22,11 @@ public class GitHubController {
|
|||
@Value("${spring.security.oauth2.client.registration.github.redirect-uri}")
|
||||
private String redirectUri;
|
||||
|
||||
@Autowired
|
||||
private GitHubService githubService;
|
||||
private final GitHubService githubService;
|
||||
|
||||
public GitHubController(GitHubService githubService) {
|
||||
this.githubService = githubService;
|
||||
}
|
||||
|
||||
@GetMapping("/authorize")
|
||||
public RedirectView authorizeGithub() {
|
||||
|
|
|
@ -5,7 +5,6 @@ import de.szut.casino.security.jwt.JwtUtils;
|
|||
import de.szut.casino.user.AuthProvider;
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
@ -29,17 +28,17 @@ public class GitHubService {
|
|||
@Value("${spring.security.oauth2.client.registration.github.client-secret}")
|
||||
private String clientSecret;
|
||||
|
||||
@Autowired
|
||||
private AuthenticationManager authenticationManager;
|
||||
private final AuthenticationManager authenticationManager;
|
||||
private final UserRepository userRepository;
|
||||
private final JwtUtils jwtUtils;
|
||||
private final PasswordEncoder oauth2PasswordEncoder;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private JwtUtils jwtUtils;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder oauth2PasswordEncoder;
|
||||
public GitHubService(AuthenticationManager authenticationManager, UserRepository userRepository, JwtUtils jwtUtils, PasswordEncoder oauth2PasswordEncoder) {
|
||||
this.authenticationManager = authenticationManager;
|
||||
this.userRepository = userRepository;
|
||||
this.jwtUtils = jwtUtils;
|
||||
this.oauth2PasswordEncoder = oauth2PasswordEncoder;
|
||||
}
|
||||
|
||||
public AuthResponseDto processGithubCode(String code) {
|
||||
try {
|
||||
|
|
|
@ -4,7 +4,6 @@ import de.szut.casino.security.dto.AuthResponseDto;
|
|||
import de.szut.casino.security.oauth2.github.GithubCallbackDto;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
@ -24,8 +23,11 @@ public class GoogleController {
|
|||
@Value("${spring.security.oauth2.client.registration.google.redirect-uri}")
|
||||
private String redirectUri;
|
||||
|
||||
@Autowired
|
||||
private GoogleService googleService;
|
||||
private final GoogleService googleService;
|
||||
|
||||
public GoogleController(GoogleService googleService) {
|
||||
this.googleService = googleService;
|
||||
}
|
||||
|
||||
@GetMapping("/authorize")
|
||||
public RedirectView authorizeGoogle() {
|
||||
|
|
|
@ -7,7 +7,6 @@ import de.szut.casino.user.UserEntity;
|
|||
import de.szut.casino.user.UserRepository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
@ -23,7 +22,9 @@ import org.springframework.util.MultiValueMap;
|
|||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class GoogleService {
|
||||
|
@ -44,17 +45,17 @@ public class GoogleService {
|
|||
@Value("${spring.security.oauth2.client.provider.google.user-info-uri}")
|
||||
private String userInfoUri;
|
||||
|
||||
@Autowired
|
||||
private AuthenticationManager authenticationManager;
|
||||
private final AuthenticationManager authenticationManager;
|
||||
private final UserRepository userRepository;
|
||||
private final JwtUtils jwtUtils;
|
||||
private final PasswordEncoder oauth2PasswordEncoder;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private JwtUtils jwtUtils;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder oauth2PasswordEncoder;
|
||||
public GoogleService(AuthenticationManager authenticationManager, UserRepository userRepository, JwtUtils jwtUtils, PasswordEncoder oauth2PasswordEncoder) {
|
||||
this.authenticationManager = authenticationManager;
|
||||
this.userRepository = userRepository;
|
||||
this.jwtUtils = jwtUtils;
|
||||
this.oauth2PasswordEncoder = oauth2PasswordEncoder;
|
||||
}
|
||||
|
||||
public AuthResponseDto processGoogleCode(String code) {
|
||||
try {
|
||||
|
|
|
@ -11,7 +11,6 @@ import de.szut.casino.user.dto.CreateUserDto;
|
|||
import de.szut.casino.user.dto.GetUserDto;
|
||||
import jakarta.mail.MessagingException;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
@ -25,20 +24,19 @@ import java.util.Optional;
|
|||
@Service
|
||||
public class AuthService {
|
||||
|
||||
@Autowired
|
||||
private AuthenticationManager authenticationManager;
|
||||
private final AuthenticationManager authenticationManager;
|
||||
private final JwtUtils jwtUtils;
|
||||
private final UserService userService;
|
||||
private final EmailService emailService;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Autowired
|
||||
private JwtUtils jwtUtils;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private EmailService emailService;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
public AuthService(AuthenticationManager authenticationManager, JwtUtils jwtUtils, UserService userService, EmailService emailService, PasswordEncoder passwordEncoder) {
|
||||
this.authenticationManager = authenticationManager;
|
||||
this.jwtUtils = jwtUtils;
|
||||
this.userService = userService;
|
||||
this.emailService = emailService;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
public AuthResponseDto login(LoginRequestDto loginRequest) throws EmailNotVerifiedException {
|
||||
if (!userService.isVerified(loginRequest.getUsernameOrEmail())) {
|
||||
|
|
|
@ -2,7 +2,6 @@ package de.szut.casino.security.service;
|
|||
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
|
@ -14,8 +13,11 @@ import java.util.Optional;
|
|||
@Service
|
||||
public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public UserDetailsServiceImpl(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String usernameOrEmail) throws UsernameNotFoundException {
|
||||
|
|
|
@ -32,13 +32,7 @@ public class SlotController {
|
|||
|
||||
@PostMapping("/slots/spin")
|
||||
public ResponseEntity<Object> spinSlots(@RequestBody @Valid BetDto betDto) {
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser();
|
||||
|
||||
if (optionalUser.isEmpty()) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
UserEntity user = optionalUser.get();
|
||||
UserEntity user = userService.getCurrentUser();
|
||||
|
||||
if (!this.balanceService.hasFunds(user, betDto)) {
|
||||
throw new InsufficientFundsException();
|
||||
|
|
|
@ -2,7 +2,6 @@ package de.szut.casino.user;
|
|||
|
||||
import de.szut.casino.user.dto.GetUserDto;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
@ -15,14 +14,17 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
private final UserService userService;
|
||||
|
||||
@Autowired
|
||||
private UserMappingService userMappingService;
|
||||
private final UserMappingService userMappingService;
|
||||
|
||||
public UserController(UserService userService, UserMappingService userMappingService) {
|
||||
this.userService = userService;
|
||||
this.userMappingService = userMappingService;
|
||||
}
|
||||
|
||||
@GetMapping("/me")
|
||||
public ResponseEntity<GetUserDto> getCurrentUser() {
|
||||
return ResponseEntity.ok(userMappingService.mapToGetUserDto(userService.getCurrentUser().orElseThrow()));
|
||||
return ResponseEntity.ok(userMappingService.mapToGetUserDto(userService.getCurrentUser()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package de.szut.casino.user;
|
||||
|
||||
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
|
||||
import de.szut.casino.user.dto.CreateUserDto;
|
||||
import jakarta.persistence.EntityExistsException;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -13,11 +13,13 @@ import java.util.Optional;
|
|||
|
||||
@Service
|
||||
public class UserService {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
||||
this.userRepository = userRepository;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
public UserEntity createUser(CreateUserDto createUserDto) {
|
||||
if (userRepository.existsByUsername(createUserDto.getUsername())) {
|
||||
|
@ -39,10 +41,15 @@ public class UserService {
|
|||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
public Optional<UserEntity> getCurrentUser() {
|
||||
public UserEntity getCurrentUser() {
|
||||
String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
|
||||
return userRepository.findByUsername(username);
|
||||
Optional<UserEntity> optionalUser = userRepository.findByUsername(username);
|
||||
if (optionalUser.isEmpty()) {
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
return optionalUser.get();
|
||||
}
|
||||
|
||||
public Optional<UserEntity> getUserByVerificationToken(String token) {
|
||||
|
|
|
@ -6,31 +6,29 @@ import de.szut.casino.user.UserEntity;
|
|||
import de.szut.casino.user.UserService;
|
||||
import de.szut.casino.user.transaction.dto.GetTransactionDto;
|
||||
import de.szut.casino.user.transaction.dto.UserTransactionsDto;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class GetTransactionService {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
private final UserService userService;
|
||||
|
||||
@Autowired
|
||||
private TransactionRepository transactionRepository;
|
||||
private final TransactionRepository transactionRepository;
|
||||
|
||||
public UserTransactionsDto getUserTransactionsDto(String authToken, Integer limit, Integer offset) {
|
||||
Optional<UserEntity> user = this.userService.getCurrentUser();
|
||||
if (user.isPresent()) {
|
||||
List<TransactionEntity> transactionEntities = this.transactionRepository.findByUserIdWithLimit(user.get(), limit, offset);
|
||||
Boolean hasMore = this.transactionRepository.hasMore(user.get(), limit, offset);
|
||||
public GetTransactionService(UserService userService, TransactionRepository transactionRepository) {
|
||||
this.userService = userService;
|
||||
this.transactionRepository = transactionRepository;
|
||||
}
|
||||
|
||||
return new UserTransactionsDto(mapTransactionsToDtos(transactionEntities), hasMore);
|
||||
}
|
||||
public UserTransactionsDto getUserTransactionsDto(Integer limit, Integer offset) {
|
||||
UserEntity user = userService.getCurrentUser();
|
||||
|
||||
return new UserTransactionsDto(List.of(), false);
|
||||
List<TransactionEntity> transactionEntities = this.transactionRepository.findByUserIdWithLimit(user, limit, offset);
|
||||
Boolean hasMore = this.transactionRepository.hasMore(user, limit, offset);
|
||||
|
||||
return new UserTransactionsDto(mapTransactionsToDtos(transactionEntities), hasMore);
|
||||
}
|
||||
|
||||
public List<GetTransactionDto> mapTransactionsToDtos(List<TransactionEntity> transactions) {
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
package de.szut.casino.user.transaction;
|
||||
|
||||
import de.szut.casino.user.transaction.dto.UserTransactionsDto;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class TransactionController {
|
||||
|
||||
@Autowired
|
||||
private GetTransactionService transactionService;
|
||||
private final GetTransactionService transactionService;
|
||||
|
||||
public TransactionController(GetTransactionService transactionService) {
|
||||
this.transactionService = transactionService;
|
||||
}
|
||||
|
||||
@GetMapping("/user/transactions")
|
||||
public ResponseEntity<UserTransactionsDto> getUserTransactions(
|
||||
@RequestHeader("Authorization") String authToken,
|
||||
@RequestParam(value = "limit", required = false) Integer limit,
|
||||
@RequestParam(value = "offset", required = false) Integer offset
|
||||
) {
|
||||
UserTransactionsDto transactionEntities = this.transactionService.getUserTransactionsDto(authToken, limit, offset);
|
||||
UserTransactionsDto transactionEntities = this.transactionService.getUserTransactionsDto(limit, offset);
|
||||
|
||||
return ResponseEntity.ok(transactionEntities);
|
||||
}
|
||||
|
|
Reference in a new issue