From 404149972bd6e8aef96a25c4e36f43f61f64c8d5 Mon Sep 17 00:00:00 2001
From: Jonas <jonas.fischer14@gmx.de>
Date: Mon, 18 Dec 2023 15:35:51 +0100
Subject: [PATCH] 18.12.2023 - create models #4

---
 .../java/hdm/mi/sthbackend/model/Match.java   | 44 ++++++++-----------
 .../java/hdm/mi/sthbackend/model/Player.java  | 16 ++++---
 .../java/hdm/mi/sthbackend/model/Team.java    | 39 +++++-----------
 .../mi/sthbackend/model/TeamMatchScore.java   | 24 ++++++----
 .../hdm/mi/sthbackend/model/Tournament.java   | 17 ++++++-
 5 files changed, 69 insertions(+), 71 deletions(-)

diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/model/Match.java b/sth-backend/src/main/java/hdm/mi/sthbackend/model/Match.java
index 1d480af..f613efe 100644
--- a/sth-backend/src/main/java/hdm/mi/sthbackend/model/Match.java
+++ b/sth-backend/src/main/java/hdm/mi/sthbackend/model/Match.java
@@ -1,60 +1,54 @@
 package hdm.mi.sthbackend.model;
 
 import hdm.mi.sthbackend.exeptions.TeamMatchScoreIdNotFoundException;
+import lombok.AllArgsConstructor;
 import lombok.Getter;
+import lombok.RequiredArgsConstructor;
 import lombok.Setter;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
 
 import java.util.List;
 import java.util.Objects;
 import java.util.UUID;
 
+@Getter
+@Document("Match")
+@AllArgsConstructor
 public class Match {
-    @Getter
-    private final UUID matchId;
-    private final List<TeamMatchScore> teams;
+    private static final Logger log = LogManager.getLogger(Match.class);
+
+    @Id
+    private UUID matchId;
+    private List<TeamMatchScore> teamScores;
 
-    @Getter
     @Setter
     private Team winner;
 
-    @Getter
     @Setter
     private String comment;
 
-    @Getter
     @Setter
     private Match nextMatch;
 
-    public Match(List<TeamMatchScore> teams, Team winner, String comment, Match nextMatch) {
-        this.matchId = UUID.randomUUID();
-        this.teams = teams;
-        this.winner = winner;
-        this.comment = comment;
-        this.nextMatch = nextMatch;
-    }
-
-    public Match(List<TeamMatchScore> teams, Team winner, Match nextMatch) {
-        this(teams, winner, "", nextMatch);
-    }
-
-    public void addTeam(Team team) {
-        teams.add(new TeamMatchScore(team, 0));
-    }
 
     public void removeTeam(UUID teamMatchScoreId) throws Exception {
-        TeamMatchScore team = teams.stream().filter(t -> t.getTeamMatchScoreId().equals(teamMatchScoreId)).findFirst().orElse(null);
+        TeamMatchScore team = teamScores.stream().filter(t -> t.getTeamMatchScoreId().equals(teamMatchScoreId)).findFirst().orElse(null);
+        log.debug("removeTeam: " + team);
         if (team == null) {
             throw new TeamMatchScoreIdNotFoundException(String.format("TeamMatchScoreID %s was not found", teamMatchScoreId));
         }
-        teams.remove(team);
+        teamScores.remove(team);
     }
 
     public void removeTeam(TeamMatchScore team) {
-        teams.remove(team);
+        teamScores.remove(team);
     }
 
     public TeamMatchScore[] getAllTeams() {
-        return (TeamMatchScore[]) teams.stream().filter(Objects::nonNull).toArray();
+        return (TeamMatchScore[]) teamScores.stream().filter(Objects::nonNull).toArray();
     }
 
 }
diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/model/Player.java b/sth-backend/src/main/java/hdm/mi/sthbackend/model/Player.java
index c7e8c3e..ca12437 100644
--- a/sth-backend/src/main/java/hdm/mi/sthbackend/model/Player.java
+++ b/sth-backend/src/main/java/hdm/mi/sthbackend/model/Player.java
@@ -1,17 +1,19 @@
 package hdm.mi.sthbackend.model;
 
+import lombok.AllArgsConstructor;
 import lombok.Getter;
+import lombok.Setter;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
 
 import java.util.UUID;
 
 @Getter
+@Document("Player")
+@AllArgsConstructor
 public class Player {
+    @Id
     private final UUID playerId;
-    private final String name;
-
-    public Player(String name) {
-        this.playerId = UUID.randomUUID();
-        this.name = name;
-    }
-
+    @Setter
+    private String name;
 }
diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/model/Team.java b/sth-backend/src/main/java/hdm/mi/sthbackend/model/Team.java
index ed541eb..f03a287 100644
--- a/sth-backend/src/main/java/hdm/mi/sthbackend/model/Team.java
+++ b/sth-backend/src/main/java/hdm/mi/sthbackend/model/Team.java
@@ -1,39 +1,20 @@
 package hdm.mi.sthbackend.model;
 
-import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException;
+import lombok.AllArgsConstructor;
 import lombok.Getter;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
 
-import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
+@Getter
+@Document("Team")
+@AllArgsConstructor
 public class Team {
-    @Getter
-    private final UUID teamId;
-    private final List<Player> teamMembers;
-
-    public Team() {
-        this.teamId = UUID.randomUUID();
-        this.teamMembers = new ArrayList<>();
-    }
-
-    public void addPlayer(Player player) {
-        this.teamMembers.add(player);
-    }
-
-    public void removePlayer(UUID playerId) throws PlayerIdNotFoundException {
-        Player player = this.teamMembers.stream().filter(p -> p.getPlayerId().equals(playerId)).findFirst().orElse(null);
-        if (player == null) {
-            throw new PlayerIdNotFoundException(playerId.toString());
-        }
-        teamMembers.remove(player);
-    }
-
-    public void removePlayer(Player player) {
-        teamMembers.remove(player);
-    }
-
-
-
+    @Id
+    private final UUID id;
 
+    private final String teamName;
+    private final List<Player> teamMembers;
 }
diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/model/TeamMatchScore.java b/sth-backend/src/main/java/hdm/mi/sthbackend/model/TeamMatchScore.java
index 14c7535..ee9213c 100644
--- a/sth-backend/src/main/java/hdm/mi/sthbackend/model/TeamMatchScore.java
+++ b/sth-backend/src/main/java/hdm/mi/sthbackend/model/TeamMatchScore.java
@@ -1,19 +1,25 @@
 package hdm.mi.sthbackend.model;
 
+import lombok.AllArgsConstructor;
 import lombok.Getter;
+import lombok.Setter;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
 
 import java.util.UUID;
 
+@Getter
+@Document("TeamMatchScore")
+@AllArgsConstructor
 public class TeamMatchScore {
-    @Getter
-    private final UUID teamMatchScoreId;
-    private Team team;
 
-    private int score;
+    private static final Logger log = LogManager.getLogger(TeamMatchScore.class);
 
-    public TeamMatchScore(Team team, int score) {
-        this.teamMatchScoreId = UUID.randomUUID();
-        this.team = team;
-        this.score = score;
-    }
+    @Id
+    private final UUID teamMatchScoreId;
+    private final UUID teamId;
+    @Setter
+    private int score;
 }
diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/model/Tournament.java b/sth-backend/src/main/java/hdm/mi/sthbackend/model/Tournament.java
index ab1860b..4d45472 100644
--- a/sth-backend/src/main/java/hdm/mi/sthbackend/model/Tournament.java
+++ b/sth-backend/src/main/java/hdm/mi/sthbackend/model/Tournament.java
@@ -1,9 +1,24 @@
 package hdm.mi.sthbackend.model;
 
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
+
 import java.util.List;
+import java.util.Map;
+import java.util.UUID;
 
+@Document("Tournament")
+@Getter
+@AllArgsConstructor
 public class Tournament implements ITournament{
-    private String tournamentId;
+    @Id
+    private UUID tournamentId;
+
+    private String tournamentName;
 
     private List<Match> matches;
+
+    private Map<UUID, Team> teams;
 }
-- 
GitLab