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")
|
@GetMapping("byTeam")
|
||||||
public ResponseEntity<@NonNull List<RacerWSTO>> getByTeam(@RequestBody Team requestTeam){
|
public ResponseEntity<@NonNull List<RacerWSTO>> getByTeam(@RequestBody Team requestTeam){
|
||||||
UUID teamID = requestTeam.getId();
|
|
||||||
String teamName = requestTeam.getTeamName();
|
|
||||||
Team dbTeam;
|
Team dbTeam;
|
||||||
try {
|
try {
|
||||||
if (teamID != null) dbTeam = teamService.getTeam(teamID);
|
dbTeam = teamService.getTeam(requestTeam);
|
||||||
else if (teamName != null) dbTeam = teamService.getTeam(teamName);
|
|
||||||
else return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Must provide either id or name")).build();
|
|
||||||
} catch (TeamNotFoundException e) {
|
} 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();
|
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage())).build();
|
||||||
}
|
}
|
||||||
List<RacerWSTO> returnList = List.copyOf(dbTeam.getMembers().stream().map(RacerWSTO::of).toList());
|
List<RacerWSTO> returnList = List.copyOf(dbTeam.getMembers().stream().map(RacerWSTO::of).toList());
|
||||||
|
|||||||
@@ -74,19 +74,15 @@ public class TeamRestController {
|
|||||||
String name = reqTeam.getTeamName();
|
String name = reqTeam.getTeamName();
|
||||||
Team actualTeam;
|
Team actualTeam;
|
||||||
try{
|
try{
|
||||||
if(id != null){
|
actualTeam = teamService.getTeam(reqTeam);
|
||||||
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();
|
|
||||||
return new ResponseEntity<>(TeamWSTO.of(actualTeam), HttpStatus.OK);
|
return new ResponseEntity<>(TeamWSTO.of(actualTeam), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
catch(TeamNotFoundException e){
|
catch(TeamNotFoundException e){
|
||||||
if(id != null) return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,"Team with id "+ id +" not found")).build();
|
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();
|
else 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();
|
}
|
||||||
|
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){
|
public Team getTeam(Team team){
|
||||||
Team team = teamRepository.findById(id).orElse(null);
|
Team dbTeam;
|
||||||
if(team == null) throw new TeamNotFoundException("Team with id "+id+" not found");
|
if(team.getId()!=null) dbTeam = teamRepository.findById(team.getId()).orElse(null);
|
||||||
return team;
|
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){
|
if (dbTeam == null){
|
||||||
Team team = teamRepository.getTeamByTeamName(name).orElse(null);
|
if(team.getId() != null) throw new TeamNotFoundException("Team with id "+team.getId()+" not found");
|
||||||
if(team == null) throw new TeamNotFoundException("Team with name "+name+" not found");
|
else throw new TeamNotFoundException("Team with id "+team.getTeamName()+" not found");
|
||||||
return team;
|
}
|
||||||
|
return dbTeam;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Team> getTeams(){
|
public List<Team> getTeams(){
|
||||||
|
|||||||
Reference in New Issue
Block a user