feat: move competition setting to ffsaf site
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package fr.titionfire.ffsaf.domain.service;
|
||||
|
||||
import fr.titionfire.ffsaf.data.model.ClubModel;
|
||||
import fr.titionfire.ffsaf.data.model.MatchModel;
|
||||
import fr.titionfire.ffsaf.data.repository.CombRepository;
|
||||
import fr.titionfire.ffsaf.data.repository.MatchRepository;
|
||||
import fr.titionfire.ffsaf.data.repository.PouleRepository;
|
||||
import fr.titionfire.ffsaf.rest.data.MatchData;
|
||||
import fr.titionfire.ffsaf.utils.CompetitionSystem;
|
||||
import io.quarkus.hibernate.reactive.panache.Panache;
|
||||
import io.quarkus.hibernate.reactive.panache.common.WithSession;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@WithSession
|
||||
@ApplicationScoped
|
||||
public class MatchService {
|
||||
|
||||
@Inject
|
||||
MatchRepository repository;
|
||||
|
||||
@Inject
|
||||
PouleRepository pouleRepository;
|
||||
|
||||
@Inject
|
||||
CombRepository combRepository;
|
||||
|
||||
public Uni<MatchData> getById(Consumer<ClubModel> checkPerm, CompetitionSystem system, Long id) {
|
||||
return repository.find("systemId = ?1 AND system = ?2", id, system).firstResult()
|
||||
.onItem().ifNull().failWith(() -> new RuntimeException("Match not found"))
|
||||
.call(data -> pouleRepository.findById(data.getPoule())
|
||||
.invoke(data2 -> checkPerm.accept(data2.getCompet().getClub())))
|
||||
.map(MatchData::fromModel);
|
||||
}
|
||||
|
||||
public Uni<List<MatchData>> getAllByPoule(Consumer<ClubModel> checkPerm, CompetitionSystem system, Long id) {
|
||||
return pouleRepository.find("systemId = ?1 AND system = ?2", id, system).firstResult()
|
||||
.onItem().ifNull().failWith(() -> new RuntimeException("Poule not found"))
|
||||
.invoke(data -> checkPerm.accept(data.getCompet().getClub()))
|
||||
.chain(data -> repository.list("poule = ?1", data.getId())
|
||||
.map(o -> o.stream().map(MatchData::fromModel).toList()));
|
||||
}
|
||||
|
||||
public Uni<MatchData> addOrUpdate(Consumer<ClubModel> checkPerm, CompetitionSystem system, MatchData data) {
|
||||
return repository.find("systemId = ?1 AND system = ?2", data.getId(), system).firstResult()
|
||||
.chain(o -> {
|
||||
if (o == null) {
|
||||
return pouleRepository.findById(data.getPoule())
|
||||
.onItem().ifNull().failWith(() -> new RuntimeException("Poule not found"))
|
||||
.invoke(data2 -> checkPerm.accept(data2.getCompet().getClub()))
|
||||
.map(pouleModel -> {
|
||||
MatchModel model = new MatchModel();
|
||||
|
||||
model.setId(null);
|
||||
model.setSystem(system);
|
||||
model.setSystemId(data.getId());
|
||||
model.setPoule(pouleModel.getId());
|
||||
return model;
|
||||
});
|
||||
} else {
|
||||
return pouleRepository.findById(data.getPoule())
|
||||
.onItem().ifNull().failWith(() -> new RuntimeException("Poule not found"))
|
||||
.invoke(data2 -> checkPerm.accept(data2.getCompet().getClub()))
|
||||
.map(__ -> o);
|
||||
}
|
||||
}
|
||||
)
|
||||
.chain(o -> {
|
||||
o.setC1_str(data.getC1_str());
|
||||
o.setC2_str(data.getC2_str());
|
||||
o.setPoule_ord(data.getPoule_ord());
|
||||
o.setScores(data.getScores());
|
||||
|
||||
return Uni.createFrom().nullItem()
|
||||
.chain(() -> (data.getC1_id() == null) ?
|
||||
Uni.createFrom().nullItem() : combRepository.findById(data.getC1_id()))
|
||||
.invoke(o::setC1_id)
|
||||
.chain(() -> (data.getC1_id() == null) ?
|
||||
Uni.createFrom().nullItem() : combRepository.findById(data.getC2_id()))
|
||||
.invoke(o::setC2_id)
|
||||
.chain(() -> Panache.withTransaction(() -> repository.persist(o)));
|
||||
})
|
||||
.map(MatchData::fromModel);
|
||||
}
|
||||
|
||||
public Uni<?> delete(Consumer<ClubModel> checkPerm, Long id) {
|
||||
return repository.findById(id)
|
||||
.onItem().ifNull().failWith(() -> new RuntimeException("Match not found"))
|
||||
.call(data -> pouleRepository.findById(data.getPoule())
|
||||
.invoke(data2 -> checkPerm.accept(data2.getCompet().getClub()))
|
||||
.chain(data2 -> Panache.withTransaction(() -> repository.delete("id", data.getId()))));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user