All checks were successful
Deploy Production Server / if_merged (pull_request) Successful in 6m55s
116 lines
5.8 KiB
Java
116 lines
5.8 KiB
Java
package fr.titionfire.ffsaf.domain.service;
|
|
|
|
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.rest.exception.DNotFoundException;
|
|
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;
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
import jakarta.inject.Inject;
|
|
|
|
import java.util.List;
|
|
|
|
@WithSession
|
|
@ApplicationScoped
|
|
public class MatchService {
|
|
|
|
@Inject
|
|
MatchRepository repository;
|
|
|
|
@Inject
|
|
PouleRepository pouleRepository;
|
|
|
|
@Inject
|
|
CombRepository combRepository;
|
|
|
|
@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 DNotFoundException("Match not found"))
|
|
.call(data -> permService.hasViewPerm(securityCtx, data.getPoule().getCompet()))
|
|
.map(MatchData::fromModel);
|
|
}
|
|
|
|
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 DNotFoundException("Poule not found"))
|
|
.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(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 DNotFoundException("Poule not found"))
|
|
.call(o2 -> permService.hasEditPerm(securityCtx, o2.getCompet()))
|
|
.map(pouleModel -> {
|
|
MatchModel model = new MatchModel();
|
|
|
|
model.setId(null);
|
|
model.setSystem(system);
|
|
model.setSystemId(data.getId());
|
|
model.setPoule(pouleModel);
|
|
return model;
|
|
});
|
|
} else {
|
|
return pouleRepository.find("systemId = ?1 AND system = ?2", data.getPoule(), system)
|
|
.firstResult()
|
|
.onItem().ifNull().failWith(() -> new DNotFoundException("Poule not found"))
|
|
.call(o2 -> permService.hasEditPerm(securityCtx, o2.getCompet()))
|
|
.map(__ -> o);
|
|
}
|
|
}
|
|
)
|
|
.chain(o -> {
|
|
o.setC1_str(data.getC1_str());
|
|
o.setC2_str(data.getC2_str());
|
|
o.setPoule_ord(data.getPoule_ord());
|
|
o.getScores().clear();
|
|
o.getScores().addAll(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<?> 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 DNotFoundException("Match not found"))
|
|
.call(o2 -> permService.hasEditPerm(securityCtx, o2.getPoule().getCompet()))
|
|
.invoke(data -> {
|
|
data.getScores().clear();
|
|
data.getScores().addAll(scores);
|
|
})
|
|
.chain(data -> Panache.withTransaction(() -> repository.persist(data)))
|
|
.map(o -> "OK");
|
|
}
|
|
|
|
public Uni<?> delete(SecurityCtx securityCtx, CompetitionSystem system, Long id) {
|
|
return repository.find("systemId = ?1 AND system = ?2", id, system).firstResult()
|
|
.onItem().ifNull().failWith(() -> new DNotFoundException("Match not found"))
|
|
.call(o2 -> permService.hasEditPerm(securityCtx, o2.getPoule().getCompet()))
|
|
.chain(data -> Panache.withTransaction(() -> repository.delete(data)));
|
|
}
|
|
}
|