wip: competition card system
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package fr.titionfire.ffsaf.domain.service;
|
||||
|
||||
import fr.titionfire.ffsaf.data.model.CardModel;
|
||||
import fr.titionfire.ffsaf.data.model.MatchModel;
|
||||
import fr.titionfire.ffsaf.data.repository.CardRepository;
|
||||
import fr.titionfire.ffsaf.domain.entity.MatchEntity;
|
||||
import fr.titionfire.ffsaf.rest.exception.DBadRequestException;
|
||||
import fr.titionfire.ffsaf.ws.recv.RCard;
|
||||
import io.quarkus.hibernate.reactive.panache.common.WithSession;
|
||||
import io.quarkus.panache.common.Sort;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@WithSession
|
||||
@ApplicationScoped
|
||||
public class CardService {
|
||||
@Inject
|
||||
CardRepository cardRepository;
|
||||
|
||||
@Inject
|
||||
TradService trad;
|
||||
|
||||
private static final List<CardModel.CardType> COMPETITION_LEVEL_CARDS = List.of(
|
||||
CardModel.CardType.YELLOW,
|
||||
CardModel.CardType.RED,
|
||||
CardModel.CardType.BLACK
|
||||
);
|
||||
|
||||
private List<Long> extractCombIds(MatchModel match) {
|
||||
List<Long> ids = new ArrayList<>();
|
||||
if (match.getC1_id() != null)
|
||||
ids.add(match.getC1_id().getId());
|
||||
if (match.getC2_id() != null)
|
||||
ids.add(match.getC2_id().getId());
|
||||
if (match.getC1_guest() != null)
|
||||
ids.add(match.getC1_guest().getId() * -1);
|
||||
if (match.getC2_guest() != null)
|
||||
ids.add(match.getC2_guest().getId() * -1);
|
||||
return ids;
|
||||
}
|
||||
|
||||
private Collection<Long> extractCombIds(MatchEntity match) {
|
||||
List<Long> ids = new ArrayList<>();
|
||||
if (match.getC1() != null)
|
||||
ids.add(match.getC1().getId());
|
||||
if (match.getC2() != null)
|
||||
ids.add(match.getC2().getId());
|
||||
return ids;
|
||||
}
|
||||
|
||||
public Uni<List<CardModel>> getForMatch(MatchModel match) {
|
||||
return cardRepository.list(
|
||||
"competition = ?1 AND (type IN ?2 OR (type = CardType.BLUE AND category = ?4)) AND comb IN ?3",
|
||||
match.getCategory().getCompet().getId(), COMPETITION_LEVEL_CARDS,
|
||||
extractCombIds(match), match.getCategory().getId());
|
||||
}
|
||||
|
||||
public Uni<List<CardModel>> getAll(Long competitionId) {
|
||||
return cardRepository.list("competition = ?1", competitionId);
|
||||
}
|
||||
|
||||
public Uni<RCard.SendCardAdd> checkCanBeAdded(RCard.SendCardAdd card, MatchModel matchModel) {
|
||||
return cardRepository.find("competition = ?1 AND comb = ?2",
|
||||
Sort.descending("type"),
|
||||
matchModel.getCategory().getCompet().getId(), card.combId())
|
||||
.firstResult()
|
||||
.map(card_ -> {
|
||||
if (card.type() == CardModel.CardType.BLUE) {
|
||||
return card_ == null || (card_.getType() == CardModel.CardType.BLUE
|
||||
&& !Objects.equals(card_.getCategory(), matchModel.getCategory().getId()));
|
||||
}
|
||||
if (card.type() == CardModel.CardType.BLACK) {
|
||||
return card_ != null && card_.getType() == CardModel.CardType.RED;
|
||||
}
|
||||
|
||||
return card_ == null || card_.getType().ordinal() < card.type().ordinal();
|
||||
})
|
||||
.chain(b -> {
|
||||
if (b)
|
||||
return Uni.createFrom().item(card);
|
||||
else
|
||||
return Uni.createFrom().failure(new DBadRequestException(trad.t("card.cannot.be.added")));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user