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

18.12.2023 - create models #4

parent dd5f96a9
No related branches found
No related tags found
1 merge request!7Dev in Main Merge
package hdm.mi.sthbackend.model; package hdm.mi.sthbackend.model;
import hdm.mi.sthbackend.exeptions.TeamMatchScoreIdNotFoundException; import hdm.mi.sthbackend.exeptions.TeamMatchScoreIdNotFoundException;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter; 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.List;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@Getter
@Document("Match")
@AllArgsConstructor
public class Match { public class Match {
@Getter private static final Logger log = LogManager.getLogger(Match.class);
private final UUID matchId;
private final List<TeamMatchScore> teams; @Id
private UUID matchId;
private List<TeamMatchScore> teamScores;
@Getter
@Setter @Setter
private Team winner; private Team winner;
@Getter
@Setter @Setter
private String comment; private String comment;
@Getter
@Setter @Setter
private Match nextMatch; 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 { 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) { if (team == null) {
throw new TeamMatchScoreIdNotFoundException(String.format("TeamMatchScoreID %s was not found", teamMatchScoreId)); throw new TeamMatchScoreIdNotFoundException(String.format("TeamMatchScoreID %s was not found", teamMatchScoreId));
} }
teams.remove(team); teamScores.remove(team);
} }
public void removeTeam(TeamMatchScore team) { public void removeTeam(TeamMatchScore team) {
teams.remove(team); teamScores.remove(team);
} }
public TeamMatchScore[] getAllTeams() { public TeamMatchScore[] getAllTeams() {
return (TeamMatchScore[]) teams.stream().filter(Objects::nonNull).toArray(); return (TeamMatchScore[]) teamScores.stream().filter(Objects::nonNull).toArray();
} }
} }
package hdm.mi.sthbackend.model; package hdm.mi.sthbackend.model;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.UUID; import java.util.UUID;
@Getter @Getter
@Document("Player")
@AllArgsConstructor
public class Player { public class Player {
@Id
private final UUID playerId; private final UUID playerId;
private final String name; @Setter
private String name;
public Player(String name) {
this.playerId = UUID.randomUUID();
this.name = name;
}
} }
package hdm.mi.sthbackend.model; package hdm.mi.sthbackend.model;
import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException; import lombok.AllArgsConstructor;
import lombok.Getter; 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.List;
import java.util.UUID; import java.util.UUID;
@Getter
@Document("Team")
@AllArgsConstructor
public class Team { public class Team {
@Getter @Id
private final UUID teamId; private final UUID id;
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);
}
private final String teamName;
private final List<Player> teamMembers;
} }
package hdm.mi.sthbackend.model; package hdm.mi.sthbackend.model;
import lombok.AllArgsConstructor;
import lombok.Getter; 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; import java.util.UUID;
@Getter
@Document("TeamMatchScore")
@AllArgsConstructor
public class TeamMatchScore { 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) { @Id
this.teamMatchScoreId = UUID.randomUUID(); private final UUID teamMatchScoreId;
this.team = team; private final UUID teamId;
this.score = score; @Setter
} private int score;
} }
package hdm.mi.sthbackend.model; 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.List;
import java.util.Map;
import java.util.UUID;
@Document("Tournament")
@Getter
@AllArgsConstructor
public class Tournament implements ITournament{ public class Tournament implements ITournament{
private String tournamentId; @Id
private UUID tournamentId;
private String tournamentName;
private List<Match> matches; private List<Match> matches;
private Map<UUID, Team> teams;
} }
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