mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2025-12-22 14:41:57 +00:00
develop controllers
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
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.services.TeamService;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ProblemDetail;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/racer")
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class RacerRestController {
|
||||
|
||||
private final RacerService racerService;
|
||||
private final TeamService teamService;
|
||||
|
||||
@GetMapping("singleRacer")
|
||||
public ResponseEntity<Racer> getSingleRacers(@RequestParam Racer requestRacer){
|
||||
Racer actualRacer = racerService.getRacer(requestRacer.getFirstName(), requestRacer.getLastName());
|
||||
if (actualRacer == null){
|
||||
return ResponseEntity.of(
|
||||
ProblemDetail.forStatusAndDetail(
|
||||
HttpStatus.NOT_FOUND,
|
||||
String.format("Racer with name %s %s not found",
|
||||
requestRacer.getFirstName(), requestRacer.getLastName()
|
||||
)
|
||||
)
|
||||
).build();
|
||||
}
|
||||
return ResponseEntity.ok(actualRacer);
|
||||
}
|
||||
|
||||
// @GetMapping("byTeam")
|
||||
// public ResponseEntity<@NonNull List<Racer>> getByTeam(@RequestParam Team requestTeam){
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,45 +1,103 @@
|
||||
package de.pnreichmuth.timekeep_backend.controllers;
|
||||
|
||||
import de.pnreichmuth.timekeep_backend.entities.Team;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamExistsException;
|
||||
import de.pnreichmuth.timekeep_backend.exceptions.TeamNotFoundException;
|
||||
import de.pnreichmuth.timekeep_backend.services.TeamService;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.antlr.v4.runtime.misc.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ProblemDetail;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/teams")
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class TeamRestController {
|
||||
|
||||
@Autowired
|
||||
TeamService teamService;
|
||||
private final TeamService teamService;
|
||||
|
||||
@PostMapping("createTeam")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public Team createTeam(@RequestBody Team team){
|
||||
teamService.createTeam(team);
|
||||
return team;
|
||||
public ResponseEntity<Team> createTeam(@RequestBody Team team){
|
||||
try {
|
||||
teamService.createTeam(team);
|
||||
return new ResponseEntity<>(team, HttpStatus.CREATED);
|
||||
} catch (TeamExistsException e) {
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT,"This team already exists")).build();
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("createTeamDebug")
|
||||
public ResponseEntity<Team> createTeam(){
|
||||
try {
|
||||
Team team = teamService.createTeam();
|
||||
return new ResponseEntity<>(team, HttpStatus.CREATED);
|
||||
} catch (TeamExistsException e) {
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT,"This team already exists")).build();
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("all")
|
||||
public List<Team> findAllTeams(){
|
||||
return teamService.getTeams();
|
||||
public ResponseEntity<@NonNull List<Team>> getAllTeams(){
|
||||
List<Team> teams = teamService.getTeams();
|
||||
if(teams.isEmpty()){
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return new ResponseEntity<>(teams, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("single-team")
|
||||
public ResponseEntity<@NonNull Team> getSingleTeam(@RequestBody Team reqTeam){
|
||||
UUID id = reqTeam.getId();
|
||||
String name = reqTeam.getTeamName();
|
||||
Team actualTeam;
|
||||
try{
|
||||
if(id != null){
|
||||
actualTeam = teamService.getTeam(id); //prefer uuid over name, as it is the most reliable way to find a JPA object
|
||||
}
|
||||
else if (name != null){
|
||||
actualTeam = teamService.getTeam(name);
|
||||
}
|
||||
else return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Must provide either id or name")).build();
|
||||
return new ResponseEntity<>(actualTeam, HttpStatus.OK);
|
||||
}
|
||||
catch(TeamNotFoundException e){
|
||||
if(id != null) return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,"Team with id "+ id +" not found")).build();
|
||||
else if (name != null) return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,"Team with name "+ name +" not found")).build();
|
||||
else return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Must provide either id or name.")).build();
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("all")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void deleteAllTeams(){
|
||||
public ResponseEntity< @NonNull String> deleteAllTeams(){
|
||||
teamService.deleteAllTeams();
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.OK,"All teams deleted")).build();
|
||||
}
|
||||
|
||||
@DeleteMapping("deleteTeam")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void deleteTeam(@RequestBody Team team){
|
||||
teamService.deleteTeam(team.getTeamName());
|
||||
@DeleteMapping("single-team")
|
||||
public ResponseEntity<@NonNull String> deleteTeam(@RequestBody Team team){
|
||||
String name = team.getTeamName();
|
||||
UUID id = team.getId();
|
||||
try {
|
||||
if(id != null) teamService.deleteTeam(id); //prefer uuid over name
|
||||
else if(name != null) teamService.deleteTeam(name);
|
||||
else return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "Must provide either id or name")).build();
|
||||
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.OK,"Team deleted successfully")).build();
|
||||
}
|
||||
catch (TeamNotFoundException e){
|
||||
if (id != null) return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,"Team with id "+ id +" not found")).build();
|
||||
else if(name != null) return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,"Team with name "+ name +"not found")).build();
|
||||
else return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, "Team could not be found")).build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user