feat: secure match ans poule endpoints

This commit is contained in:
2024-08-15 13:56:54 +02:00
parent bd386d1b0a
commit 6b38405e94
7 changed files with 82 additions and 61 deletions

View File

@@ -1,6 +1,5 @@
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;
@@ -8,6 +7,7 @@ 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 fr.titionfire.ffsaf.utils.SecurityCtx;
import io.quarkus.hibernate.reactive.panache.Panache;
import io.quarkus.hibernate.reactive.panache.common.WithSession;
import io.smallrye.mutiny.Uni;
@@ -15,7 +15,6 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.function.Consumer;
@WithSession
@ApplicationScoped
@@ -30,30 +29,32 @@ public class MatchService {
@Inject
CombRepository combRepository;
public Uni<MatchData> getById(Consumer<ClubModel> checkPerm, CompetitionSystem system, Long id) {
@Inject
CompetPermService permService;
public Uni<MatchData> getById(SecurityCtx securityCtx, 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().getId())
.invoke(data2 -> checkPerm.accept(data2.getCompet().getClub())))
.call(data -> permService.hasViewPerm(securityCtx, data.getPoule().getCompet()))
.map(MatchData::fromModel);
}
public Uni<List<MatchData>> getAllByPoule(Consumer<ClubModel> checkPerm, CompetitionSystem system, Long id) {
public Uni<List<MatchData>> getAllByPoule(SecurityCtx securityCtx, 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()))
.call(data -> permService.hasViewPerm(securityCtx, data.getCompet()))
.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) {
public Uni<MatchData> addOrUpdate(SecurityCtx securityCtx, CompetitionSystem system, MatchData data) {
return repository.find("systemId = ?1 AND system = ?2", data.getId(), system).firstResult()
.chain(o -> {
if (o == null) {
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()))
.call(o2 -> permService.hasEditPerm(securityCtx, o2.getCompet()))
.map(pouleModel -> {
MatchModel model = new MatchModel();
@@ -67,7 +68,7 @@ public class MatchService {
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()))
.call(o2 -> permService.hasEditPerm(securityCtx, o2.getCompet()))
.map(__ -> o);
}
}
@@ -91,9 +92,11 @@ public class MatchService {
.map(MatchData::fromModel);
}
public Uni<?> updateScore(CompetitionSystem system, Long id, List<ScoreEmbeddable> scores) {
public Uni<?> updateScore(SecurityCtx securityCtx, 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(o2 -> permService.hasEditPerm(securityCtx, o2.getPoule().getCompet()))
.invoke(data -> {
data.getScores().clear();
data.getScores().addAll(scores);
@@ -102,11 +105,10 @@ public class MatchService {
.map(o -> "OK");
}
public Uni<?> delete(Consumer<ClubModel> checkPerm, CompetitionSystem system, Long id) {
public Uni<?> delete(SecurityCtx securityCtx, 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(data))));
.call(o2 -> permService.hasEditPerm(securityCtx, o2.getPoule().getCompet()))
.chain(data -> Panache.withTransaction(() -> repository.delete(data)));
}
}