mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2026-02-06 04:53:25 +00:00
added Mappings
This commit is contained in:
@@ -2,12 +2,14 @@ package de.pnreichmuth.timekeep_backend.controllers;
|
|||||||
|
|
||||||
import de.pnreichmuth.timekeep_backend.entities.Racer;
|
import de.pnreichmuth.timekeep_backend.entities.Racer;
|
||||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
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.services.TeamService;
|
||||||
import de.pnreichmuth.timekeep_backend.wsto.RacerWSTO;
|
|
||||||
import de.pnreichmuth.timekeep_backend.wsto.TeamWSTO;
|
import de.pnreichmuth.timekeep_backend.wsto.TeamWSTO;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ProblemDetail;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@@ -17,8 +19,38 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class TeamMemberRestController {
|
public class TeamMemberRestController {
|
||||||
private final TeamService teamService;
|
private final TeamService teamService;
|
||||||
private final RacerService racerService;
|
|
||||||
// ///////////////////////////////////////////////BEGIN GET MAPPINGS///////////////////////////////////////////////////
|
// ///////////////////////////////////////////////BEGIN GET MAPPINGS///////////////////////////////////////////////////
|
||||||
|
|
||||||
// ///////////////////////////////////////////////BEGIN POST 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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package de.pnreichmuth.timekeep_backend.services;
|
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.entities.Team;
|
||||||
|
import de.pnreichmuth.timekeep_backend.exceptions.RacerExistsException;
|
||||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamExistsException;
|
import de.pnreichmuth.timekeep_backend.exceptions.TeamExistsException;
|
||||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
|
import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
|
||||||
import de.pnreichmuth.timekeep_backend.repositories.RacerRepository;
|
|
||||||
import de.pnreichmuth.timekeep_backend.repositories.TeamRepository;
|
import de.pnreichmuth.timekeep_backend.repositories.TeamRepository;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -19,6 +20,7 @@ import java.util.UUID;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class TeamService {
|
public class TeamService {
|
||||||
private final TeamRepository teamRepository;
|
private final TeamRepository teamRepository;
|
||||||
|
private final RacerService racerService;
|
||||||
|
|
||||||
private void checkTeamIsDuplicate(Team team) throws TeamExistsException {
|
private void checkTeamIsDuplicate(Team team) throws TeamExistsException {
|
||||||
List<Team> teams = teamRepository.findAll();
|
List<Team> teams = teamRepository.findAll();
|
||||||
@@ -70,9 +72,33 @@ public class TeamService {
|
|||||||
|
|
||||||
public void updateTeam(Team team) throws TeamNotFoundException {
|
public void updateTeam(Team team) throws TeamNotFoundException {
|
||||||
Objects.requireNonNull(team, "Can't update null team.");
|
Objects.requireNonNull(team, "Can't update null team.");
|
||||||
checkTeamIsDuplicate(team);
|
|
||||||
teamRepository.save(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
|
* DANGER ZONE
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user