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

11.01.2024 - adjust service #24

parent 362e75ed
No related branches found
No related tags found
1 merge request!7Dev in Main Merge
......@@ -2,7 +2,9 @@ package hdm.mi.sthbackend.service;
import hdm.mi.sthbackend.dto.PlayerDTO;
import hdm.mi.sthbackend.dto.TeamDTO;
import hdm.mi.sthbackend.dto.TournamentDTO;
import hdm.mi.sthbackend.exeptions.*;
import hdm.mi.sthbackend.mapper.ModelToDTOMapper;
import hdm.mi.sthbackend.model.Match;
import hdm.mi.sthbackend.model.Player;
import hdm.mi.sthbackend.model.Team;
......@@ -14,6 +16,7 @@ import hdm.mi.sthbackend.repository.ITournamentRepository;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
......@@ -22,14 +25,17 @@ import java.util.*;
@AllArgsConstructor
public class TournamentService {
private final static Logger log = LogManager.getLogger(TournamentService.class);
private final Logger log = LogManager.getLogger(TournamentService.class);
ITournamentRepository tournamentRepository;
IPlayerRepository playerRepository;
ITeamRepository teamRepository;
IMatchRepository matchRepository;
public Player addPlayerToTeam(UUID teamId,
@Autowired
ModelToDTOMapper mapper;
public PlayerDTO addPlayerToTeam(UUID teamId,
String playerName) throws TeamIdNotFoundException {
Team team = teamRepository.findById(teamId)
.orElseThrow(() -> new TeamIdNotFoundException(teamId));
......@@ -41,14 +47,14 @@ public class TournamentService {
playerRepository.insert(newPlayer);
teamRepository.save(team);
log.debug("Player " + newPlayer.getPlayerId() + " added to Team " + teamId);
return newPlayer;
return mapper.mapToPlayerDTO(newPlayer);
}
public PlayerDTO findPlayerById(UUID playerId) throws PlayerIdNotFoundException {
Player player = playerRepository.findById(playerId)
.orElseThrow(() -> new PlayerIdNotFoundException(playerId));
return new PlayerDTO(player.getPlayerId(), player.getName());
return mapper.mapToPlayerDTO(player);
}
/*
......@@ -74,7 +80,7 @@ public class TournamentService {
teamRepository.save(team);
log.debug("Player " + playerId + " removed from Team " + teamId);
return new PlayerDTO(playerToDelete.getPlayerId(), playerToDelete.getName());
return mapper.mapToPlayerDTO(playerToDelete);
}
/*
......@@ -101,10 +107,10 @@ public class TournamentService {
forEach(pId -> playerRepository.findById(pId)
.ifPresent(p -> teamMembers.put(p.getPlayerId(), new PlayerDTO(p.getPlayerId(), p.getName()))));
return new TeamDTO(team.getId(), team.getTeamName(), teamMembers);
return mapper.mapToTeamDTO(team);
}
public Team createAndAddTeamToTournament(UUID tournamentId, String teamName) throws TournamentIdNotFoundException {
public TeamDTO createAndAddTeamToTournament(UUID tournamentId, String teamName) throws TournamentIdNotFoundException {
Tournament tournament = tournamentRepository.findById(tournamentId)
.orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
......@@ -115,7 +121,7 @@ public class TournamentService {
teamRepository.insert(newTeam);
tournamentRepository.save(tournament);
return newTeam;
return mapper.mapToTeamDTO(newTeam);
}
public UUID deleteAndRemoveTeamFromTournament(UUID tournamentId,
......@@ -137,64 +143,44 @@ public class TournamentService {
return teamId;
}
public Tournament createTournament(String tournamentName) {
public TournamentDTO createTournament(String tournamentName) {
Tournament tournament = new Tournament(UUID.randomUUID(), tournamentName, new ArrayList<>(), new ArrayList<>());
tournamentRepository.insert(tournament);
log.debug("created Tournament " + tournament.getTournamentId());
return tournament;
return mapper.mapToTournamentDTO(tournament);
}
public UUID determineWinner (UUID tournamentId, UUID matchId) throws TournamentIdNotFoundException, MatchIdNotFoundException, WinnerNotDeterminedException {
Tournament tournament = tournamentRepository.findById(tournamentId)
.orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
Match match = tournament.getMatches().stream()
.filter(m -> m.getMatchId().equals(matchId))
.findFirst()
.orElseThrow(() -> new MatchIdNotFoundException(matchId));
public UUID determineWinner (UUID matchId) throws MatchIdNotFoundException, WinnerNotDeterminedException {
Match match = matchRepository.findById(matchId).orElseThrow(() -> new MatchIdNotFoundException(matchId));
UUID winnerTeamId = match.getTeamScores().entrySet().stream()
.max(Comparator.comparing(Map.Entry::getKey))
.map(Map.Entry::getKey)
.orElseThrow(() -> new WinnerNotDeterminedException(matchId,tournamentId));
.orElseThrow(() -> new WinnerNotDeterminedException(matchId));
match.setWinnerTeamId(winnerTeamId);
tournamentRepository.save(tournament);
log.debug("The Winner " + winnerTeamId + " is determined for Match " + matchId + " in Tournament " + tournamentId);
matchRepository.save(match);
log.debug("The Winner " + winnerTeamId + " is determined for Match " + matchId);
return winnerTeamId;
}
public void assignTeamToMatch(UUID tournamentId, UUID matchId, UUID teamId, int score) throws TournamentIdNotFoundException, MatchIdNotFoundException {
Tournament tournament = tournamentRepository.findById(tournamentId)
.orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
Match match = tournament.getMatches().stream()
.filter(m -> m.getMatchId().equals(matchId))
.findFirst()
.orElseThrow(() -> new MatchIdNotFoundException(matchId));
Match match = matchRepository.findById(matchId).orElseThrow(() -> new MatchIdNotFoundException(matchId));
match.getTeamScores().put(teamId,score);
tournamentRepository.save(tournament);
matchRepository.save(match);
log.debug("Team " + teamId + " assign to Match " + matchId + " in Tournament " + tournamentId);
}
public void updateTeamScore(UUID tournamentId, UUID matchId, UUID teamId, int newScore)throws TournamentIdNotFoundException, MatchIdNotFoundException, TeamIdNotFoundException{
Tournament tournament = tournamentRepository.findById(tournamentId)
.orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
Match match = tournament.getMatches().stream()
.filter(m -> m.getMatchId().equals(matchId))
.findFirst()
.orElseThrow(() -> new MatchIdNotFoundException(matchId));
Match match = matchRepository.findById(matchId).orElseThrow(() -> new MatchIdNotFoundException(matchId));
if (!match.getTeamScores().containsKey(teamId)) {
throw new TeamIdNotFoundException(teamId);
}
match.getTeamScores().put(teamId, newScore);
tournamentRepository.save(tournament);
matchRepository.save(match);
log.debug("Score of Team " + teamId + " updated to " + newScore);
}
......@@ -217,12 +203,12 @@ public class TournamentService {
log.debug("Tournament Name for TournamentID " + tournamentId + " updated to " + newTournamentName);
return tournamentId;
}
public Tournament getTournament(UUID tournamentId) throws TournamentIdNotFoundException {
public TournamentDTO getTournament(UUID tournamentId) throws TournamentIdNotFoundException {
Tournament tournament = tournamentRepository.findById(tournamentId)
.orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
log.debug(tournament + "is found");
return tournament;
return mapper.mapToTournamentDTO(tournament);
}
public void deleteTournament(UUID tournamentId) throws TournamentIdNotFoundException {
......
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