Skip to content

Commit

Permalink
UserDTO from the me endpoint has wrong list of sports (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: Matěj Frnka <[email protected]>
  • Loading branch information
kz44 and MatejFrnka authored Apr 13, 2024
1 parent 4e1eff0 commit ebb3071
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
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

0 comments on commit ebb3071

Please sign in to comment.