diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/AuthController.java b/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/AuthController.java index b0b29a87..ade47149 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/AuthController.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/controllers/AuthController.java @@ -2,6 +2,7 @@ import com.sportsmatch.auth.AuthService; import com.sportsmatch.dtos.AuthRequestDTO; +import com.sportsmatch.dtos.UserDTO; import com.sportsmatch.services.UserService; import com.sportsmatch.services.ValidationService; import io.swagger.v3.oas.annotations.Operation; @@ -54,11 +55,7 @@ public ResponseEntity login( @GetMapping("/me") @Tag(name = "ex.secured endpoint") - public ResponseEntity getUserMainPage() { - try { - return ResponseEntity.ok().body(userService.getMyRank()); - } catch (ResponseStatusException e) { - return new ResponseEntity<>(e.getMessage(), e.getStatusCode()); - } + public UserDTO getUserMainPage() { + return userService.getMyRank(); } } diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/UserDTO.java b/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/UserDTO.java index 280731e4..9ccad33e 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/UserDTO.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/dtos/UserDTO.java @@ -10,6 +10,7 @@ @Builder public class UserDTO { + private Long id; private String name; private Integer elo; private Integer win; diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/UserMapper.java b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/UserMapper.java index 5a59277a..e754295f 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/UserMapper.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/mappers/UserMapper.java @@ -1,18 +1,23 @@ package com.sportsmatch.mappers; import com.sportsmatch.dtos.AuthRequestDTO; +import com.sportsmatch.dtos.SportDTO; import com.sportsmatch.dtos.UserDTO; import com.sportsmatch.models.Role; +import com.sportsmatch.models.SportUser; import com.sportsmatch.models.User; import lombok.RequiredArgsConstructor; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Component; +import java.util.List; + @Component @RequiredArgsConstructor public class UserMapper { private final PasswordEncoder passwordEncoder; + private final SportMapper sportMapper; public User registerToUser(AuthRequestDTO authRequestDTO) { return User.builder() @@ -23,8 +28,17 @@ public User registerToUser(AuthRequestDTO authRequestDTO) { } public UserDTO toDTO(User user) { + List userSports = user.getSportUsers().stream() + .map(SportUser::getSport) + .map(sportMapper::toDTO) + .toList(); return UserDTO.builder() - .name(user.getName()) - .build(); + .id(user.getId()) + .name(user.getName()) + .sports(userSports) + .elo(user.getRank()) + .win(user.getWin()) + .loss(user.getLoss()) + .build(); } } diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/services/UserServiceImp.java b/backend/sportsmatch/src/main/java/com/sportsmatch/services/UserServiceImp.java index 61ffbd53..86cface0 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/services/UserServiceImp.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/services/UserServiceImp.java @@ -9,21 +9,20 @@ import com.sportsmatch.repositories.SportRepository; import com.sportsmatch.repositories.UserRepository; import jakarta.transaction.Transactional; -import lombok.RequiredArgsConstructor; +import lombok.AllArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; - import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.*; @Service -@RequiredArgsConstructor +@AllArgsConstructor public class UserServiceImp implements UserService { private final UserRepository userRepository; @@ -67,25 +66,12 @@ public UserDTO getUserById(Long id) { throw new ResponseStatusException(HttpStatus.NOT_FOUND); } - Set sportUsers = user.get().getSportUsers(); - - List userSports = sportUsers.stream() - .map(SportUser::getSport) - .map(sportMapper::toDTO) - .toList(); - List events = new ArrayList<>(user.get().getEventsPlayed()); for (EventPlayer e : events) { rankService.updatePlayersRanks(e.getEvent()); } - return UserDTO.builder() - .name(user.get().getName()) - .sports(userSports) - .elo(user.get().getRank()) - .win(user.get().getWin()) - .loss(user.get().getLoss()) - .build(); + return userMapper.toDTO(user.get()); } @Transactional diff --git a/backend/sportsmatch/src/test/java/com/sportsmatch/services/UserServiceImpTest.java b/backend/sportsmatch/src/test/java/com/sportsmatch/services/UserServiceImpTest.java index 81f8f0ec..56b8b539 100644 --- a/backend/sportsmatch/src/test/java/com/sportsmatch/services/UserServiceImpTest.java +++ b/backend/sportsmatch/src/test/java/com/sportsmatch/services/UserServiceImpTest.java @@ -3,35 +3,50 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; - import com.sportsmatch.BaseTest; import com.sportsmatch.dtos.SportDTO; import com.sportsmatch.dtos.UserDTO; import com.sportsmatch.dtos.UserInfoDTO; import com.sportsmatch.mappers.SportMapper; +import com.sportsmatch.mappers.UserMapper; import com.sportsmatch.models.*; +import com.sportsmatch.repositories.SportRepository; import com.sportsmatch.repositories.UserRepository; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.*; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.crypto.password.PasswordEncoder; @ExtendWith(MockitoExtension.class) class UserServiceImpTest extends BaseTest { @Mock private UserRepository userRepository; + @Mock private SportRepository sportRepository; @Mock private SportMapper sportMapper; @Mock private SecurityContext securityContext; @Mock private RankService rankService; - @InjectMocks private UserServiceImp userServiceImp; + @Mock private PasswordEncoder passwordEncoder; + private UserServiceImp userServiceImp; + + @BeforeEach + void initialize() { + userServiceImp = + new UserServiceImp( + userRepository, + sportMapper, + sportRepository, + new UserMapper(passwordEncoder, sportMapper), + rankService); + } @Test void updateUserInfo() { diff --git a/frontend/sportsmatch-app/src/components/Navbar.tsx b/frontend/sportsmatch-app/src/components/Navbar.tsx index 3e53154d..1044d6a1 100644 --- a/frontend/sportsmatch-app/src/components/Navbar.tsx +++ b/frontend/sportsmatch-app/src/components/Navbar.tsx @@ -10,22 +10,27 @@ function Navbar() { const loggedOutUserImgUrl = 'pictures/unknown-user-placeholder.png' const [isLoggedIn, setLoggedIn] = useState(false) + const [userId, setUserId] = useState() useEffect(() => { - const init = async () => { - OpenAPI.TOKEN = localStorage.getItem('token')! - try { - await ExSecuredEndpointService.getUserMainPage() - setLoggedIn(true) - } catch (error) { - const code = (error as ApiError).status - if (code === 401) { - localStorage.removeItem('token') - setLoggedIn(false) + if (localStorage.getItem('token')) { + const init = async () => { + OpenAPI.TOKEN = localStorage.getItem('token')! + try { + const user = await ExSecuredEndpointService.getUserMainPage() + console.log(user) + setUserId(user.id) + setLoggedIn(true) + } catch (error) { + const code = (error as ApiError).status + if (code === 401) { + localStorage.removeItem('token') + setLoggedIn(false) + } } } + init() } - init() }, []) return ( @@ -52,7 +57,10 @@ function Navbar() { id="collapsible-nav-dropdown" > Home - Profile + {/* Profile */} + + Profile + ) : ( > { + public static getUserMainPage(): CancelablePromise { return __request(OpenAPI, { method: 'GET', url: '/api/v1/auth/me',