refactor pure objects as request body into WSTOs

This commit is contained in:
2026-02-04 21:14:41 +01:00
parent 41fda9770a
commit a87a5a6198
3 changed files with 30 additions and 23 deletions

View File

@@ -8,6 +8,7 @@ import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
import de.pnreichmuth.timekeep_backend.services.RacerService; import de.pnreichmuth.timekeep_backend.services.RacerService;
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.RacerWSTO;
import de.pnreichmuth.timekeep_backend.wsto.TeamWSTO;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -28,7 +29,8 @@ public class RacerRestController {
private final TeamService teamService; private final TeamService teamService;
// ///////////////////////////////////////////////BEGIN POST MAPPINGS/////////////////////////////////////////////////// // ///////////////////////////////////////////////BEGIN POST MAPPINGS///////////////////////////////////////////////////
@PostMapping("createRacer") @PostMapping("createRacer")
public ResponseEntity<@NonNull RacerWSTO> createRacer(@RequestBody Racer racer) { public ResponseEntity<@NonNull RacerWSTO> createRacer(@RequestBody RacerWSTO racerWSTO) {
Racer racer = RacerWSTO.toEntity(racerWSTO);
try { try {
racerService.updateRacer(racer); racerService.updateRacer(racer);
return new ResponseEntity<>(RacerWSTO.of(racer), HttpStatus.CREATED); return new ResponseEntity<>(RacerWSTO.of(racer), HttpStatus.CREATED);
@@ -49,26 +51,24 @@ public class RacerRestController {
} }
@GetMapping("singleRacer") @GetMapping("singleRacer")
public ResponseEntity<@NonNull RacerWSTO> getSingleRacers(@RequestBody Racer requestRacer){ public ResponseEntity<@NonNull RacerWSTO> getSingleRacers(@RequestBody RacerWSTO requestRacerWSTO){
Racer actualRacer = racerService.getRacer(requestRacer); try{
if (actualRacer == null){ Racer actualRacer = racerService.getRacer(RacerWSTO.toEntity(requestRacerWSTO));
return ResponseEntity.of( return ResponseEntity.ok(RacerWSTO.of(actualRacer));
ProblemDetail.forStatusAndDetail( }
HttpStatus.NOT_FOUND, catch (RacerNotFoundException e){
String.format("Racer with name %s %s not found", return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage())).build();
requestRacer.getFirstName(), requestRacer.getLastName() }
) catch (IllegalArgumentException e) {
) return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage())).build();
).build();
} }
return ResponseEntity.ok(RacerWSTO.of(actualRacer));
} }
@GetMapping("byTeam") @GetMapping("byTeam")
public ResponseEntity<@NonNull List<RacerWSTO>> getByTeam(@RequestBody Team requestTeam){ public ResponseEntity<@NonNull List<RacerWSTO>> getByTeam(@RequestBody TeamWSTO requestTeamWSTO){
Team dbTeam; Team dbTeam;
try { try {
dbTeam = teamService.getTeam(requestTeam); dbTeam = teamService.getTeam(TeamWSTO.toEntity(requestTeamWSTO));
} catch (TeamNotFoundException e) { } catch (TeamNotFoundException e) {
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage())).build(); return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage())).build();
} }
@@ -81,7 +81,8 @@ public class RacerRestController {
// ////////////////////////////////////////////////BEGIN DELETE MAPPINGS/////////////////////////////////////////////////// // ////////////////////////////////////////////////BEGIN DELETE MAPPINGS///////////////////////////////////////////////////
@DeleteMapping("/removeRacer") @DeleteMapping("/removeRacer")
public ResponseEntity<?> removeRacer(@RequestBody Racer racer){ public ResponseEntity<?> removeRacer(@RequestBody RacerWSTO racerWSTO){
Racer racer = RacerWSTO.toEntity(racerWSTO);
try { try {
racerService.deleteRacer(racer); racerService.deleteRacer(racer);
return ResponseEntity.ok(racer); return ResponseEntity.ok(racer);

View File

@@ -5,6 +5,7 @@ import de.pnreichmuth.timekeep_backend.entities.Team;
import de.pnreichmuth.timekeep_backend.exceptions.ExistsException; import de.pnreichmuth.timekeep_backend.exceptions.ExistsException;
import de.pnreichmuth.timekeep_backend.exceptions.NotFoundException; 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;
@@ -23,9 +24,10 @@ public class TeamMemberRestController {
// ///////////////////////////////////////////////BEGIN POST MAPPINGS/////////////////////////////////////////////////// // ///////////////////////////////////////////////BEGIN POST MAPPINGS///////////////////////////////////////////////////
@PostMapping("/addMemberToTeam") @PostMapping("/addMemberToTeam")
public ResponseEntity<?> addMemberToTeam(@RequestParam("teamName") String teamName, @RequestBody Racer racer) { public ResponseEntity<?> addMemberToTeam(@RequestParam("teamName") String teamName, @RequestBody RacerWSTO racerWsto) {
Team mockTeam = new Team(); Team mockTeam = new Team();
mockTeam.setTeamName(teamName); mockTeam.setTeamName(teamName);
Racer racer = RacerWSTO.toEntity(racerWsto);
try{ try{
this.teamService.addMember(mockTeam,racer); this.teamService.addMember(mockTeam,racer);
} }
@@ -39,9 +41,10 @@ public class TeamMemberRestController {
} }
// ///////////////////////////////////////////////BEGIN DELETE MAPPINGS/////////////////////////////////////////////////// // ///////////////////////////////////////////////BEGIN DELETE MAPPINGS///////////////////////////////////////////////////
@DeleteMapping("/removeMemberFromTeam") @DeleteMapping("/removeMemberFromTeam")
public ResponseEntity<?> removeMemberFromTeam(@RequestParam("teamName") String teamName, @RequestBody Racer racer) { public ResponseEntity<?> removeMemberFromTeam(@RequestParam("teamName") String teamName, @RequestBody RacerWSTO racerWsto) {
Team mockTeam = new Team(); Team mockTeam = new Team();
mockTeam.setTeamName(teamName); mockTeam.setTeamName(teamName);
Racer racer = RacerWSTO.toEntity(racerWsto);
try{ try{
this.teamService.removeMember(mockTeam,racer); this.teamService.removeMember(mockTeam,racer);
} }

View File

@@ -34,7 +34,8 @@ public class TeamRestController {
* either containing a TeamWSTO if a team was successfully created or a HttpStatus.CONFLICT if the team already existed beforehand * either containing a TeamWSTO if a team was successfully created or a HttpStatus.CONFLICT if the team already existed beforehand
*/ */
@PostMapping("createTeam") @PostMapping("createTeam")
public ResponseEntity<@NonNull TeamWSTO> createTeam(@RequestBody Team team){ public ResponseEntity<@NonNull TeamWSTO> createTeam(@RequestBody TeamWSTO teamwsto){
Team team = TeamWSTO.toEntity(teamwsto);
try { try {
teamService.createTeam(team); teamService.createTeam(team);
return new ResponseEntity<>(TeamWSTO.of(team), HttpStatus.CREATED); return new ResponseEntity<>(TeamWSTO.of(team), HttpStatus.CREATED);
@@ -65,11 +66,12 @@ public class TeamRestController {
/** /**
* Gets all the information pertaining to a given team via the API * Gets all the information pertaining to a given team via the API
* @param reqTeam the team to query all information about with either a name or an id field set * @param reqTeamWSTO the teamWSTO to query all information about with either a name or an id field set
* @return a ResponseEntity containing the team in WSTO form, or a ResponseEntity containing either BAD_REQUEST or NOT_FOUND. * @return a ResponseEntity containing the team in WSTO form, or a ResponseEntity containing either BAD_REQUEST or NOT_FOUND.
*/ */
@GetMapping("single-team") @GetMapping("single-team")
public ResponseEntity<@NonNull TeamWSTO> getSingleTeam(@RequestBody Team reqTeam){ public ResponseEntity<@NonNull TeamWSTO> getSingleTeam(@RequestBody TeamWSTO reqTeamWSTO){
Team reqTeam = TeamWSTO.toEntity(reqTeamWSTO);
UUID id = reqTeam.getId(); UUID id = reqTeam.getId();
String name = reqTeam.getTeamName(); String name = reqTeam.getTeamName();
Team actualTeam; Team actualTeam;
@@ -100,12 +102,13 @@ public class TeamRestController {
/** /**
* Deletes a team via its name or id * Deletes a team via its name or id
* @param team the team to query all information about with either a name or an id field set * @param teamWSTO the teamWSTO to query all information about with either a name or an id field set
* @return a ResponseEntity containing HttpStatus.OK if deletion was successful, * @return a ResponseEntity containing HttpStatus.OK if deletion was successful,
* or a ResponseEntity containing either BAD_REQUEST or NOT_FOUND. * or a ResponseEntity containing either BAD_REQUEST or NOT_FOUND.
*/ */
@DeleteMapping("single-team") @DeleteMapping("single-team")
public ResponseEntity<@NonNull String> deleteTeam(@RequestBody Team team){ public ResponseEntity<@NonNull String> deleteTeam(@RequestBody TeamWSTO teamWSTO){
Team team = TeamWSTO.toEntity(teamWSTO);
String name = team.getTeamName(); String name = team.getTeamName();
UUID id = team.getId(); UUID id = team.getId();
try { try {