mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2025-12-22 14:41:57 +00:00
extend endpoints
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
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.TeamNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.services.RacerService;
|
||||
import de.pnreichmuth.timekeep_backend.services.TeamService;
|
||||
import de.pnreichmuth.timekeep_backend.wsto.RacerWSTO;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -9,10 +13,10 @@ 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.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/racers")
|
||||
@@ -21,6 +25,28 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
public class RacerRestController {
|
||||
|
||||
private final RacerService racerService;
|
||||
private final TeamService teamService;
|
||||
// ///////////////////////////////////////////////BEGIN POST MAPPINGS///////////////////////////////////////////////////
|
||||
@PostMapping("createRacer")
|
||||
public ResponseEntity<@NonNull RacerWSTO> createRacer(@RequestBody Racer racer) {
|
||||
try {
|
||||
racerService.updateRacer(racer);
|
||||
return new ResponseEntity<>(RacerWSTO.of(racer), HttpStatus.CREATED);
|
||||
} catch (RacerExistsException e) {
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT,"This racer already exists")).build();
|
||||
}
|
||||
}
|
||||
|
||||
// ///////////////////////////////////////////////BEGIN GET MAPPINGS///////////////////////////////////////////////////
|
||||
@GetMapping("")
|
||||
public ResponseEntity<@NonNull List<RacerWSTO>> getAllRacers() {
|
||||
List<Racer> dbRacers = racerService.getAllRacers();
|
||||
if (dbRacers == null || dbRacers.isEmpty()) return ResponseEntity.of(
|
||||
ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, "Could not find any racers in database"))
|
||||
.build();
|
||||
List<RacerWSTO> returnList = List.copyOf(dbRacers.stream().map(RacerWSTO::of).toList());
|
||||
return ResponseEntity.ok(returnList);
|
||||
}
|
||||
|
||||
@GetMapping("singleRacer")
|
||||
public ResponseEntity<@NonNull RacerWSTO> getSingleRacers(@RequestParam Racer requestRacer){
|
||||
@@ -38,9 +64,23 @@ public class RacerRestController {
|
||||
return ResponseEntity.ok(RacerWSTO.of(actualRacer));
|
||||
}
|
||||
|
||||
// @GetMapping("byTeam")
|
||||
// public ResponseEntity<@NonNull List<Racer>> getByTeam(@RequestParam Team requestTeam){
|
||||
//
|
||||
// }
|
||||
@GetMapping("byTeam")
|
||||
public ResponseEntity<@NonNull List<RacerWSTO>> getByTeam(@RequestParam Team requestTeam){
|
||||
UUID teamID = requestTeam.getId();
|
||||
String teamName = requestTeam.getTeamName();
|
||||
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();
|
||||
} catch (TeamNotFoundException e) {
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage())).build();
|
||||
}
|
||||
List<RacerWSTO> returnList = List.copyOf(dbTeam.getMembers().stream().map(RacerWSTO::of).toList());
|
||||
return ResponseEntity.ok(returnList);
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////BEGIN DELETE MAPPINGS///////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user