mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2026-02-05 20:43:27 +00:00
60 lines
2.8 KiB
Java
60 lines
2.8 KiB
Java
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.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.*;
|
|
|
|
@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 RacerWSTO racerWsto) {
|
|
Team mockTeam = new Team();
|
|
mockTeam.setTeamName(teamName);
|
|
Racer racer = RacerWSTO.toEntity(racerWsto);
|
|
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 RacerWSTO racerWsto) {
|
|
Team mockTeam = new Team();
|
|
mockTeam.setTeamName(teamName);
|
|
Racer racer = RacerWSTO.toEntity(racerWsto);
|
|
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)));
|
|
}
|
|
}
|