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

UserDTO from the me endpoint has wrong list of sports #70

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
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -69,16 +67,21 @@ 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());
List<SportDTO> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
Loading