Skip to content
Snippets Groups Projects
Commit fa044ceb authored by Schneider Stefan's avatar Schneider Stefan
Browse files

08.01.2024 - added WinnerNotDeterminedException and changed determineWinner method #24 #25

parent 3b3be323
No related branches found
No related tags found
1 merge request!7Dev in Main Merge
...@@ -2,11 +2,8 @@ package hdm.mi.sthbackend.controller; ...@@ -2,11 +2,8 @@ package hdm.mi.sthbackend.controller;
import hdm.mi.sthbackend.dto.PlayerDTO; import hdm.mi.sthbackend.dto.PlayerDTO;
import hdm.mi.sthbackend.exeptions.MatchIdNotFoundException; import hdm.mi.sthbackend.exeptions.*;
import hdm.mi.sthbackend.dto.TeamDTO; import hdm.mi.sthbackend.dto.TeamDTO;
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.Player;
import hdm.mi.sthbackend.model.Team; import hdm.mi.sthbackend.model.Team;
import hdm.mi.sthbackend.model.Tournament; import hdm.mi.sthbackend.model.Tournament;
...@@ -70,7 +67,7 @@ public class TournamentController { ...@@ -70,7 +67,7 @@ public class TournamentController {
} }
@PatchMapping @PatchMapping
public UUID determineWinner(@PathVariable UUID tournamentId, public UUID determineWinner(@PathVariable UUID tournamentId,
@PathVariable UUID matchId) throws TournamentIdNotFoundException, MatchIdNotFoundException { @PathVariable UUID matchId) throws TournamentIdNotFoundException, MatchIdNotFoundException, WinnerNotDeterminedException {
return service.determineWinner(tournamentId, matchId); return service.determineWinner(tournamentId, matchId);
} }
......
package hdm.mi.sthbackend.exeptions;
import java.util.UUID;
public class WinnerNotDeterminedException extends Exception{
public WinnerNotDeterminedException(UUID match, UUID tournamentId) {
super(String.format("Winner not determined for Match %s in Tournament %s ", match.toString(), tournamentId.toString()));
}
}
\ No newline at end of file
...@@ -2,10 +2,7 @@ package hdm.mi.sthbackend.service; ...@@ -2,10 +2,7 @@ package hdm.mi.sthbackend.service;
import hdm.mi.sthbackend.dto.PlayerDTO; import hdm.mi.sthbackend.dto.PlayerDTO;
import hdm.mi.sthbackend.dto.TeamDTO; import hdm.mi.sthbackend.dto.TeamDTO;
import hdm.mi.sthbackend.exeptions.MatchIdNotFoundException; import hdm.mi.sthbackend.exeptions.*;
import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException;
import hdm.mi.sthbackend.exeptions.TeamIdNotFoundException;
import hdm.mi.sthbackend.exeptions.TournamentIdNotFoundException;
import hdm.mi.sthbackend.model.Match; import hdm.mi.sthbackend.model.Match;
import hdm.mi.sthbackend.model.Player; import hdm.mi.sthbackend.model.Player;
import hdm.mi.sthbackend.model.Team; import hdm.mi.sthbackend.model.Team;
...@@ -147,7 +144,7 @@ public class TournamentService { ...@@ -147,7 +144,7 @@ public class TournamentService {
return tournament; return tournament;
} }
public UUID determineWinner (UUID tournamentId, UUID matchId) throws TournamentIdNotFoundException, MatchIdNotFoundException{ public UUID determineWinner (UUID tournamentId, UUID matchId) throws TournamentIdNotFoundException, MatchIdNotFoundException, WinnerNotDeterminedException {
Tournament tournament = tournamentRepository.findById(tournamentId) Tournament tournament = tournamentRepository.findById(tournamentId)
.orElseThrow(() -> new TournamentIdNotFoundException(tournamentId)); .orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
...@@ -158,9 +155,9 @@ public class TournamentService { ...@@ -158,9 +155,9 @@ public class TournamentService {
.orElseThrow(() -> new MatchIdNotFoundException(matchId)); .orElseThrow(() -> new MatchIdNotFoundException(matchId));
UUID winnerTeamId = match.getTeamScores().entrySet().stream() UUID winnerTeamId = match.getTeamScores().entrySet().stream()
.max(Comparator.<Map.Entry<UUID, Number>, Double>comparing(entry -> entry.getValue().doubleValue())) .max(Comparator.comparing(Map.Entry::getKey))
.map(Map.Entry::getKey) .map(Map.Entry::getKey)
.orElse(null); .orElseThrow(() -> new WinnerNotDeterminedException(matchId,tournamentId));
match.setWinnerTeamId(winnerTeamId); match.setWinnerTeamId(winnerTeamId);
tournamentRepository.save(tournament); tournamentRepository.save(tournament);
......
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