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

20.12.2023 - added TeamIDException, added removeTeam logic #25 #24 #22

parent 292c6d62
No related branches found
No related tags found
1 merge request!7Dev in Main Merge
...@@ -2,6 +2,7 @@ package hdm.mi.sthbackend.controller; ...@@ -2,6 +2,7 @@ package hdm.mi.sthbackend.controller;
import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException; import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException;
import hdm.mi.sthbackend.exeptions.TeamIdNotFoundException;
import hdm.mi.sthbackend.exeptions.TournamentIdNotFoundException; 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;
...@@ -36,8 +37,13 @@ public class TournamentController { ...@@ -36,8 +37,13 @@ public class TournamentController {
@PostMapping("/{tournamentId}/teams") @PostMapping("/{tournamentId}/teams")
public Team createTeam(@PathVariable UUID tournamentId, public Team createTeam(@PathVariable UUID tournamentId,
@RequestBody String teamName) throws TournamentIdNotFoundException { @RequestBody String teamName) throws TournamentIdNotFoundException {
return service.createTeam(tournamentId, teamName); return service.createTeamFromTournament(tournamentId, teamName);
} }
@DeleteMapping("/{tournamentId}/teams/{teamId}")
public UUID removeTeamFromTournament(@PathVariable UUID tournamentId,
@PathVariable UUID teamId) throws TournamentIdNotFoundException, TeamIdNotFoundException{
return service.removeTeamFromTournament(tournamentId, teamId);
};
@PostMapping("/{tournamentId}/teams/{teamId}/addPlayer") @PostMapping("/{tournamentId}/teams/{teamId}/addPlayer")
public Player addPlayerToTeam(@PathVariable UUID tournamentId, public Player addPlayerToTeam(@PathVariable UUID tournamentId,
...@@ -54,6 +60,7 @@ public class TournamentController { ...@@ -54,6 +60,7 @@ public class TournamentController {
} }
/** /**
* Tournament Endpoints * Tournament Endpoints
*/ */
......
package hdm.mi.sthbackend.exeptions;
import java.util.UUID;
public class TeamIdNotFoundException extends Exception{
public TeamIdNotFoundException(UUID teamId) {
super(String.format("TeamId %s not found", teamId.toString()));
}
}
...@@ -24,6 +24,7 @@ public class Match { ...@@ -24,6 +24,7 @@ public class Match {
private UUID matchId; private UUID matchId;
private List<TeamMatchScore> teamScores; private List<TeamMatchScore> teamScores;
// todo problem mit doppelten gespeicherten daten? Eher nur UUID nutzen für winner
@Setter @Setter
private Team winner; private Team winner;
......
...@@ -2,6 +2,7 @@ package hdm.mi.sthbackend.service; ...@@ -2,6 +2,7 @@ package hdm.mi.sthbackend.service;
import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClient;
import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException; import hdm.mi.sthbackend.exeptions.PlayerIdNotFoundException;
import hdm.mi.sthbackend.exeptions.TeamIdNotFoundException;
import hdm.mi.sthbackend.exeptions.TournamentIdNotFoundException; 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;
...@@ -11,10 +12,7 @@ import hdm.mi.sthbackend.repository.MongoManager; ...@@ -11,10 +12,7 @@ import hdm.mi.sthbackend.repository.MongoManager;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
@Service @Service
@AllArgsConstructor @AllArgsConstructor
...@@ -79,7 +77,7 @@ public class TournamentService { ...@@ -79,7 +77,7 @@ public class TournamentService {
return playerId; return playerId;
} }
public Team createTeam(UUID tournamentId, String teamName) throws TournamentIdNotFoundException { public Team createTeamFromTournament(UUID tournamentId, String teamName) throws TournamentIdNotFoundException {
Tournament tournament = tournamentRepository.findById(tournamentId) Tournament tournament = tournamentRepository.findById(tournamentId)
.orElseThrow(() -> new TournamentIdNotFoundException(tournamentId)); .orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
...@@ -93,6 +91,15 @@ public class TournamentService { ...@@ -93,6 +91,15 @@ public class TournamentService {
return team; return team;
} }
public UUID removeTeamFromTournament(UUID tournamentId, UUID teamId) throws TournamentIdNotFoundException, TeamIdNotFoundException {
Tournament tournament = tournamentRepository.findById(tournamentId)
.orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
Optional.ofNullable(tournament.getTeams().remove(teamId)).orElseThrow(() -> new TeamIdNotFoundException(tournamentId));
tournamentRepository.save(tournament);
return teamId;
}
public Tournament createTournament(String tournamentName) { public Tournament createTournament(String tournamentName) {
MongoManager manager = new MongoManager(); MongoManager manager = new MongoManager();
MongoClient client = manager.getMongoClient(); MongoClient client = manager.getMongoClient();
......
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