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 f0c838b4..61ffbd53 100644 --- a/backend/sportsmatch/src/main/java/com/sportsmatch/services/UserServiceImp.java +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/services/UserServiceImp.java @@ -1,13 +1,13 @@ package com.sportsmatch.services; +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 com.sportsmatch.dtos.SportDTO; -import com.sportsmatch.dtos.UserInfoDTO; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; @@ -16,13 +16,11 @@ 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.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Optional; +import java.util.*; @Service @RequiredArgsConstructor @@ -69,16 +67,21 @@ 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()); - List sports = new ArrayList<>(); for (EventPlayer e : events) { rankService.updatePlayersRanks(e.getEvent()); - sports.add(sportMapper.toDTO(e.getEvent().getSport())); } return UserDTO.builder() .name(user.get().getName()) - .sports(sports) + .sports(userSports) .elo(user.get().getRank()) .win(user.get().getWin()) .loss(user.get().getLoss()) 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 c875c297..81f8f0ec 100644 --- a/backend/sportsmatch/src/test/java/com/sportsmatch/services/UserServiceImpTest.java +++ b/backend/sportsmatch/src/test/java/com/sportsmatch/services/UserServiceImpTest.java @@ -106,7 +106,7 @@ void getUserById() { Sport sport = new Sport(); sport.setId(1L); sport.setName("Tennis"); - sport.setEmoji("🎾"); + sport.setEmoji(""); sport.setBackgroundImageURL("./assets/sport-component-tennis.png"); // SportUser @@ -125,17 +125,16 @@ void getUserById() { sports.add(sportDTO); // Event - Event event = mock(Event.class); + Event event = new Event(); event.setSport(sport); - EventPlayer eventPlayer = mock(EventPlayer.class); - - when(eventPlayer.getEvent()).thenReturn(event); - when(eventPlayer.getEvent().getSport()).thenReturn(sport); + // EventPlayer (no need to mock) + EventPlayer eventPlayer = new EventPlayer(); + eventPlayer.setEvent(event); // Set the event directly user.getEventsPlayed().add(eventPlayer); - doNothing().when(rankService).updatePlayersRanks(event); + // No need to mock eventPlayer.getEvent() UserDTO expectedUserDTO = UserDTO.builder().name(user.getName()).elo(user.getRank()).sports(sports).build();