wip: send competition data

This commit is contained in:
2024-08-14 21:33:11 +02:00
parent 4be7b28efd
commit 7f80c876d3
12 changed files with 291 additions and 43 deletions

View File

@@ -7,6 +7,7 @@ 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 fr.titionfire.ffsaf.utils.ScoreEmbeddable;
import io.quarkus.hibernate.reactive.panache.Panache;
import io.quarkus.hibernate.reactive.panache.common.WithSession;
import io.smallrye.mutiny.Uni;
@@ -32,7 +33,7 @@ public class MatchService {
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())
.call(data -> pouleRepository.findById(data.getPoule().getId())
.invoke(data2 -> checkPerm.accept(data2.getCompet().getClub())))
.map(MatchData::fromModel);
}
@@ -49,7 +50,8 @@ public class MatchService {
return repository.find("systemId = ?1 AND system = ?2", data.getId(), system).firstResult()
.chain(o -> {
if (o == null) {
return pouleRepository.findById(data.getPoule())
return pouleRepository.find("systemId = ?1 AND system = ?2", data.getPoule(), system)
.firstResult()
.onItem().ifNull().failWith(() -> new RuntimeException("Poule not found"))
.invoke(data2 -> checkPerm.accept(data2.getCompet().getClub()))
.map(pouleModel -> {
@@ -58,11 +60,12 @@ public class MatchService {
model.setId(null);
model.setSystem(system);
model.setSystemId(data.getId());
model.setPoule(pouleModel.getId());
model.setPoule(pouleModel);
return model;
});
} else {
return pouleRepository.findById(data.getPoule())
return pouleRepository.find("systemId = ?1 AND system = ?2", data.getPoule(), system)
.firstResult()
.onItem().ifNull().failWith(() -> new RuntimeException("Poule not found"))
.invoke(data2 -> checkPerm.accept(data2.getCompet().getClub()))
.map(__ -> o);
@@ -73,7 +76,8 @@ public class MatchService {
o.setC1_str(data.getC1_str());
o.setC2_str(data.getC2_str());
o.setPoule_ord(data.getPoule_ord());
o.setScores(data.getScores());
o.getScores().clear();
o.getScores().addAll(data.getScores());
return Uni.createFrom().nullItem()
.chain(() -> (data.getC1_id() == null) ?
@@ -87,11 +91,22 @@ public class MatchService {
.map(MatchData::fromModel);
}
public Uni<?> delete(Consumer<ClubModel> checkPerm, Long id) {
return repository.findById(id)
public Uni<?> updateScore(CompetitionSystem system, Long id, List<ScoreEmbeddable> scores) {
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(data -> {
data.getScores().clear();
data.getScores().addAll(scores);
})
.chain(data -> Panache.withTransaction(() -> repository.persist(data)))
.map(o -> "OK");
}
public Uni<?> delete(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"))
.chain(data -> pouleRepository.findById(data.getPoule().getId())
.invoke(data2 -> checkPerm.accept(data2.getCompet().getClub()))
.chain(data2 -> Panache.withTransaction(() -> repository.delete("id", data.getId()))));
.chain(data2 -> Panache.withTransaction(() -> repository.delete(data))));
}
}