mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2025-12-22 14:41:57 +00:00
add TeamExists throw when team already in DB
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package de.pnreichmuth.timekeep_backend.services;
|
||||
|
||||
import de.pnreichmuth.timekeep_backend.entities.Racer;
|
||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamExistsException;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.repositories.RacerRepository;
|
||||
import de.pnreichmuth.timekeep_backend.repositories.TeamRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -17,6 +19,7 @@ import java.util.UUID;
|
||||
@RequiredArgsConstructor
|
||||
public class TeamService {
|
||||
private final TeamRepository teamRepository;
|
||||
private final RacerRepository racerRepository;
|
||||
|
||||
public Team getTeam(UUID id){
|
||||
return teamRepository.findById(id).orElse(null);
|
||||
@@ -32,13 +35,17 @@ public class TeamService {
|
||||
return teamRepository.findAll();
|
||||
}
|
||||
|
||||
public void createTeam(){
|
||||
public Team createTeam(){
|
||||
Team team = new Team();
|
||||
Racer racer = new Racer(null,"Paul", "Reichmuth",false,"0");
|
||||
team.addMember(racer);
|
||||
racerRepository.save(racer);
|
||||
teamRepository.save(team);
|
||||
log.info("Team created: {}", team);
|
||||
return team;
|
||||
}
|
||||
|
||||
public void createTeam(Team team){
|
||||
public void createTeam(Team team) throws TeamExistsException{
|
||||
if(teamRepository.existsByTeamName(team.getTeamName())){
|
||||
throw new TeamExistsException("A team by this name already exists.", team);
|
||||
}
|
||||
@@ -46,9 +53,10 @@ public class TeamService {
|
||||
log.info("Team created: {}", team);
|
||||
}
|
||||
|
||||
public void createTeam(String name){
|
||||
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);
|
||||
teamRepository.save(tempTeam);
|
||||
log.info("Team created: {}", tempTeam);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user