Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sma 95 profile should redirect you to your profile #83

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Builder
public class UserDTO {

private Long id;
private String name;
private Integer elo;
private Integer win;
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -23,8 +28,17 @@ public User registerToUser(AuthRequestDTO authRequestDTO) {
}

public UserDTO toDTO(User user) {
List<SportDTO> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,25 +66,12 @@ public UserDTO getUserById(Long id) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}

Set<SportUser> sportUsers = user.get().getSportUsers();

List<SportDTO> userSports = sportUsers.stream()
.map(SportUser::getSport)
.map(sportMapper::toDTO)
.toList();

List<EventPlayer> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
32 changes: 20 additions & 12 deletions frontend/sportsmatch-app/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ function Navbar() {
const loggedOutUserImgUrl = 'pictures/unknown-user-placeholder.png'

const [isLoggedIn, setLoggedIn] = useState<boolean>(false)
const [userId, setUserId] = useState<number>()

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 (
Expand All @@ -52,7 +57,10 @@ function Navbar() {
id="collapsible-nav-dropdown"
>
<NavDropdown.Item href="/">Home</NavDropdown.Item>
<NavDropdown.Item href="/profile">Profile</NavDropdown.Item>
{/* <NavDropdown.Item href="/profile">Profile</NavDropdown.Item> */}
<NavDropdown.Item href={`/user/${userId}/ratings`}>
Profile
</NavDropdown.Item>
</NavDropdown>
) : (
<NavDropdown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/* eslint-disable */
import type { SportDTO } from './SportDTO';
export type UserDTO = {
id?: number;
name?: string;
elo?: number;
win?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { UserDTO } from '../models/UserDTO';
import type { CancelablePromise } from '../core/CancelablePromise';
import { OpenAPI } from '../core/OpenAPI';
import { request as __request } from '../core/request';
export class ExSecuredEndpointService {
/**
* @returns any OK
* @returns UserDTO OK
* @throws ApiError
*/
public static getUserMainPage(): CancelablePromise<Record<string, any>> {
public static getUserMainPage(): CancelablePromise<UserDTO> {
return __request(OpenAPI, {
method: 'GET',
url: '/api/v1/auth/me',
Expand Down
Loading