mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2026-02-06 04:53:25 +00:00
refactor getTeam() to be more universal
This commit is contained in:
@@ -67,14 +67,13 @@ public class RacerRestController {
|
||||
|
||||
@GetMapping("byTeam")
|
||||
public ResponseEntity<@NonNull List<RacerWSTO>> getByTeam(@RequestBody Team requestTeam){
|
||||
UUID teamID = requestTeam.getId();
|
||||
String teamName = requestTeam.getTeamName();
|
||||
Team dbTeam;
|
||||
try {
|
||||
if (teamID != null) dbTeam = teamService.getTeam(teamID);
|
||||
else if (teamName != null) dbTeam = teamService.getTeam(teamName);
|
||||
else return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Must provide either id or name")).build();
|
||||
dbTeam = teamService.getTeam(requestTeam);
|
||||
} catch (TeamNotFoundException e) {
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage())).build();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage())).build();
|
||||
}
|
||||
List<RacerWSTO> returnList = List.copyOf(dbTeam.getMembers().stream().map(RacerWSTO::of).toList());
|
||||
|
||||
@@ -74,19 +74,15 @@ public class TeamRestController {
|
||||
String name = reqTeam.getTeamName();
|
||||
Team actualTeam;
|
||||
try{
|
||||
if(id != null){
|
||||
actualTeam = teamService.getTeam(id); //prefer uuid over name, as it is the most reliable way to find a JPA object
|
||||
}
|
||||
else if (name != null){
|
||||
actualTeam = teamService.getTeam(name);
|
||||
}
|
||||
else return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Must provide either id or name")).build();
|
||||
actualTeam = teamService.getTeam(reqTeam);
|
||||
return new ResponseEntity<>(TeamWSTO.of(actualTeam), HttpStatus.OK);
|
||||
}
|
||||
catch(TeamNotFoundException e){
|
||||
if(id != null) return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,"Team with id "+ id +" not found")).build();
|
||||
if (name != null) return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,"Team with name "+ name +" not found")).build();
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Must provide either id or name.")).build();
|
||||
else return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,"Team with name "+ name +" not found")).build();
|
||||
}
|
||||
catch (IllegalArgumentException e){
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST,e.getMessage())).build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,16 +27,17 @@ public class TeamService {
|
||||
}
|
||||
}
|
||||
|
||||
public Team getTeam(UUID id){
|
||||
Team team = teamRepository.findById(id).orElse(null);
|
||||
if(team == null) throw new TeamNotFoundException("Team with id "+id+" not found");
|
||||
return team;
|
||||
}
|
||||
public Team getTeam(Team team){
|
||||
Team dbTeam;
|
||||
if(team.getId()!=null) dbTeam = teamRepository.findById(team.getId()).orElse(null);
|
||||
else if (team.getTeamName() != null) dbTeam = teamRepository.getTeamByTeamName(team.getTeamName()).orElse(null);
|
||||
else throw new IllegalArgumentException("Must provide either team id or team name");
|
||||
|
||||
public Team getTeam(String name){
|
||||
Team team = teamRepository.getTeamByTeamName(name).orElse(null);
|
||||
if(team == null) throw new TeamNotFoundException("Team with name "+name+" not found");
|
||||
return team;
|
||||
if (dbTeam == null){
|
||||
if(team.getId() != null) throw new TeamNotFoundException("Team with id "+team.getId()+" not found");
|
||||
else throw new TeamNotFoundException("Team with id "+team.getTeamName()+" not found");
|
||||
}
|
||||
return dbTeam;
|
||||
}
|
||||
|
||||
public List<Team> getTeams(){
|
||||
|
||||
Reference in New Issue
Block a user