Skip to content
Snippets Groups Projects
Commit a08b3696 authored by Jonas's avatar Jonas
Browse files

11.01.2024 - write mapper class and adjust DTOs #22

parent 7074a3b5
No related branches found
No related tags found
1 merge request!7Dev in Main Merge
......@@ -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;
......
......@@ -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;
}
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
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment