From fa044ceb12aac6cdb0c3a7c50e888694a9ccf0e1 Mon Sep 17 00:00:00 2001
From: msiStefan <ss576@hdm-stuttgart.de>
Date: Mon, 8 Jan 2024 19:15:59 +0100
Subject: [PATCH] 08.01.2024 - added WinnerNotDeterminedException and changed
 determineWinner method #24 #25

---
 .../sthbackend/controller/TournamentController.java   |  7 ++-----
 .../exeptions/WinnerNotDeterminedException.java       |  9 +++++++++
 .../hdm/mi/sthbackend/service/TournamentService.java  | 11 ++++-------
 3 files changed, 15 insertions(+), 12 deletions(-)
 create mode 100644 sth-backend/src/main/java/hdm/mi/sthbackend/exeptions/WinnerNotDeterminedException.java

diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/controller/TournamentController.java b/sth-backend/src/main/java/hdm/mi/sthbackend/controller/TournamentController.java
index 0185e76..4428bb1 100644
--- a/sth-backend/src/main/java/hdm/mi/sthbackend/controller/TournamentController.java
+++ b/sth-backend/src/main/java/hdm/mi/sthbackend/controller/TournamentController.java
@@ -2,11 +2,8 @@ package hdm.mi.sthbackend.controller;
 
 
 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.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;
@@ -70,7 +67,7 @@ public class TournamentController {
     }
     @PatchMapping
     public UUID determineWinner(@PathVariable UUID tournamentId,
-                                @PathVariable UUID matchId) throws TournamentIdNotFoundException, MatchIdNotFoundException {
+                                @PathVariable UUID matchId) throws TournamentIdNotFoundException, MatchIdNotFoundException, WinnerNotDeterminedException {
         return service.determineWinner(tournamentId, matchId);
     }
 
diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/exeptions/WinnerNotDeterminedException.java b/sth-backend/src/main/java/hdm/mi/sthbackend/exeptions/WinnerNotDeterminedException.java
new file mode 100644
index 0000000..3c15f2e
--- /dev/null
+++ b/sth-backend/src/main/java/hdm/mi/sthbackend/exeptions/WinnerNotDeterminedException.java
@@ -0,0 +1,9 @@
+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
diff --git a/sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java b/sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java
index 757f74f..531845e 100644
--- a/sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java
+++ b/sth-backend/src/main/java/hdm/mi/sthbackend/service/TournamentService.java
@@ -2,10 +2,7 @@ package hdm.mi.sthbackend.service;
 
 import hdm.mi.sthbackend.dto.PlayerDTO;
 import hdm.mi.sthbackend.dto.TeamDTO;
-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.exeptions.*;
 import hdm.mi.sthbackend.model.Match;
 import hdm.mi.sthbackend.model.Player;
 import hdm.mi.sthbackend.model.Team;
@@ -147,7 +144,7 @@ public class TournamentService {
         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)
                 .orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
@@ -158,9 +155,9 @@ public class TournamentService {
                 .orElseThrow(() -> new MatchIdNotFoundException(matchId));
 
         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)
-                .orElse(null);
+                .orElseThrow(() -> new WinnerNotDeterminedException(matchId,tournamentId));
 
         match.setWinnerTeamId(winnerTeamId);
         tournamentRepository.save(tournament);
-- 
GitLab