diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/dto/MatchDTO.java b/sth-backend/src/main/java/hdm/mi/sthbackend/dto/MatchDTO.java index 736568c6565d91a73aad7beffc27e0505f282671..b214144905df840e5e593d8c8905e26375983e55 100644 --- a/sth-backend/src/main/java/hdm/mi/sthbackend/dto/MatchDTO.java +++ b/sth-backend/src/main/java/hdm/mi/sthbackend/dto/MatchDTO.java @@ -14,7 +14,7 @@ import java.util.UUID; @NoArgsConstructor public class MatchDTO { private UUID matchId; - private Map<UUID, Number> teamScores; + private Map<UUID, Integer> teamScores; private UUID winnerTeamId; private String comment; private UUID nextMatchId; diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/dto/TournamentDTO.java b/sth-backend/src/main/java/hdm/mi/sthbackend/dto/TournamentDTO.java index 982bcda4ec27cdb8a1f2b775a4afd665c2dc1d13..b51fb0d9514ae236057144584261d5d8f97f2112 100644 --- a/sth-backend/src/main/java/hdm/mi/sthbackend/dto/TournamentDTO.java +++ b/sth-backend/src/main/java/hdm/mi/sthbackend/dto/TournamentDTO.java @@ -18,6 +18,6 @@ import java.util.UUID; public class TournamentDTO { private UUID tournamentId; private String tournamentName; - private List<Match> matches; - private Map<UUID, Team> teams; // TODO maybe just uuid? + private Map<UUID, MatchDTO> matches; + private Map<UUID, TeamDTO> teams; } diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/mapper/ModelToDTOMapper.java b/sth-backend/src/main/java/hdm/mi/sthbackend/mapper/ModelToDTOMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..897da408c9b9c5fe212796953adc7f53b8e762cf --- /dev/null +++ b/sth-backend/src/main/java/hdm/mi/sthbackend/mapper/ModelToDTOMapper.java @@ -0,0 +1,122 @@ +package hdm.mi.sthbackend.mapper; + +import hdm.mi.sthbackend.dto.MatchDTO; +import hdm.mi.sthbackend.dto.PlayerDTO; +import hdm.mi.sthbackend.dto.TeamDTO; +import hdm.mi.sthbackend.dto.TournamentDTO; +import hdm.mi.sthbackend.exeptions.MatchIdNotFoundException; +import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException; +import hdm.mi.sthbackend.exeptions.TournamentIdNotFoundException; +import hdm.mi.sthbackend.model.Match; +import hdm.mi.sthbackend.model.Player; +import hdm.mi.sthbackend.model.Team; +import hdm.mi.sthbackend.model.Tournament; +import hdm.mi.sthbackend.repository.IMatchRepository; +import hdm.mi.sthbackend.repository.IPlayerRepository; +import hdm.mi.sthbackend.repository.ITeamRepository; +import hdm.mi.sthbackend.repository.ITournamentRepository; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.UUID; +import java.util.stream.Collectors; + +@Component +public class ModelToDTOMapper { + private final Logger log = LogManager.getLogger(ModelToDTOMapper.class); + + ITournamentRepository tournamentRepository; + IMatchRepository matchRepository; + ITeamRepository teamRepository; + IPlayerRepository playerRepository; + + @Autowired + public ModelToDTOMapper(ITournamentRepository tournamentRepository, + IMatchRepository matchRepository, + ITeamRepository teamRepository, + IPlayerRepository playerRepository) { + this.tournamentRepository = tournamentRepository; + this.matchRepository = matchRepository; + this.teamRepository = teamRepository; + this.playerRepository = playerRepository; + } + + public TournamentDTO mapToTournamentDTO(Tournament tournament) { + Map<UUID, Match> matches = new HashMap<>(); + tournament.getMatches() + .forEach(mId -> { + try { + matches.put(mId, matchRepository.findById(mId) + .orElseThrow(() -> new MatchIdNotFoundException(mId))); + } catch (MatchIdNotFoundException e) { + log.debug("Match with id " + mId + " not found"); + } + }); + Map<UUID, MatchDTO> mappedMatches = matches.entrySet() + .stream() + .collect(Collectors.toMap(Entry::getKey, e -> mapToMatchDTO(e.getValue()))); + + Map<UUID, Team> teams = new HashMap<>(); + tournament.getTeams() + .forEach(teamId -> { + try { + teams.put(teamId, teamRepository.findById(teamId) + .orElseThrow(() -> new TournamentIdNotFoundException(teamId))); + } catch (TournamentIdNotFoundException e) { + log.debug("Team with id " + teamId + " not found"); + } + }); + Map<UUID, TeamDTO> mappedTeams = teams.entrySet() + .stream() + .collect(Collectors.toMap(Entry::getKey, e -> mapToTeamDTO(e.getValue()))); + return new TournamentDTO( + tournament.getTournamentId(), + tournament.getTournamentName(), + mappedMatches, + mappedTeams + ); + } + + public MatchDTO mapToMatchDTO(Match match) { + return new MatchDTO( + match.getMatchId(), + match.getTeamScores(), + match.getWinnerTeamId(), + match.getComment(), + match.getNextMatchId() + ); + } + + public PlayerDTO mapToPlayerDTO(Player player) { + return new PlayerDTO( + player.getPlayerId(), + player.getName() + ); + } + + public TeamDTO mapToTeamDTO(Team team) { + Map<UUID, Player> players = new HashMap<>(); + team.getTeamMembers() + .forEach(playerId -> { + try { + players.put(playerId, playerRepository.findById(playerId) + .orElseThrow(() -> new PlayerIdNotFoundException(playerId))); + } catch (PlayerIdNotFoundException e) { + log.debug("Player with id " + playerId + " not found"); + } + }); + Map<UUID, PlayerDTO> mappedPlayers = players.entrySet() + .stream() + .collect(Collectors.toMap(Entry::getKey, e -> mapToPlayerDTO(e.getValue()))); + return new TeamDTO( + team.getId(), + team.getTeamName(), + mappedPlayers + ); + } +}