diff --git a/src/main/java/nl/connectedit/swiss/controller/TournamentController.java b/src/main/java/nl/connectedit/swiss/controller/TournamentController.java index 76bb435..efe8650 100755 --- a/src/main/java/nl/connectedit/swiss/controller/TournamentController.java +++ b/src/main/java/nl/connectedit/swiss/controller/TournamentController.java @@ -26,184 +26,204 @@ import java.util.Objects; @RequiredArgsConstructor public class TournamentController { - private final TournamentService tournamentService; + private final TournamentService tournamentService; - private final TournamentMapper tournamentMapper; + private final TournamentMapper tournamentMapper; - private final TournamentValidationService tournamentValidationService; + private final TournamentValidationService tournamentValidationService; - private final TournamentValidationMapper tournamentValidationMapper; + private final TournamentValidationMapper tournamentValidationMapper; - private final TournamentDivideService tournamentDivideService; + private final TournamentDivideService tournamentDivideService; - private final TournamentDrawService tournamentDrawService; + private final TournamentDrawService tournamentDrawService; - private final TournamentPlayService tournamentPlayService; + private final TournamentPlayService tournamentPlayService; - @GetMapping("/tournaments") - public ResponseEntity> getTournaments(@RequestParam(value = "status", required = false) TournamentStatus status) { - List tournaments; + @GetMapping("/tournaments") + public ResponseEntity> getTournaments(@RequestParam(value = "status", required = false) TournamentStatus status) { + List tournaments; - if (Objects.nonNull(status)) { - tournaments = tournamentService.findAllTournamentsWithStatus(status); - } else { - tournaments = tournamentService.findAllTournaments(); - } - - return ResponseEntity.ok(tournaments - .stream() - .map(tournamentMapper::toDto) - .toList()); + if (Objects.nonNull(status)) { + tournaments = tournamentService.findAllTournamentsWithStatus(status); + } else { + tournaments = tournamentService.findAllTournaments(); } - @GetMapping("/tournaments/{id}") - @Transactional - public ResponseEntity getTournament(@PathVariable Long id) { - var tournament = tournamentService.findTournamentById(id); + return ResponseEntity.ok(tournaments + .stream() + .map(tournamentMapper::toDto) + .toList()); + } - if (tournament == null) { - return ResponseEntity.notFound().build(); - } + @GetMapping("/tournaments/{id}") + @Transactional + public ResponseEntity getTournament(@PathVariable Long id) { + var tournament = tournamentService.findTournamentById(id); - return ResponseEntity.ok(tournamentMapper.toDto(tournament)); + if (tournament == null) { + return ResponseEntity.notFound().build(); } - @PostMapping("/tournaments") - public ResponseEntity 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))); + return ResponseEntity.ok(tournamentMapper.toDto(tournament)); + } + + @PostMapping("/tournaments") + public ResponseEntity 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 updateTournament(@PathVariable Long id, @RequestBody TournamentDto tournamentDto) { - var newTournament = tournamentMapper.toEntity(tournamentDto); - var tournament = tournamentService.updateTournament(id, newTournament); + @PutMapping("/tournaments/{id}") + public ResponseEntity updateTournament(@PathVariable Long id, @RequestBody TournamentDto tournamentDto) { + var newTournament = tournamentMapper.toEntity(tournamentDto); + var tournament = tournamentService.updateTournament(id, newTournament); - return ResponseEntity.ok(tournamentMapper.toDto(tournament)); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournament)); + } - @GetMapping("/tournaments/{id}/validate") - public ResponseEntity validateTournament(@PathVariable Long id) { - var tournament = tournamentService.findTournamentById(id); + @GetMapping("/tournaments/{id}/validate") + public ResponseEntity validateTournament(@PathVariable Long id) { + var tournament = tournamentService.findTournamentById(id); - var tournamentValidation = tournamentValidationService.validate(tournament); + var tournamentValidation = tournamentValidationService.validate(tournament); - return ResponseEntity.ok(tournamentValidationMapper.toDto(tournamentValidation)); - } + return ResponseEntity.ok(tournamentValidationMapper.toDto(tournamentValidation)); + } - @PostMapping("/tournaments/{id}/divide") - public ResponseEntity divideTournamentNew(@PathVariable Long id) { - var tournament = tournamentService.findTournamentById(id); + @PostMapping("/tournaments/{id}/divide") + public ResponseEntity divideTournamentNew(@PathVariable Long id) { + var tournament = tournamentService.findTournamentById(id); - return ResponseEntity.ok(tournamentMapper.toDto(tournamentDivideService.divide(tournament))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentDivideService.divide(tournament))); + } - @PostMapping("/tournaments/{id}/divide/clear") - public ResponseEntity clearTournamentDivision(@PathVariable Long id) { - var tournament = tournamentService.findTournamentById(id); + @PostMapping("/tournaments/{id}/divide/clear") + public ResponseEntity clearTournamentDivision(@PathVariable Long id) { + var tournament = tournamentService.findTournamentById(id); - return ResponseEntity.ok(tournamentMapper.toDto(tournamentDivideService.clear(tournament))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentDivideService.clear(tournament))); + } - @PostMapping("/tournaments/{id}/draw") - public ResponseEntity drawTournament(@PathVariable Long id) { - var tournament = tournamentService.findTournamentById(id); + @PostMapping("/tournaments/{id}/draw") + public ResponseEntity drawTournament(@PathVariable Long id) { + var tournament = tournamentService.findTournamentById(id); - return ResponseEntity.ok(tournamentMapper.toDto(tournamentDrawService.draw(tournament))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentDrawService.draw(tournament))); + } - @PostMapping("/tournaments/{id}/draw/clear") - public ResponseEntity clearTournamentDraw(@PathVariable Long id) { - var tournament = tournamentService.findTournamentById(id); + @PostMapping("/tournaments/{id}/draw/clear") + public ResponseEntity clearTournamentDraw(@PathVariable Long id) { + var tournament = tournamentService.findTournamentById(id); - return ResponseEntity.ok(tournamentMapper.toDto(tournamentDrawService.clear(tournament))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentDrawService.clear(tournament))); + } - @PostMapping("/tournaments/{tournamentId}/rounds/{roundId}/start") - public ResponseEntity startRound(@PathVariable Long tournamentId, @PathVariable Long roundId) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PostMapping("/tournaments/{tournamentId}/rounds/{roundId}/start") + public ResponseEntity startRound(@PathVariable Long tournamentId, @PathVariable Long roundId) { + var tournament = tournamentService.findTournamentById(tournamentId); - return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.startRound(tournament, roundId))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.startRound(tournament, roundId))); + } - @PostMapping("/tournaments/{tournamentId}/rounds/{roundId}/finish") - public ResponseEntity finishRound(@PathVariable Long tournamentId, @PathVariable Long roundId) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PostMapping("/tournaments/{tournamentId}/rounds/{roundId}/finish") + public ResponseEntity finishRound(@PathVariable Long tournamentId, @PathVariable Long roundId) { + var tournament = tournamentService.findTournamentById(tournamentId); - return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.finishRound(tournament, roundId))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.finishRound(tournament, roundId))); + } - @PostMapping("/tournaments/{tournamentId}/groups/{groupId}/finish") - public ResponseEntity finishGroup(@PathVariable Long tournamentId, @PathVariable Long groupId) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PostMapping("/tournaments/{tournamentId}/groups/{groupId}/finish") + public ResponseEntity finishGroup(@PathVariable Long tournamentId, @PathVariable Long groupId) { + var tournament = tournamentService.findTournamentById(tournamentId); - return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.finishGroup(tournament, groupId))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.finishGroup(tournament, groupId))); + } - @PostMapping("/tournaments/{tournamentId}/groups/{groupId}/reopen") - public ResponseEntity reopenGroup(@PathVariable Long tournamentId, @PathVariable Long groupId) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PostMapping("/tournaments/{tournamentId}/groups/{groupId}/reopen") + public ResponseEntity reopenGroup(@PathVariable Long tournamentId, @PathVariable Long groupId) { + var tournament = tournamentService.findTournamentById(tournamentId); - return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.reopenGroup(tournament, groupId))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.reopenGroup(tournament, groupId))); + } - @PostMapping("/tournaments/{tournamentId}/groups/{groupId}/new") - public ResponseEntity newRound(@PathVariable Long tournamentId, @PathVariable Long groupId) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PostMapping("/tournaments/{tournamentId}/groups/{groupId}/new") + public ResponseEntity newRound(@PathVariable Long tournamentId, @PathVariable Long groupId) { + var tournament = tournamentService.findTournamentById(tournamentId); - return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.newRound(tournament, groupId))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.newRound(tournament, groupId))); + } - @PostMapping("/tournaments/{tournamentId}/matches/{matchId}/start") - public ResponseEntity startMatch(@PathVariable Long tournamentId, @PathVariable Long matchId, - @RequestParam("court") Long court, @RequestParam("counter") Long counter) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PostMapping("/tournaments/{tournamentId}/matches/{matchId}/start") + public ResponseEntity 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))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.startMatch(tournament, matchId, court, counter))); + } - @PostMapping("/tournaments/{tournamentId}/matches/{matchId}/stop") - public ResponseEntity stopMatch(@PathVariable Long tournamentId, @PathVariable Long matchId) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PostMapping("/tournaments/{tournamentId}/matches/{matchId}/stop") + public ResponseEntity stopMatch(@PathVariable Long tournamentId, @PathVariable Long matchId) { + var tournament = tournamentService.findTournamentById(tournamentId); - return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.stopMatch(tournament, matchId))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.stopMatch(tournament, matchId))); + } - @PostMapping("/tournaments/{tournamentId}/matches/{matchId}") - public ResponseEntity saveResult(@PathVariable Long tournamentId, @PathVariable Long matchId, @RequestBody ResultDto resultDto) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PostMapping("/tournaments/{tournamentId}/matches/{matchId}") + public ResponseEntity 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))); - } + return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.saveResult(tournament, matchId, resultDto))); + } - @PatchMapping("/tournaments/{tournamentId}/players/{playerId}/paid/{paid}") - public ResponseEntity updatePaid(@PathVariable Long tournamentId, @PathVariable Long playerId, @PathVariable Boolean paid) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PatchMapping("/tournaments/{tournamentId}/players/{playerId}/paid/{paid}") + public ResponseEntity updatePaid(@PathVariable Long tournamentId, + @PathVariable Long playerId, + @PathVariable Boolean paid) { + var tournament = tournamentService.findTournamentById(tournamentId); - tournamentPlayService.updatePaid(tournament, playerId, paid); + tournamentPlayService.updatePaid(tournament, playerId, paid); - return ResponseEntity.noContent().build(); - } + return ResponseEntity.noContent().build(); + } - @PatchMapping("/tournaments/{tournamentId}/players/{playerId}/present/{present}") - public ResponseEntity updatePresent(@PathVariable Long tournamentId, @PathVariable Long playerId, @PathVariable Boolean present) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PatchMapping("/tournaments/{tournamentId}/players/{playerId}/present/{present}") + public ResponseEntity updatePresent(@PathVariable Long tournamentId, + @PathVariable Long playerId, + @PathVariable Boolean present) { + var tournament = tournamentService.findTournamentById(tournamentId); - tournamentPlayService.updatePresent(tournament, playerId, present); + tournamentPlayService.updatePresent(tournament, playerId, present); - return ResponseEntity.noContent().build(); - } + return ResponseEntity.noContent().build(); + } - @PostMapping("/tournaments/{tournamentId}/players/{playerId}/substitutions") - public ResponseEntity substitutePlayer(@PathVariable Long tournamentId, @PathVariable Long playerId, @RequestBody TournamentPlayerSubstitutionDto[] substitutions) { - var tournament = tournamentService.findTournamentById(tournamentId); + @PostMapping("/tournaments/{tournamentId}/players/{playerId}/substitutions") + public ResponseEntity 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))); + } + + @PatchMapping("/tournaments/{tournamentId}/matches/{matchId}/update") + public ResponseEntity updateCounter(@PathVariable Long tournamentId, + @PathVariable Long matchId, + @RequestParam("counter") Long counter) { + var tournament = tournamentService.findTournamentById(tournamentId); + + return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.updateCounter(tournament, matchId, counter))); + } - return ResponseEntity.ok(tournamentMapper.toDto(tournamentPlayService.playerSubstitutions(tournament, playerId, substitutions))); - } } diff --git a/src/main/java/nl/connectedit/swiss/service/TournamentPlayService.java b/src/main/java/nl/connectedit/swiss/service/TournamentPlayService.java index a015d19..bcf5829 100644 --- a/src/main/java/nl/connectedit/swiss/service/TournamentPlayService.java +++ b/src/main/java/nl/connectedit/swiss/service/TournamentPlayService.java @@ -220,6 +220,21 @@ public class TournamentPlayService { return null; } + public Tournament updateCounter(Tournament tournament, Long matchId, Long counter) { + var match = getMatch(tournament, matchId); + + var currentlyCountingPlayer = getTournamentPlayer(tournament, match.getCounter().getId()); + currentlyCountingPlayer.setCounting(false); + + var newCountingPlayer = getTournamentPlayer(tournament, counter); + newCountingPlayer.setCounting(true); + match.setCounter(newCountingPlayer.getPlayer()); + + tournamentRepository.save(tournament); + return tournament; + + } + private static class ConflictInDrawException extends RuntimeException {}