1
0
Fork 0
mirror of https://codeberg.org/beerbrawl/beerbrawl.git synced 2024-09-22 21:20:52 +02:00

fix(#15): add tournament team delete endpoint

This commit is contained in:
motzik 2024-05-25 10:45:30 +02:00
parent e871ddd874
commit 136b2f1e53
6 changed files with 162 additions and 78 deletions

7
.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
# db files
/database/db.mv.db
/database/db.trace.db
/database/db.lock.db
# logs
/log

View file

@ -61,7 +61,8 @@ public class TournamentEndpoint {
@Secured("ROLE_USER")
@ResponseStatus(HttpStatus.OK)
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Get a list of all tournaments from a specific Organizer", security = @SecurityRequirement(name = "apiKey"))
@Operation(summary = "Get a list of all tournaments from a specific Organizer",
security = @SecurityRequirement(name = "apiKey"))
public ResponseEntity<List<TournamentListDto>> tournaments(Authentication authentication) {
LOG.info("GET {}", BASE_ENDPOINT);
var tournaments = tournamentService.findAllByOrganizer(authentication.getName());
@ -94,22 +95,6 @@ public class TournamentEndpoint {
.toList();
}
@Secured("ROLE_USER")
@ResponseStatus(HttpStatus.OK)
@GetMapping(value = "{id}/teams")
@Operation(summary = "Get teams for a tournament", security = @SecurityRequirement(name = "apiKey"))
public List<TeamDto> getTournamentTeams(@PathVariable(name = "id") Long tournamentId,
Authentication authentication) {
LOG.info("GET /api/v1/tournaments/{}/teams", tournamentId);
// check if user is organizer of tournament
if (!tournamentService.isOrganizer(authentication.getName(), tournamentId)) {
throw new AccessDeniedException("Current user isn't organizer of tournament.");
}
return tournamentService.getTournamentTeams(tournamentId).stream().map(teamMapper::entityToDto).toList();
}
@Secured("ROLE_USER")
@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{tournamentId}")
@ -157,6 +142,40 @@ public class TournamentEndpoint {
public record SignupTeamResponseDto(SignupTeamResult signupTeamResult) {
}
@Secured("ROLE_USER")
@ResponseStatus(HttpStatus.OK)
@GetMapping(value = "{id}/teams")
@Operation(summary = "Get teams for a tournament", security = @SecurityRequirement(name = "apiKey"))
public List<TeamDto> getTournamentTeams(@PathVariable(name = "id") Long tournamentId,
Authentication authentication) {
LOG.info("GET /api/v1/tournaments/{}/teams", tournamentId);
// check if user is organizer of tournament
if (!tournamentService.isOrganizer(authentication.getName(), tournamentId)) {
throw new AccessDeniedException("Current user isn't organizer of tournament.");
}
return tournamentService.getTournamentTeams(tournamentId).stream().map(teamMapper::entityToDto).toList();
}
@Secured("ROLE_USER")
@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping(value = "{tournamentId}/teams/{teamId}")
@Operation(summary = "Delete team from a tournament", security = @SecurityRequirement(name = "apiKey"))
public ResponseEntity<Void> deleteTournamentTeam(@PathVariable("tournamentId") Long tournamentId,
@PathVariable("teamId") Long teamId,
Authentication authentication
) {
LOG.info("Delete /api/v1/tournaments/{}/teams/{}", tournamentId, teamId);
// check if user is organizer of tournament
if (!tournamentService.isOrganizer(authentication.getName(), tournamentId)) {
throw new AccessDeniedException("Current user isn't organizer of tournament.");
}
tournamentService.deleteTeam(tournamentId, teamId);
return ResponseEntity.noContent().build();
}
@PermitAll
@ResponseStatus(HttpStatus.OK) // No location header, thus 200
@PostMapping("{tournamentId}/teams")

View file

@ -3,6 +3,7 @@ package at.ac.tuwien.sepr.groupphase.backend.endpoint.exceptionhandler;
import at.ac.tuwien.sepr.groupphase.backend.exception.NotFoundException;
import at.ac.tuwien.sepr.groupphase.backend.exception.PreconditionFailedException;
import at.ac.tuwien.sepr.groupphase.backend.exception.TournamentAlreadyStartedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
@ -90,4 +91,16 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
HttpStatus.FORBIDDEN,
request);
}
@ExceptionHandler({TournamentAlreadyStartedException.class})
protected ResponseEntity<Object> handleTournamentAlreadyStartedException(RuntimeException ex, WebRequest request) {
LOGGER.debug(ex.getMessage());
return handleExceptionInternal(
ex,
"Tournament already started",
new HttpHeaders(),
HttpStatus.CONFLICT,
request);
}
}

View file

@ -0,0 +1,19 @@
package at.ac.tuwien.sepr.groupphase.backend.exception;
public class TournamentAlreadyStartedException extends RuntimeException {
public TournamentAlreadyStartedException() {
}
public TournamentAlreadyStartedException(String message) {
super(message);
}
public TournamentAlreadyStartedException(String message, Throwable cause) {
super(message, cause);
}
public TournamentAlreadyStartedException(Exception e) {
super(e);
}
}

View file

@ -94,4 +94,12 @@ public interface TournamentService {
* (domain-level authorization)
*/
void generateKoMatchesForTournament(Long tournamentId, String subject);
/**
* Delete a team from a tournament.
*
* @param tournamentId the id of the tournament entity
* @param teamId the id of the team entity
*/
void deleteTeam(Long tournamentId, Long teamId);
}

View file

@ -8,6 +8,7 @@ import java.util.List;
import java.util.stream.Collectors;
import at.ac.tuwien.sepr.groupphase.backend.entity.Team;
import at.ac.tuwien.sepr.groupphase.backend.exception.TournamentAlreadyStartedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
@ -277,4 +278,21 @@ public class TournamentServiceImpl implements TournamentService {
koStandingsRepository.saveAndFlush(layer[0]);
}
@Override
public void deleteTeam(Long tournamentId, Long teamId)
throws NotFoundException, IllegalArgumentException, IllegalStateException {
LOGGER.debug("Delete team with id {} from tournament with id {}", teamId, tournamentId);
var team = teamRepository.findById(teamId).orElseThrow(() -> new NotFoundException("Team not found in tournament"));
var tournament = team.getTournament();
if (!tournament.getId().equals(tournamentId)) {
throw new NotFoundException("Team not found in tournament");
}
if ((long) tournament.getQualificationMatches().size() > 0) {
throw new TournamentAlreadyStartedException();
}
teamRepository.delete(team);
}
}