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