From a08b36967532da14ea33308adf5882ee5499711d Mon Sep 17 00:00:00 2001
From: Jonas <jonas.fischer14@gmx.de>
Date: Thu, 11 Jan 2024 22:17:36 +0100
Subject: [PATCH] 11.01.2024 - write mapper class and adjust DTOs #22

---
 .../java/hdm/mi/sthbackend/dto/MatchDTO.java  |   2 +-
 .../hdm/mi/sthbackend/dto/TournamentDTO.java  |   4 +-
 .../sthbackend/mapper/ModelToDTOMapper.java   | 122 ++++++++++++++++++
 3 files changed, 125 insertions(+), 3 deletions(-)
 create mode 100644 sth-backend/src/main/java/hdm/mi/sthbackend/mapper/ModelToDTOMapper.java

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 736568c..b214144 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 982bcda..b51fb0d 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 0000000..897da40
--- /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
+        );
+    }
+}
-- 
GitLab