Skip to content
Snippets Groups Projects
Commit 468bf62c authored by Bauer Matthis's avatar Bauer Matthis
Browse files

Merge branch 'ServiceMethods' of gitlab.mi.hdm-stuttgart.de:ss576/student-tournament-hub into dev

parents 63d47116 4e10f04b
No related branches found
No related tags found
1 merge request!7Dev in Main Merge
package hdm.mi.sthbackend.controller;
import hdm.mi.sthbackend.exeptions.MatchIdNotFoundException;
import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException;
import hdm.mi.sthbackend.exeptions.TeamIdNotFoundException;
import hdm.mi.sthbackend.exeptions.TournamentIdNotFoundException;
......@@ -43,7 +44,7 @@ public class TournamentController {
public UUID removeTeamFromTournament(@PathVariable UUID tournamentId,
@PathVariable UUID teamId) throws TournamentIdNotFoundException, TeamIdNotFoundException{
return service.removeTeamFromTournament(tournamentId, teamId);
};
}
@PostMapping("/{tournamentId}/teams/{teamId}/addPlayer")
public Player addPlayerToTeam(@PathVariable UUID tournamentId,
......@@ -55,8 +56,14 @@ public class TournamentController {
@DeleteMapping("/{tournamentId}/teams/{teamId}/players/{playerId}")
public Player removePlayerFromTeam(@PathVariable UUID tournamentId,
@PathVariable UUID teamId,
@PathVariable UUID playerId) throws PlayerIdNotFoundException, TournamentIdNotFoundException {
return service.removePlayerFromTeam(tournamentId, teamId, playerId);
@PathVariable UUID playerId) throws PlayerIdNotFoundException, TournamentIdNotFoundException { // muss hier die PlayerIdNotFoundException eingefügt werden,
return service.removePlayerFromTeam(tournamentId, teamId, playerId); // wenn die nicht im TournamentService drin ist?
}
@PatchMapping
public UUID determineWinner(@PathVariable UUID tournamentId,
@PathVariable UUID matchId) throws TournamentIdNotFoundException, MatchIdNotFoundException {
return service.determineWinner(tournamentId, matchId);
}
......
package hdm.mi.sthbackend.exeptions;
import java.util.UUID;
public class MatchIdNotFoundException extends Exception{
public MatchIdNotFoundException(UUID matchId){
super(String.format("MatchID %s not found", matchId.toString()));
}
}
package hdm.mi.sthbackend.service;
import com.mongodb.client.MongoClient;
import hdm.mi.sthbackend.exeptions.MatchIdNotFoundException;
import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException;
import hdm.mi.sthbackend.exeptions.TeamIdNotFoundException;
import hdm.mi.sthbackend.exeptions.TournamentIdNotFoundException;
import hdm.mi.sthbackend.model.Player;
import hdm.mi.sthbackend.model.Team;
import hdm.mi.sthbackend.model.Tournament;
import hdm.mi.sthbackend.model.*;
import hdm.mi.sthbackend.repository.ITournamentRepository;
import hdm.mi.sthbackend.repository.MongoManager;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
@Service
@AllArgsConstructor
public class TournamentService {
private final static Logger log = LogManager.getLogger(TournamentService.class);
ITournamentRepository tournamentRepository;
public Player addPlayerToTeam(UUID tournamentId,
......@@ -35,9 +35,21 @@ public class TournamentService {
.getTeamMembers()
.put(newPlayer.getPlayerId(), newPlayer);
tournamentRepository.save(tournament);
log.debug("Player " + newPlayer.getPlayerId() + " added to Team " + teamId + " in Tournament " + tournamentId);
return newPlayer;
}
/*
Wie gehen wir damit um, wenn zwei Player denselben Namen haben? Also: Stefan vs Stefan = Stefan gewinnt
Sollte man hier in den Methoden das ausschließen und dann geben die Spieler ihren ersten Buchstaben vom Nachnamen an? z.B. Stefan S. vs Stefan w.
oder geben wir die UUID auch mit an zum PlayerName
*/
public Player removePlayerFromTeam(UUID tournamentId,
UUID teamId,
UUID playerId) throws TournamentIdNotFoundException {
......@@ -50,9 +62,16 @@ public class TournamentService {
.remove(playerId);
tournamentRepository.save(tournament);
log.debug("Player " + playerId + " removed from Team " + teamId + " in Tournament " + tournamentId);
return removedPlayer;
}
/*
Kann es passieren, dass hier bei der Methode removePlayerFromTeam() es den Player gar nicht gibt?
und dann eine PlayerIdNotFoundException geworfen werden muss? oder ist durch die Übergabe von UUID playerId schon safe die playerId vorhanden
*/
public UUID updatePlayerName(UUID tournamentId,
UUID teamId,
UUID playerId,
......@@ -67,6 +86,7 @@ public class TournamentService {
.setName(newPlayerName);
tournamentRepository.save(tournament);
log.debug("Player " + playerId + " changed Name to " + newPlayerName + " in " + teamId + " in Tournament " + tournamentId);
return playerId;
}
......@@ -81,6 +101,7 @@ public class TournamentService {
.put(team.getId(), team);
tournamentRepository.save(tournament);
log.debug("Team " + team.getId() + " added to Tournament " + tournamentId);
return team;
}
......@@ -93,6 +114,7 @@ public class TournamentService {
.remove(teamId))
.orElseThrow(() -> new TeamIdNotFoundException(tournamentId));
tournamentRepository.save(tournament);
log.debug("Team " + teamId + " added to Tournament " + tournamentId);
return teamId;
}
......@@ -103,8 +125,53 @@ public class TournamentService {
Tournament tournament = new Tournament(UUID.randomUUID(), tournamentName, new ArrayList<>(), new HashMap<>());
tournamentRepository.insert(tournament);
tournamentRepository.save(tournament);// Muss es hier nochmal gespeichert werden?
log.debug("created Tournament " + tournament.getTournamentId());
return tournament;
}
public UUID determineWinner (UUID tournamentId, UUID matchId) 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));
UUID winnerTeamId = match.getTeamScores().entrySet().stream()
.max(Comparator.<Map.Entry<UUID, Number>, Double>comparing(entry -> entry.getValue().doubleValue()))
.map(Map.Entry::getKey)
.orElse(null);
match.setWinnerTeamId(winnerTeamId);
tournamentRepository.save(tournament);
log.debug("The Winner " + winnerTeamId + " is determined for Match " + matchId + " in Tournament " + tournamentId);
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.getTeamScores().put(teamId,score);
tournamentRepository.save(tournament);
log.debug("Team " + teamId + " assign to Match " + matchId + " in Tournament " + tournamentId);
}
/*
Weitere Methoden:
Gewinner bestimmen, defineWinner()
Teams Matches zuordnen,
*/
}
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