Files
timekeep-backend/src/main/java/de/pnreichmuth/timekeep_backend/controllers/TeamRestController.java

125 lines
5.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 de.pnreichmuth.timekeep_backend.wsto.TeamWSTO;
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.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@RestController
@RequestMapping("/teams")
@Slf4j
@RequiredArgsConstructor
public class TeamRestController {
private final TeamService teamService;
// ///////////////////////////////////////////////BEGIN POST MAPPINGS///////////////////////////////////////////////////
/**
* Creates a team with the information provided via the /teams/createTeam endpoint
* @param team
* @return a ResponseEntity,
* either containing a TeamWSTO if a team was successfully created or a HttpStatus.CONFLICT if the team already existed beforehand
*/
@PostMapping("createTeam")
public ResponseEntity<@NonNull TeamWSTO> createTeam(@RequestBody Team team){
try {
teamService.createTeam(team);
return new ResponseEntity<>(TeamWSTO.of(team), HttpStatus.CREATED);
} catch (TeamExistsException e) {
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT,"This team already exists")).build();
}
}
// ///////////////////////////////////////////////BEGIN GET MAPPINGS///////////////////////////////////////////////////
/**
* Gets all stored teams from the database
* @return a HttpStatus.NOT_FOUND ResponseEntity if the database does not contain any teams,
* else a ResponseEntity containing the list of all Teams in WSTO form
*/
@GetMapping("")
public ResponseEntity<@NonNull List<TeamWSTO>> getAllTeams(){
List<Team> teams = teamService.getTeams();
List<TeamWSTO> teamWSTOs = new ArrayList<>();
if(teams.isEmpty()){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
for(Team team : teams){
teamWSTOs.add(TeamWSTO.of(team));
}
return new ResponseEntity<>(teamWSTOs, HttpStatus.OK);
}
/**
* 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
* @return a ResponseEntity containing the team in WSTO form, or a ResponseEntity containing either BAD_REQUEST or NOT_FOUND.
*/
@GetMapping("single-team")
public ResponseEntity<@NonNull TeamWSTO> getSingleTeam(@RequestBody Team reqTeam){
UUID id = reqTeam.getId();
String name = reqTeam.getTeamName();
Team actualTeam;
try{
actualTeam = teamService.getTeam(reqTeam);
return new ResponseEntity<>(TeamWSTO.of(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 return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,"Team with name "+ name +" not found")).build();
}
catch (IllegalArgumentException e){
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST,e.getMessage())).build();
}
}
// ///////////////////////////////////////////////BEGIN DELETE MAPPINGS///////////////////////////////////////////////////
/**
* Deletes all teams (yes, really)
* @return a ResponseEntity with HttpStatus.OK
*/
@DeleteMapping("all")
public ResponseEntity< @NonNull String> deleteAllTeams(){
teamService.deleteAllTeams();
return ResponseEntity.of(ProblemDetail.forStatusAndDetail(HttpStatus.OK,"All teams deleted")).build();
}
/**
* 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
* @return a ResponseEntity containing HttpStatus.OK if deletion was successful,
* or a ResponseEntity containing either BAD_REQUEST or NOT_FOUND.
*/
@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();
}
}
}