added Mappings

This commit is contained in:
2026-02-04 20:53:02 +01:00
parent 935a6975a2
commit 9396800850
2 changed files with 63 additions and 5 deletions

View File

@@ -2,12 +2,14 @@ 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.services.RacerService;
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.RacerWSTO;
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.*;
@@ -17,8 +19,38 @@ import org.springframework.web.bind.annotation.*;
@RequiredArgsConstructor
public class TeamMemberRestController {
private final TeamService teamService;
private final RacerService racerService;
// ///////////////////////////////////////////////BEGIN GET MAPPINGS///////////////////////////////////////////////////
// ///////////////////////////////////////////////BEGIN POST MAPPINGS///////////////////////////////////////////////////
@PostMapping("/addMemberToTeam")
public <T> 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 <T> 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)));
}
}

View File

@@ -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();
@@ -70,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
*/
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
*/