1
0
Fork 0
mirror of https://codeberg.org/beerbrawl/beerbrawl.git synced 2024-09-23 05:40:51 +02:00

feat(#24): backend: fix some ko drinks pickup validation bugs

Signed-off-by: Christoph Heiss <e11907069@student.tuwien.ac.at>
This commit is contained in:
Christoph Heiss 2024-06-12 16:37:29 +02:00
parent 0ba2632efd
commit 5006dee9dc
Signed by: c8h4
GPG key ID: 73D5E7FDEE3DE49A
3 changed files with 42 additions and 21 deletions

View file

@ -79,12 +79,15 @@ public class KoStandingsEndpoint {
LOG.info("PUT /api/v1/tournaments/{}/ko-standings/{}", tournamentId, standingId);
LOG.debug("request body: {}", updateStandingDto);
final var updated = koStandingsService.updateKoStanding(
this.koStandingsService.updateKoStanding(
authentication.getName(),
tournamentId,
standingId,
updateStandingDto
);
return this.koStandingsMapper.entityToDto(updated);
return this.koStandingsMapper.entityToDto(
this.koStandingsService.getStandingById(standingId)
);
}
}

View file

@ -10,6 +10,15 @@ import at.ac.tuwien.sepr.groupphase.backend.exception.NotFoundException;
import at.ac.tuwien.sepr.groupphase.backend.exception.PreconditionFailedException;
public interface TournamentKoPhaseService {
/**
* Retrieve a standing KO-phase standing.
*
* @param standingId the id of the standing entity to retrieve
* @return the requested standing
* @throws NotFoundException if the requested standing was not found
*/
KoStanding getStandingById(long standingId) throws NotFoundException;
/**
* Generate ko matches for a tournament.
*
@ -33,12 +42,11 @@ public interface TournamentKoPhaseService {
*
* @param userName of the user that does the update, needs to be the
* same as the organizer
* @param tournamentId that the standing belongs to
* @param standingId that will be updated
* @param tournamentId tournament the standing belongs to
* @param standingId id of the ko standing to update
* @param updateDto details to update the standing with
* @return the updated ko standing
*/
KoStanding updateKoStanding(
void updateKoStanding(
String userName,
Long tournamentId,
Long standingId,

View file

@ -1,7 +1,6 @@
package at.ac.tuwien.sepr.groupphase.backend.service.impl;
import at.ac.tuwien.sepr.groupphase.backend.endpoint.dto.TournamentUpdateKoStandingDto;
import at.ac.tuwien.sepr.groupphase.backend.endpoint.mapper.TournamentKoPhaseMapper;
import at.ac.tuwien.sepr.groupphase.backend.entity.KoStanding;
import at.ac.tuwien.sepr.groupphase.backend.entity.Team;
import at.ac.tuwien.sepr.groupphase.backend.entity.Tournament;
@ -41,7 +40,14 @@ public class TournamentKoPhaseServiceImpl implements TournamentKoPhaseService {
private final KoStandingsRepository koStandingsRepository;
private final TournamentTeamService teamService;
private final TournamentQualificationService qualificationService;
private final TournamentKoPhaseMapper koPhaseMapper;
@Override
public KoStanding getStandingById(long standingId) {
return this.koStandingsRepository.findById(standingId)
.orElseThrow(() -> new NotFoundException(
"KO standing %d not found".formatted(standingId)
));
}
@Override
@Transactional
@ -202,7 +208,8 @@ public class TournamentKoPhaseServiceImpl implements TournamentKoPhaseService {
}
@Override
public KoStanding updateKoStanding(
@Transactional
public void updateKoStanding(
String userName,
Long tournamentId,
Long standingId,
@ -225,10 +232,6 @@ public class TournamentKoPhaseServiceImpl implements TournamentKoPhaseService {
if (updateDto.drinksPickup() != null) {
this.updateKoStandingDrinksStatus(koStanding, updateDto.drinksPickup());
}
// Ensure we return an up-to-date object in any case
return this.koStandingsRepository.findById(standingId)
.orElseThrow(() -> new NotFoundException("KO standing not found"));
}
private void updateKoStandingTeam(KoStanding koStanding, TournamentUpdateKoStandingDto.TeamSetDto teamSet) {
@ -250,6 +253,7 @@ public class TournamentKoPhaseServiceImpl implements TournamentKoPhaseService {
}
koStanding.setTeam(team);
koStanding.setEndTime(BeerDateTime.nowUtc());
}
this.koStandingsRepository.saveAndFlush(koStanding);
@ -278,7 +282,7 @@ public class TournamentKoPhaseServiceImpl implements TournamentKoPhaseService {
));
}
if (preceding.isDrinksCollected()) {
if (standing.isDrinksCollected()) {
throw new TeamMatchDrinksAlreadyPickedUpException(standing.getId(), preceding.getTeam().getId());
}
@ -289,12 +293,15 @@ public class TournamentKoPhaseServiceImpl implements TournamentKoPhaseService {
);
}
if (standing.getStartTime() != null) {
throw new PreconditionFailedException("KO match has already started");
}
final var next = standing.getNextStanding();
if (next != null) {
if (next.getStartTime() != null) {
throw new PreconditionFailedException("KO match has already started");
}
if (standing.getEndTime() != null) {
throw new PreconditionFailedException("KO match has already ended");
if (next.getEndTime() != null) {
throw new PreconditionFailedException("KO match has already ended");
}
}
preceding.setDrinksCollected(true);
@ -304,7 +311,10 @@ public class TournamentKoPhaseServiceImpl implements TournamentKoPhaseService {
}
private void tryStartKoPhaseMatch(KoStanding standing) {
final var participantIds = standing.getPreceedingStandings().stream().map(p -> p.getTeam().getId()).toList();
final var participantIds = standing.getPreceedingStandings()
.stream().map(p -> p.getTeam().getId())
.toList();
if (participantIds.stream().anyMatch(this.teamService::isTeamCurrentlyPlaying)) {
LOG.debug(
"Cannot start KO match {}; one or both participating teams ({}) are currently playing",
@ -326,4 +336,4 @@ public class TournamentKoPhaseServiceImpl implements TournamentKoPhaseService {
standing.setStartTime(BeerDateTime.nowUtc());
this.koStandingsRepository.saveAndFlush(standing);
}
}
}