mirror of
https://github.com/PaulReichmuth/timekeep-backend.git
synced 2025-12-22 22:41:59 +00:00
105 lines
4.6 KiB
Java
105 lines
4.6 KiB
Java
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 {
|
|
|
|
private final TeamService teamService;
|
|
|
|
@PostMapping("createTeam")
|
|
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 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")
|
|
public ResponseEntity< @NonNull String> deleteAllTeams(){
|
|
teamService.deleteAllTeams();
|
|
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.OK,"All teams deleted")).build();
|
|
}
|
|
|
|
@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();
|
|
}
|
|
}
|
|
|
|
}
|
|
|