mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2025-12-22 14:41:57 +00:00
fix duplicate teams still being allowed
This commit is contained in:
@@ -37,11 +37,13 @@ public class TeamRestController {
|
||||
|
||||
@PostMapping("createTeamDebug")
|
||||
public ResponseEntity<@NonNull TeamWSTO> createTeam(){
|
||||
Team team = teamService.createTeam();
|
||||
try {
|
||||
Team team = teamService.createTeam();
|
||||
team.setTeamName("DEBUG");
|
||||
teamService.updateTeam(team);
|
||||
return new ResponseEntity<>(TeamWSTO.of(team), HttpStatus.CREATED);
|
||||
} catch (TeamExistsException e) {
|
||||
teamService.deleteTeam(team.getId());
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT,"This team already exists")).build();
|
||||
}
|
||||
}
|
||||
@@ -76,8 +78,8 @@ public class TeamRestController {
|
||||
}
|
||||
catch(TeamNotFoundException e){
|
||||
if(id != null) return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,"Team with id "+ id +" not found")).build();
|
||||
else 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.BAD_REQUEST, "Must provide either id or name.")).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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ public class Team {
|
||||
private Map<String, Station> passedStations;
|
||||
|
||||
public Team(){
|
||||
this.teamName = "Team-" + UUID.randomUUID();
|
||||
this.teamName = null;
|
||||
this.firstSemesterTeam = true;
|
||||
this.members = new HashMap<>(4);
|
||||
this.passedStations = new HashMap<>();
|
||||
|
||||
@@ -11,6 +11,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
@SuppressWarnings("LoggingSimilarMessage")
|
||||
@@ -21,6 +22,13 @@ public class TeamService {
|
||||
private final TeamRepository teamRepository;
|
||||
private final RacerRepository racerRepository;
|
||||
|
||||
private void checkTeamIsDuplicate(Team team) throws TeamExistsException {
|
||||
List<Team> teams = teamRepository.findAll();
|
||||
if (teams.stream().anyMatch(dbTeam -> dbTeam.getTeamName().equals(team.getTeamName()) && !dbTeam.getId().equals(team.getId()))) {
|
||||
throw new TeamExistsException("A team by this name already exists.", team);
|
||||
}
|
||||
}
|
||||
|
||||
public Team getTeam(UUID id){
|
||||
return teamRepository.findById(id).orElse(null);
|
||||
}
|
||||
@@ -35,10 +43,12 @@ public class TeamService {
|
||||
return teamRepository.findAll();
|
||||
}
|
||||
|
||||
public Team createTeam(){
|
||||
public Team createTeam() throws TeamExistsException {
|
||||
Team team = new Team();
|
||||
team.setTeamName(String.format("Team %d", teamRepository.count() + 1));
|
||||
Racer racer = new Racer(null,"Paul", "Reichmuth",false,"0");
|
||||
team.addMember(racer);
|
||||
checkTeamIsDuplicate(team);
|
||||
racerRepository.save(racer);
|
||||
teamRepository.save(team);
|
||||
log.info("Team created: {}", team);
|
||||
@@ -46,9 +56,7 @@ public class TeamService {
|
||||
}
|
||||
|
||||
public void createTeam(Team team) throws TeamExistsException{
|
||||
if(teamRepository.existsByTeamName(team.getTeamName())){
|
||||
throw new TeamExistsException("A team by this name already exists.", team);
|
||||
}
|
||||
checkTeamIsDuplicate(team);
|
||||
teamRepository.save(team);
|
||||
log.info("Team created: {}", team);
|
||||
}
|
||||
@@ -56,7 +64,7 @@ public class TeamService {
|
||||
public void createTeam(String name) throws TeamExistsException{
|
||||
Team tempTeam = new Team();
|
||||
tempTeam.setTeamName(name);
|
||||
if(teamRepository.existsByTeamName(tempTeam.getTeamName())) throw new TeamExistsException("A team by this name already exists.", tempTeam);
|
||||
checkTeamIsDuplicate(tempTeam);
|
||||
teamRepository.save(tempTeam);
|
||||
log.info("Team created: {}", tempTeam);
|
||||
}
|
||||
@@ -71,6 +79,12 @@ public class TeamService {
|
||||
teamRepository.deleteTeamByTeamName(name);
|
||||
}
|
||||
|
||||
public void updateTeam(Team team) throws TeamNotFoundException {
|
||||
Objects.requireNonNull(team, "Can't update null team.");
|
||||
checkTeamIsDuplicate(team);
|
||||
racerRepository.saveAll(team.getMembers().values());
|
||||
teamRepository.save(team);
|
||||
}
|
||||
/**
|
||||
* DANGER ZONE
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user