Files
swiss-backend/src/main/java/nl/connectedit/swiss/controller/TournamentController.java
Michel ten Voorde 46662ac553
Some checks failed
Gitea/swiss-backend/pipeline/head There was a failure building this commit
Updated auth config
2025-10-08 21:36:05 +02:00

210 lines
9.3 KiB
Java
Executable File

package nl.connectedit.swiss.controller;
import jakarta.annotation.security.RolesAllowed;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import nl.connectedit.swiss.auth.Roles;
import nl.connectedit.swiss.domain.TournamentStatus;
import nl.connectedit.swiss.domain.entity.Tournament;
import nl.connectedit.swiss.dto.ResultDto;
import nl.connectedit.swiss.dto.TournamentDto;
import nl.connectedit.swiss.dto.TournamentPlayerSubstitutionDto;
import nl.connectedit.swiss.dto.TournamentValidationDto;
import nl.connectedit.swiss.mapper.TournamentMapper;
import nl.connectedit.swiss.mapper.TournamentValidationMapper;
import nl.connectedit.swiss.service.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Objects;
@RestController
@CrossOrigin
@RolesAllowed(Roles.USER)
@RequiredArgsConstructor
public class TournamentController {
private final TournamentService tournamentService;
private final TournamentMapper tournamentMapper;
private final TournamentValidationService tournamentValidationService;
private final TournamentValidationMapper tournamentValidationMapper;
private final TournamentDivideService tournamentDivideService;
private final TournamentDrawService tournamentDrawService;
private final TournamentPlayService tournamentPlayService;
@GetMapping("/tournaments")
public ResponseEntity<List<TournamentDto>> getTournaments(@RequestParam(value = "status", required = false) TournamentStatus status) {
List<Tournament> tournaments;
if (Objects.nonNull(status)) {
tournaments = tournamentService.findAllTournamentsWithStatus(status);
} else {
tournaments = tournamentService.findAllTournaments();
}
return ResponseEntity.ok(tournaments
.stream()
.map(tournamentMapper::toDto)
.toList());
}
@GetMapping("/tournaments/{id}")
@Transactional
public ResponseEntity<TournamentDto> getTournament(@PathVariable Long id) {
var tournament = tournamentService.findTournamentById(id);
if (tournament == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(tournamentMapper.toDto(tournament));
}
@PostMapping("/tournaments")
public ResponseEntity<TournamentDto> createTournament(@RequestBody TournamentDto tournamentDto) {
Tournament tournament;
try {
tournament = tournamentMapper.toEntity(tournamentDto);
} catch (NullPointerException | DateTimeParseException e) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok(tournamentMapper.toDto(tournamentService.saveTournament(tournament)));
}
@PutMapping("/tournaments/{id}")
public ResponseEntity<TournamentDto> updateTournament(@PathVariable Long id, @RequestBody TournamentDto tournamentDto) {
var newTournament = tournamentMapper.toEntity(tournamentDto);
var tournament = tournamentService.updateTournament(id, newTournament);
return ResponseEntity.ok(tournamentMapper.toDto(tournament));
}
@GetMapping("/tournaments/{id}/validate")
public ResponseEntity<TournamentValidationDto> validateTournament(@PathVariable Long id) {
var tournament = tournamentService.findTournamentById(id);
var tournamentValidation = tournamentValidationService.validate(tournament);
return ResponseEntity.ok(tournamentValidationMapper.toDto(tournamentValidation));
}
@PostMapping("/tournaments/{id}/divide")
public ResponseEntity<TournamentDto> divideTournamentNew(@PathVariable Long id) {
var tournament = tournamentService.findTournamentById(id);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentDivideService.divide(tournament)));
}
@PostMapping("/tournaments/{id}/divide/clear")
public ResponseEntity<TournamentDto> clearTournamentDivision(@PathVariable Long id) {
var tournament = tournamentService.findTournamentById(id);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentDivideService.clear(tournament)));
}
@PostMapping("/tournaments/{id}/draw")
public ResponseEntity<TournamentDto> drawTournament(@PathVariable Long id) {
var tournament = tournamentService.findTournamentById(id);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentDrawService.draw(tournament)));
}
@PostMapping("/tournaments/{id}/draw/clear")
public ResponseEntity<TournamentDto> clearTournamentDraw(@PathVariable Long id) {
var tournament = tournamentService.findTournamentById(id);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentDrawService.clear(tournament)));
}
@PostMapping("/tournaments/{tournamentId}/rounds/{roundId}/start")
public ResponseEntity<TournamentDto> startRound(@PathVariable Long tournamentId, @PathVariable Long roundId) {
var tournament = tournamentService.findTournamentById(tournamentId);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.startRound(tournament, roundId)));
}
@PostMapping("/tournaments/{tournamentId}/rounds/{roundId}/finish")
public ResponseEntity<TournamentDto> finishRound(@PathVariable Long tournamentId, @PathVariable Long roundId) {
var tournament = tournamentService.findTournamentById(tournamentId);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.finishRound(tournament, roundId)));
}
@PostMapping("/tournaments/{tournamentId}/groups/{groupId}/finish")
public ResponseEntity<TournamentDto> finishGroup(@PathVariable Long tournamentId, @PathVariable Long groupId) {
var tournament = tournamentService.findTournamentById(tournamentId);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.finishGroup(tournament, groupId)));
}
@PostMapping("/tournaments/{tournamentId}/groups/{groupId}/reopen")
public ResponseEntity<TournamentDto> reopenGroup(@PathVariable Long tournamentId, @PathVariable Long groupId) {
var tournament = tournamentService.findTournamentById(tournamentId);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.reopenGroup(tournament, groupId)));
}
@PostMapping("/tournaments/{tournamentId}/groups/{groupId}/new")
public ResponseEntity<TournamentDto> newRound(@PathVariable Long tournamentId, @PathVariable Long groupId) {
var tournament = tournamentService.findTournamentById(tournamentId);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.newRound(tournament, groupId)));
}
@PostMapping("/tournaments/{tournamentId}/matches/{matchId}/start")
public ResponseEntity<TournamentDto> startMatch(@PathVariable Long tournamentId, @PathVariable Long matchId,
@RequestParam("court") Long court, @RequestParam("counter") Long counter) {
var tournament = tournamentService.findTournamentById(tournamentId);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.startMatch(tournament, matchId, court, counter)));
}
@PostMapping("/tournaments/{tournamentId}/matches/{matchId}/stop")
public ResponseEntity<TournamentDto> stopMatch(@PathVariable Long tournamentId, @PathVariable Long matchId) {
var tournament = tournamentService.findTournamentById(tournamentId);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.stopMatch(tournament, matchId)));
}
@PostMapping("/tournaments/{tournamentId}/matches/{matchId}")
public ResponseEntity<TournamentDto> saveResult(@PathVariable Long tournamentId, @PathVariable Long matchId, @RequestBody ResultDto resultDto) {
var tournament = tournamentService.findTournamentById(tournamentId);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.saveResult(tournament, matchId, resultDto)));
}
@PatchMapping("/tournaments/{tournamentId}/players/{playerId}/paid/{paid}")
public ResponseEntity<Void> updatePaid(@PathVariable Long tournamentId, @PathVariable Long playerId, @PathVariable Boolean paid) {
var tournament = tournamentService.findTournamentById(tournamentId);
tournamentPlayService.updatePaid(tournament, playerId, paid);
return ResponseEntity.noContent().build();
}
@PatchMapping("/tournaments/{tournamentId}/players/{playerId}/present/{present}")
public ResponseEntity<Void> updatePresent(@PathVariable Long tournamentId, @PathVariable Long playerId, @PathVariable Boolean present) {
var tournament = tournamentService.findTournamentById(tournamentId);
tournamentPlayService.updatePresent(tournament, playerId, present);
return ResponseEntity.noContent().build();
}
@PostMapping("/tournaments/{tournamentId}/players/{playerId}/substitutions")
public ResponseEntity<TournamentDto> substitutePlayer(@PathVariable Long tournamentId, @PathVariable Long playerId, @RequestBody TournamentPlayerSubstitutionDto[] substitutions) {
var tournament = tournamentService.findTournamentById(tournamentId);
return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.playerSubstitutions(tournament, playerId, substitutions)));
}
}