mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2026-02-06 04:53:25 +00:00
Compare commits
16 Commits
5280ebd764
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| b55f0ca6bf | |||
| a87a5a6198 | |||
| 41fda9770a | |||
| a90d518fc3 | |||
| 3bde1d9228 | |||
| 43e8a24ec1 | |||
| 9396800850 | |||
| 935a6975a2 | |||
| b9d4943ba4 | |||
| 0a65a47749 | |||
| 43ccc44db8 | |||
| 9bad984751 | |||
| 600b9f062a | |||
| fca00d8ff4 | |||
| d93650409a | |||
| 568bdcb470 |
10
.run/Database Container.run.xml
Normal file
10
.run/Database Container.run.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Database Container" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
|
||||
<deployment type="docker-compose.yml">
|
||||
<settings>
|
||||
<option name="sourceFilePath" value="src/main/resources/compose.yaml" />
|
||||
</settings>
|
||||
</deployment>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
8
.run/Set up DB.run.xml
Normal file
8
.run/Set up DB.run.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Set up DB" type="DatabaseScript">
|
||||
<script-file value="$PROJECT_DIR$/build/generated/sources/annotationProcessor/java/main/ddl_Team.sql" />
|
||||
<script-mode>FILE</script-mode>
|
||||
<data-source id="0d777779-ef1b-44ea-a12b-5ae628beebd4" namespace="schema/"jpa"" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -11,6 +11,7 @@
|
||||
<method v="2">
|
||||
<option name="RunConfigurationTask" enabled="false" run_configuration_name="Database Container" run_configuration_type="docker-deploy" />
|
||||
<option name="Make" enabled="true" />
|
||||
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Set up DB" run_configuration_type="DatabaseScript" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -3,6 +3,7 @@ package de.pnreichmuth.timekeep_backend.controllers;
|
||||
import de.pnreichmuth.timekeep_backend.entities.Racer;
|
||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.RacerExistsException;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.RacerNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.services.RacerService;
|
||||
import de.pnreichmuth.timekeep_backend.services.TeamService;
|
||||
@@ -16,7 +17,6 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/racers")
|
||||
@@ -49,8 +49,8 @@ public class RacerRestController {
|
||||
}
|
||||
|
||||
@GetMapping("singleRacer")
|
||||
public ResponseEntity<@NonNull RacerWSTO> getSingleRacers(@RequestParam Racer requestRacer){
|
||||
Racer actualRacer = racerService.getRacer(requestRacer.getFirstName(), requestRacer.getLastName());
|
||||
public ResponseEntity<@NonNull RacerWSTO> getSingleRacers(@RequestBody Racer requestRacer){
|
||||
Racer actualRacer = racerService.getRacer(requestRacer);
|
||||
if (actualRacer == null){
|
||||
return ResponseEntity.of(
|
||||
ProblemDetail.forStatusAndDetail(
|
||||
@@ -65,15 +65,14 @@ public class RacerRestController {
|
||||
}
|
||||
|
||||
@GetMapping("byTeam")
|
||||
public ResponseEntity<@NonNull List<RacerWSTO>> getByTeam(@RequestParam Team requestTeam){
|
||||
UUID teamID = requestTeam.getId();
|
||||
String teamName = requestTeam.getTeamName();
|
||||
public ResponseEntity<@NonNull List<RacerWSTO>> getByTeam(@RequestBody Team requestTeam){
|
||||
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());
|
||||
@@ -81,6 +80,17 @@ public class RacerRestController {
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////BEGIN DELETE MAPPINGS///////////////////////////////////////////////////
|
||||
|
||||
@DeleteMapping("/removeRacer")
|
||||
public ResponseEntity<?> removeRacer(@RequestBody Racer racer){
|
||||
try {
|
||||
racerService.deleteRacer(racer);
|
||||
return ResponseEntity.ok(racer);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage())).build();
|
||||
} catch (RacerNotFoundException e) {
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage())).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package de.pnreichmuth.timekeep_backend.controllers;
|
||||
|
||||
import de.pnreichmuth.timekeep_backend.entities.Racer;
|
||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.ExistsException;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.NotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.services.TeamService;
|
||||
import de.pnreichmuth.timekeep_backend.wsto.TeamWSTO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ProblemDetail;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/members")
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class TeamMemberRestController {
|
||||
private final TeamService teamService;
|
||||
// ///////////////////////////////////////////////BEGIN GET MAPPINGS///////////////////////////////////////////////////
|
||||
|
||||
// ///////////////////////////////////////////////BEGIN POST MAPPINGS///////////////////////////////////////////////////
|
||||
@PostMapping("/addMemberToTeam")
|
||||
public ResponseEntity<?> addMemberToTeam(@RequestParam("teamName") String teamName, @RequestBody Racer racer) {
|
||||
Team mockTeam = new Team();
|
||||
mockTeam.setTeamName(teamName);
|
||||
try{
|
||||
this.teamService.addMember(mockTeam,racer);
|
||||
}
|
||||
catch(NotFoundException e){
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage())).build();
|
||||
}
|
||||
catch (ExistsException e){
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage())).build();
|
||||
}
|
||||
return ResponseEntity.ok(TeamWSTO.of(teamService.getTeam(mockTeam)));
|
||||
}
|
||||
// ///////////////////////////////////////////////BEGIN DELETE MAPPINGS///////////////////////////////////////////////////
|
||||
@DeleteMapping("/removeMemberFromTeam")
|
||||
public ResponseEntity<?> removeMemberFromTeam(@RequestParam("teamName") String teamName, @RequestBody Racer racer) {
|
||||
Team mockTeam = new Team();
|
||||
mockTeam.setTeamName(teamName);
|
||||
try{
|
||||
this.teamService.removeMember(mockTeam,racer);
|
||||
}
|
||||
catch(NotFoundException e){
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage())).build();
|
||||
}
|
||||
catch (ExistsException e){
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage())).build();
|
||||
}
|
||||
return ResponseEntity.ok(TeamWSTO.of(teamService.getTeam(mockTeam)));
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public class TeamRestController {
|
||||
|
||||
/**
|
||||
* Creates a team with the information provided via the /teams/createTeam endpoint
|
||||
* @param team
|
||||
* @param team Team object with the information to be used on creation
|
||||
* @return a ResponseEntity,
|
||||
* either containing a TeamWSTO if a team was successfully created or a HttpStatus.CONFLICT if the team already existed beforehand
|
||||
*/
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Station {
|
||||
private Set<Team> passedTeams;
|
||||
|
||||
@ElementCollection
|
||||
private Map<Team, LocalDate> passingTimes;
|
||||
private Map<UUID, LocalDate> passingTimes;
|
||||
|
||||
public Station(String name, String location){
|
||||
this.name = name;
|
||||
@@ -40,13 +40,13 @@ public class Station {
|
||||
if(!passedTeams.add(team)){
|
||||
throw new TeamExistsException("Team %s was already seen at this Station: %s".formatted(team.getTeamName(), this.location), team);
|
||||
}
|
||||
passingTimes.put(team, LocalDate.now());
|
||||
passingTimes.put(team.getId(), LocalDate.now());
|
||||
}
|
||||
|
||||
public void removePassedTeam(Team team){
|
||||
if (!passedTeams.contains(team)) throw new TeamNotFoundException("Team %s was never seen at this station: %s".formatted(team.getTeamName(), this.location));
|
||||
passedTeams.remove(team);
|
||||
passingTimes.remove(team);
|
||||
passingTimes.remove(team.getId());
|
||||
}
|
||||
|
||||
public String setPasswordHash(String passwordHash){
|
||||
|
||||
@@ -68,13 +68,11 @@ public class Team {
|
||||
|
||||
/**
|
||||
* Removes a member from the team by name
|
||||
* @param firstName the first name of the racer to be removed
|
||||
* @param lastName the last name of the racer to be removed
|
||||
* @param killableMember the Racer object to be removed from the team
|
||||
*/
|
||||
public void removeMember(String firstName, String lastName){
|
||||
this.members.forEach(racer ->{
|
||||
if(firstName.equals(racer.getFirstName()) && lastName.equals(racer.getLastName())) members.remove(racer);
|
||||
});
|
||||
public void removeMember(Racer killableMember){
|
||||
this.members.remove(killableMember);
|
||||
killableMember.setMemberTeam(null);
|
||||
this.checkFirstSemesterTeam();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package de.pnreichmuth.timekeep_backend.exceptions;
|
||||
|
||||
public class ExistsException extends RuntimeException {
|
||||
public ExistsException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package de.pnreichmuth.timekeep_backend.exceptions;
|
||||
|
||||
public class NotFoundException extends RuntimeException {
|
||||
public NotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
package de.pnreichmuth.timekeep_backend.exceptions;
|
||||
|
||||
import de.pnreichmuth.timekeep_backend.entities.Racer;
|
||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class RacerExistsException extends RuntimeException {
|
||||
public class RacerExistsException extends ExistsException {
|
||||
public RacerExistsException(String message, Racer racer) {
|
||||
super(message);
|
||||
log.warn(message, racer.getFirstName(), racer.getLastName());
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package de.pnreichmuth.timekeep_backend.exceptions;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@Slf4j
|
||||
public class RacerNotFoundException extends RuntimeException {
|
||||
public class RacerNotFoundException extends NotFoundException {
|
||||
public RacerNotFoundException(String message) {
|
||||
super(message);
|
||||
log.error(message);
|
||||
|
||||
@@ -4,7 +4,7 @@ import de.pnreichmuth.timekeep_backend.entities.Station;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class StationExistsException extends RuntimeException {
|
||||
public class StationExistsException extends ExistsException {
|
||||
public StationExistsException(String message, Station station) {
|
||||
super(message);
|
||||
log.warn(message, station.getName(), station.getLocation());
|
||||
|
||||
@@ -3,7 +3,7 @@ package de.pnreichmuth.timekeep_backend.exceptions;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class StationNotFoundException extends RuntimeException {
|
||||
public class StationNotFoundException extends NotFoundException {
|
||||
public StationNotFoundException(String message) {
|
||||
super(message);
|
||||
log.error(message);
|
||||
|
||||
@@ -2,11 +2,9 @@ package de.pnreichmuth.timekeep_backend.exceptions;
|
||||
|
||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@Slf4j
|
||||
public class TeamExistsException extends RuntimeException {
|
||||
public class TeamExistsException extends ExistsException {
|
||||
public TeamExistsException(String message, Team team) {
|
||||
super(message);
|
||||
log.warn(message, team.getTeamName());
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package de.pnreichmuth.timekeep_backend.exceptions;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@Slf4j
|
||||
public class TeamNotFoundException extends RuntimeException {
|
||||
public class TeamNotFoundException extends NotFoundException {
|
||||
public TeamNotFoundException(String message) {
|
||||
super(message);
|
||||
log.error(message);
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package de.pnreichmuth.timekeep_backend.repositories;
|
||||
|
||||
import de.pnreichmuth.timekeep_backend.entities.Racer;
|
||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
||||
import lombok.NonNull;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.pnreichmuth.timekeep_backend.services;
|
||||
import de.pnreichmuth.timekeep_backend.entities.Racer;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.RacerExistsException;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.RacerNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.repositories.RacerRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -52,6 +53,18 @@ public class RacerService {
|
||||
return racer;
|
||||
}
|
||||
|
||||
public Racer getRacer(Racer racer) throws RacerNotFoundException {
|
||||
Racer dbRacer;
|
||||
if(racer.getRacerID() != null) dbRacer = racerRepository.getRacerByRacerID(racer.getRacerID()).orElse(null);
|
||||
else if(racer.getFirstName() != null && racer.getLastName() != null) dbRacer = racerRepository.getRacerByFirstNameAndLastName(racer.getFirstName(),racer.getLastName()).orElse(null);
|
||||
else throw new IllegalArgumentException("Must provide either Racer ID or full Racer name");
|
||||
if (dbRacer == null){
|
||||
if(racer.getRacerID() != null) throw new TeamNotFoundException("Racer with id "+racer.getRacerID()+" not found");
|
||||
else throw new TeamNotFoundException("Racer with name "+racer.getFirstName()+" "+ racer.getLastName()+" not found");
|
||||
}
|
||||
return dbRacer;
|
||||
}
|
||||
|
||||
public UUID getRacerIdByName(String firstName, String lastName) throws RacerNotFoundException {
|
||||
Racer racer = racerRepository.getRacerByFirstNameAndLastName(firstName,lastName).orElse(null);
|
||||
if(racer == null) throw new RacerNotFoundException(String.format("Racer with name %s %s not found", firstName, lastName));
|
||||
@@ -72,9 +85,13 @@ public class RacerService {
|
||||
return racerRepository.save(racer);
|
||||
}
|
||||
|
||||
public void deleteRacer(UUID id) throws RacerNotFoundException {
|
||||
Racer racer = racerRepository.findById(id).orElse(null);
|
||||
if(racer == null) throw new RacerNotFoundException("Racer not found");
|
||||
public void deleteRacer(Racer racer) throws RacerNotFoundException {
|
||||
Racer dbRacer;
|
||||
if(racer.getRacerID() != null) dbRacer = racerRepository.getRacerByRacerID(racer.getRacerID()).orElse(null);
|
||||
else if(racer.getFirstName() != null && racer.getLastName() != null) dbRacer = racerRepository.getRacerByFirstNameAndLastName(racer.getFirstName(),racer.getLastName()).orElse(null);
|
||||
else throw new IllegalArgumentException("Must provide either Racer ID or full Racer name");
|
||||
if(dbRacer == null) throw new RacerNotFoundException("Racer not found");
|
||||
racerRepository.delete(dbRacer);
|
||||
}
|
||||
|
||||
public void updateRacer(Racer racer) throws RacerExistsException{
|
||||
|
||||
@@ -9,7 +9,6 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
|
||||
@@ -1,9 +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.RacerExistsException;
|
||||
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;
|
||||
@@ -19,6 +20,7 @@ import java.util.UUID;
|
||||
@RequiredArgsConstructor
|
||||
public class TeamService {
|
||||
private final TeamRepository teamRepository;
|
||||
private final RacerService racerService;
|
||||
|
||||
private void checkTeamIsDuplicate(Team team) throws TeamExistsException {
|
||||
List<Team> teams = teamRepository.findAll();
|
||||
@@ -27,16 +29,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 name "+team.getTeamName()+" not found");
|
||||
}
|
||||
return dbTeam;
|
||||
}
|
||||
|
||||
public List<Team> getTeams(){
|
||||
@@ -69,9 +72,33 @@ public class TeamService {
|
||||
|
||||
public void updateTeam(Team team) throws TeamNotFoundException {
|
||||
Objects.requireNonNull(team, "Can't update null team.");
|
||||
checkTeamIsDuplicate(team);
|
||||
teamRepository.save(team);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new member to the given team
|
||||
* @param team team object to add the member to
|
||||
* @param newMember racer object to be added as a member
|
||||
*/
|
||||
public void addMember(Team team,Racer newMember){
|
||||
Team dbTeam = this.getTeam(team);
|
||||
Racer dbRacer = racerService.getRacer(newMember);
|
||||
if(dbTeam.getMembers().stream().anyMatch(e -> e.equals(dbRacer))){
|
||||
throw new RacerExistsException("%s %s is already a member".formatted(dbRacer.getFirstName(),dbRacer.getLastName()), newMember);
|
||||
}
|
||||
dbTeam.addMember(dbRacer);
|
||||
this.updateTeam(dbTeam);
|
||||
}
|
||||
|
||||
public void removeMember(Team team, Racer newMember){
|
||||
Team dbTeam = this.getTeam(team);
|
||||
Racer dbRacer = racerService.getRacer(newMember);
|
||||
if(dbTeam.getMembers().stream().noneMatch(e -> e.equals(dbRacer))){
|
||||
throw new RacerExistsException("%s %s is not a member".formatted(dbRacer.getFirstName(),dbRacer.getLastName()), newMember);
|
||||
}
|
||||
dbTeam.removeMember(dbRacer);
|
||||
this.updateTeam(dbTeam);
|
||||
}
|
||||
/**
|
||||
* DANGER ZONE
|
||||
*/
|
||||
|
||||
@@ -8,7 +8,7 @@ package de.pnreichmuth.timekeep_backend.spring_configs;
|
||||
//
|
||||
//
|
||||
//@Configuration
|
||||
////@EnableWebSecurity
|
||||
//@EnableWebSecurity
|
||||
//public class TimekeepBackendSecurityConfig {
|
||||
// @Bean
|
||||
// public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
|
||||
@@ -20,14 +20,10 @@ public record TeamWSTO(String teamName,
|
||||
|
||||
public static TeamWSTO of(Team team){
|
||||
List<RacerWSTO> tempMemberList = new ArrayList<>();
|
||||
team.getMembers().forEach(member -> {
|
||||
tempMemberList.add(RacerWSTO.of(member));
|
||||
});
|
||||
team.getMembers().forEach(member -> tempMemberList.add(RacerWSTO.of(member)));
|
||||
|
||||
List<StationWSTO> tempStationList = new ArrayList<>();
|
||||
team.getPassedStations().forEach(passedStation -> {
|
||||
tempStationList.add(StationWSTO.of(passedStation));
|
||||
});
|
||||
team.getPassedStations().forEach(passedStation -> tempStationList.add(StationWSTO.of(passedStation)));
|
||||
|
||||
return new TeamWSTO(
|
||||
team.getTeamName(),
|
||||
@@ -39,14 +35,10 @@ public record TeamWSTO(String teamName,
|
||||
|
||||
public static Team toEntity(TeamWSTO teamWSTO){
|
||||
List<Racer> tempMemberList = new ArrayList<>();
|
||||
teamWSTO.teamMembers().forEach(member -> {
|
||||
tempMemberList.add(RacerWSTO.toEntity(member));
|
||||
});
|
||||
teamWSTO.teamMembers().forEach(member -> tempMemberList.add(RacerWSTO.toEntity(member)));
|
||||
|
||||
List<Station> tempStationList = new ArrayList<>();
|
||||
teamWSTO.passedStations().forEach(passedStation -> {
|
||||
tempStationList.add(StationWSTO.toEntity(passedStation));
|
||||
});
|
||||
teamWSTO.passedStations().forEach(passedStation -> tempStationList.add(StationWSTO.toEntity(passedStation)));
|
||||
|
||||
return new Team(
|
||||
teamWSTO.teamName(),
|
||||
|
||||
Reference in New Issue
Block a user