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

11.01.2024 - create new endpoints and adjust types and service #24 #22

parent b0e7ce03
No related branches found
No related tags found
1 merge request!7Dev in Main Merge
package hdm.mi.sthbackend.controller;
import hdm.mi.sthbackend.dto.MatchDTO;
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.model.Player;
import hdm.mi.sthbackend.model.Team;
import hdm.mi.sthbackend.model.Match;
import hdm.mi.sthbackend.service.TournamentService;
import hdm.mi.sthbackend.types.TeamMatchScore;
import hdm.mi.sthbackend.types.TeamName;
import hdm.mi.sthbackend.types.TournamentName;
import lombok.AllArgsConstructor;
......@@ -46,7 +47,7 @@ public class TournamentController {
@PostMapping("/tournaments/{tournamentId}/teams")
public TeamDTO createTeam(@PathVariable UUID tournamentId,
@RequestBody TeamName teamName) throws TournamentIdNotFoundException {
@RequestBody TeamName teamName) throws TournamentIdNotFoundException {
return service.createAndAddTeamToTournament(tournamentId, teamName.getName());
}
......@@ -58,7 +59,7 @@ public class TournamentController {
@PostMapping("/teams/{teamId}/addPlayer")
public PlayerDTO addPlayerToTeam(@PathVariable UUID teamId,
@RequestBody String playerName) throws Exception {
@RequestBody String playerName) throws Exception {
return service.addPlayerToTeam(teamId, playerName);
}
......@@ -68,6 +69,26 @@ public class TournamentController {
return service.removePlayerFromTeam(teamId, playerId);
}
@PatchMapping("/teams/{teamId}/updateTeamName")
public TeamDTO updateTeamName(@PathVariable UUID teamId, @RequestBody TeamName newTeamName) throws TeamIdNotFoundException {
return service.updateTeamName(teamId, newTeamName.getName());
}
/**
* Match Endpoints
*/
@PatchMapping("matches/{matchId}/teams/{teamId}/assignTeamToMatch")
public MatchDTO assignTeamToMatch(@PathVariable UUID matchId, @PathVariable UUID teamId, @RequestBody TeamMatchScore score) throws MatchIdNotFoundException {
return service.assignTeamToMatch(matchId, teamId, score.getScore());
}
@PatchMapping("matches/{matchId}/teams/{teamId}/updateScore")
public MatchDTO updateScore(@PathVariable UUID matchId, @PathVariable UUID teamId, @RequestBody TeamMatchScore newScore) throws MatchIdNotFoundException, TeamIdNotFoundException {
return service.updateTeamScore(matchId, teamId, newScore.getScore());
}
@PatchMapping("/match/{matchId}/determineWinner")
public UUID determineWinner(@PathVariable UUID matchId) throws MatchIdNotFoundException, WinnerNotDeterminedException {
return service.determineWinner(matchId);
......@@ -87,6 +108,14 @@ public class TournamentController {
return service.createTournament(params.getName());
}
@DeleteMapping("/tournaments/{tournamentId}")
public TournamentDTO deleteTournament(@PathVariable UUID tournamentId) throws TournamentIdNotFoundException {
return service.deleteTournament(tournamentId);
}
@PatchMapping("/tournaments/{tournamentId}/updateName")
public UUID updateTournamentName(@PathVariable UUID tournamentId, @RequestBody String newTournamentName) throws TournamentIdNotFoundException {
return service.updateTournamentName(tournamentId, newTournamentName);
}
}
package hdm.mi.sthbackend.service;
import hdm.mi.sthbackend.dto.MatchDTO;
import hdm.mi.sthbackend.dto.PlayerDTO;
import hdm.mi.sthbackend.dto.TeamDTO;
import hdm.mi.sthbackend.dto.TournamentDTO;
......@@ -13,6 +14,7 @@ import hdm.mi.sthbackend.repository.IMatchRepository;
import hdm.mi.sthbackend.repository.IPlayerRepository;
import hdm.mi.sthbackend.repository.ITeamRepository;
import hdm.mi.sthbackend.repository.ITournamentRepository;
import hdm.mi.sthbackend.types.TeamMatchScore;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -164,16 +166,17 @@ public class TournamentService {
return winnerTeamId;
}
public void assignTeamToMatch(UUID tournamentId, UUID matchId, UUID teamId, int score) throws TournamentIdNotFoundException, MatchIdNotFoundException {
public MatchDTO assignTeamToMatch(UUID matchId, UUID teamId, int score) throws MatchIdNotFoundException {
Match match = matchRepository.findById(matchId).orElseThrow(() -> new MatchIdNotFoundException(matchId));
match.getTeamScores().put(teamId,score);
matchRepository.save(match);
log.debug("Team " + teamId + " assign to Match " + matchId + " in Tournament " + tournamentId);
log.debug("Team " + teamId + " assign to Match " + matchId );
return mapper.mapToMatchDTO(match);
}
public void updateTeamScore(UUID tournamentId, UUID matchId, UUID teamId, int newScore)throws TournamentIdNotFoundException, MatchIdNotFoundException, TeamIdNotFoundException{
public MatchDTO updateTeamScore(UUID matchId, UUID teamId, int newScore)throws MatchIdNotFoundException, TeamIdNotFoundException{
Match match = matchRepository.findById(matchId).orElseThrow(() -> new MatchIdNotFoundException(matchId));
if (!match.getTeamScores().containsKey(teamId)) {
......@@ -182,16 +185,17 @@ public class TournamentService {
match.getTeamScores().put(teamId, newScore);
matchRepository.save(match);
log.debug("Score of Team " + teamId + " updated to " + newScore);
return mapper.mapToMatchDTO(match);
}
public UUID updateTeamName(UUID teamId, String newTeamName) throws TeamIdNotFoundException {
public TeamDTO updateTeamName(UUID teamId, String newTeamName) throws TeamIdNotFoundException {
Team team = teamRepository.findById(teamId)
.orElseThrow(() -> new TeamIdNotFoundException(teamId));
team.setTeamName(newTeamName);
teamRepository.save(team);
log.debug("Teamname of " + teamId + " updated to " + newTeamName);
return teamId;
return mapper.mapToTeamDTO(team);
}
public UUID updateTournamentName(UUID tournamentId, String newTournamentName)throws TournamentIdNotFoundException{
......@@ -211,12 +215,13 @@ public class TournamentService {
return mapper.mapToTournamentDTO(tournament);
}
public void deleteTournament(UUID tournamentId) throws TournamentIdNotFoundException {
public TournamentDTO deleteTournament(UUID tournamentId) throws TournamentIdNotFoundException {
Tournament tournamentToDelete = tournamentRepository.findById(tournamentId)
.orElseThrow(() -> new TournamentIdNotFoundException(tournamentId));
tournamentRepository.delete(tournamentToDelete);
log.debug("Tournament " + tournamentId + " has been deleted");
return mapper.mapToTournamentDTO(tournamentToDelete);
}
/*
......
package hdm.mi.sthbackend.types;
import lombok.Getter;
@Getter
public class TeamMatchScore {
int score;
}
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