wip: send competition data

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

View File

@ -4,10 +4,7 @@ import fr.titionfire.ffsaf.utils.CompetitionSystem;
import fr.titionfire.ffsaf.utils.ScoreEmbeddable;
import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;
import java.util.ArrayList;
import java.util.List;
@ -19,6 +16,7 @@ import java.util.List;
@RegisterForReflection
@Entity
@ToString
@Table(name = "match")
public class MatchModel {
@ -30,24 +28,27 @@ public class MatchModel {
CompetitionSystem system;
Long systemId;
@ManyToOne
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "c1", referencedColumnName = "id")
MembreModel c1_id = null;
String c1_str = null;
@ManyToOne
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "c2", referencedColumnName = "id")
MembreModel c2_id = null;
String c2_str = null;
@Column(name = "id_poule")
Long poule;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id_poule", referencedColumnName = "id")
PouleModel poule = null;
long poule_ord = 0;
@ElementCollection
boolean isEnd = true;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "score", joinColumns = @JoinColumn(name = "id_match"))
List<ScoreEmbeddable> scores = new ArrayList<>();

View File

@ -33,11 +33,11 @@ public class PouleModel {
@JoinColumn(name = "id_compet", referencedColumnName = "id")
CompetitionModel compet;
@OneToMany
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "id_poule", referencedColumnName = "id")
List<MatchModel> matchs;
@OneToMany
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "id_poule", referencedColumnName = "id")
List<TreeModel> tree;

View File

@ -25,15 +25,15 @@ public class TreeModel {
Integer level;
@ManyToOne
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "match_id", referencedColumnName = "id")
MatchModel match;
@ManyToOne
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
@JoinColumn(referencedColumnName = "id")
TreeModel left;
@ManyToOne
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
@JoinColumn(referencedColumnName = "id")
TreeModel right;
}

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))));
}
}

View File

@ -1,11 +1,10 @@
package fr.titionfire.ffsaf.domain.service;
import fr.titionfire.ffsaf.data.model.ClubModel;
import fr.titionfire.ffsaf.data.model.PouleModel;
import fr.titionfire.ffsaf.data.repository.CompetitionRepository;
import fr.titionfire.ffsaf.data.repository.PouleRepository;
import fr.titionfire.ffsaf.data.repository.TreeRepository;
import fr.titionfire.ffsaf.data.model.*;
import fr.titionfire.ffsaf.data.repository.*;
import fr.titionfire.ffsaf.rest.data.PouleData;
import fr.titionfire.ffsaf.rest.data.PouleFullData;
import fr.titionfire.ffsaf.rest.data.TreeData;
import fr.titionfire.ffsaf.utils.CompetitionSystem;
import fr.titionfire.ffsaf.utils.GroupeUtils;
import io.quarkus.hibernate.reactive.panache.Panache;
@ -15,10 +14,14 @@ import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.eclipse.microprofile.jwt.JsonWebToken;
import org.hibernate.reactive.mutiny.Mutiny;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Stream;
@WithSession
@ApplicationScoped
@ -30,9 +33,15 @@ public class PouleService {
@Inject
CompetitionRepository competRepository;
@Inject
MatchRepository matchRepository;
@Inject
TreeRepository treeRepository;
@Inject
CombRepository combRepository;
public Uni<PouleData> getById(Consumer<ClubModel> checkPerm, CompetitionSystem system, Long id) {
return repository.find("systemId = ?1 AND system = ?2", id, system)
.firstResult()
@ -79,10 +88,181 @@ public class PouleService {
}).map(PouleData::fromModel);
}
public Uni<?> delete(Consumer<ClubModel> checkPerm, Long id) {
return repository.findById(id)
private MatchModel findMatch(List<MatchModel> matchModelList, Long id) {
return matchModelList.stream().filter(m -> m.getSystemId().equals(id))
.findFirst().orElse(null);
}
private TreeModel findNode(List<TreeModel> node, Long match_id) {
return node.stream().filter(m -> m.getMatch().getSystemId().equals(match_id))
.findFirst().orElse(null);
}
private void flatTreeChild(TreeModel current, ArrayList<TreeModel> node) {
if (current != null) {
node.add(current);
flatTreeChild(current.getLeft(), node);
flatTreeChild(current.getRight(), node);
}
}
private void flatTreeChild(TreeData current, ArrayList<TreeData> node) {
if (current != null) {
node.add(current);
flatTreeChild(current.getLeft(), node);
flatTreeChild(current.getRight(), node);
}
}
private Uni<TreeModel> persisteTree(TreeData data, List<TreeModel> node, PouleModel poule,
List<MatchModel> matchModelList) {
TreeModel mm = findNode(node, data.getMatch());
if (mm == null) {
mm = new TreeModel();
mm.setId(null);
}
mm.setLevel(data.getLevel());
mm.setPoule(poule.getId());
mm.setMatch(findMatch(matchModelList, data.getMatch()));
return Uni.createFrom().item(mm)
.call(o -> (data.getLeft() == null ? Uni.createFrom().nullItem().invoke(o1 -> o.setLeft(null)) :
persisteTree(data.getLeft(), node, poule, matchModelList).invoke(o::setLeft)))
.call(o -> (data.getRight() == null ? Uni.createFrom().nullItem().invoke(o1 -> o.setRight(null)) :
persisteTree(data.getRight(), node, poule, matchModelList).invoke(o::setRight)))
.chain(o -> Panache.withTransaction(() -> treeRepository.persist(o)));
}
public Uni<?> syncPoule(CompetitionSystem system, PouleFullData data) {
System.out.println(data);
return repository.find("systemId = ?1 AND system = ?2", data.getId(), system)
.firstResult()
.onItem().ifNull().switchTo(
() -> competRepository.findById(data.getCompet())
.onItem().ifNull().failWith(() -> new RuntimeException("Compet not found"))
.map(o -> {
PouleModel model = new PouleModel();
model.setId(null);
model.setSystem(system);
model.setSystemId(data.getId());
model.setMatchs(new ArrayList<>());
model.setTree(new ArrayList<>());
model.setCompet(o);
return model;
}))
.call(o -> Mutiny.fetch(o.getMatchs()))
.call(o -> Mutiny.fetch(o.getTree()))
.map(o -> {
o.setName(data.getName());
o.setType(data.getType());
WorkData workData = new WorkData();
workData.poule = o;
return workData;
})
.call(o -> Panache.withTransaction(() -> repository.persist(o.poule)))
.call(o -> (data.getMatches() == null || data.getMatches().isEmpty()) ? Uni.createFrom().nullItem() :
Uni.createFrom()
.item(data.getMatches().stream().flatMap(m -> Stream.of(m.getC1_id(), m.getC2_id())
.filter(Objects::nonNull)).distinct().toList())
.chain(ids -> ids.isEmpty() ? Uni.createFrom().nullItem()
: combRepository.list("id IN ?1", ids)
.invoke(o2 -> o2.forEach(m -> o.membres.put(m.getId(), m)))
)
)
.invoke(in -> {
ArrayList<TreeModel> node = new ArrayList<>();
for (TreeModel treeModel : in.poule.getTree())
flatTreeChild(treeModel, node);
ArrayList<TreeData> new_node = new ArrayList<>();
for (TreeData treeModel : data.getTrees())
flatTreeChild(treeModel, new_node);
in.toRmNode = node.stream().filter(m -> new_node.stream()
.noneMatch(m2 -> m2.getMatch().equals(m.getMatch().getSystemId())))
.map(TreeModel::getId).toList();
in.unlinkNode = node;
in.unlinkNode.forEach(n -> {
n.setRight(null);
n.setLeft(null);
});
in.toRmMatch = in.poule.getMatchs().stream()
.filter(m -> data.getMatches().stream().noneMatch(m2 -> m2.getId().equals(m.getSystemId())))
.map(MatchModel::getId).toList();
})
.call(in -> in.unlinkNode.isEmpty() ? Uni.createFrom().nullItem() :
Panache.withTransaction(() -> treeRepository.persist(in.unlinkNode)))
.call(in -> in.toRmNode.isEmpty() ? Uni.createFrom().nullItem() :
Panache.withTransaction(() -> treeRepository.delete("id IN ?1", in.toRmNode)))
.call(in -> in.toRmMatch.isEmpty() ? Uni.createFrom().nullItem() :
Panache.withTransaction(() -> Uni.join().all(
in.toRmMatch.stream().map(l -> matchRepository.deleteById(l)).toList())
.andCollectFailures()))
.call(in -> data.getMatches().isEmpty() ? Uni.createFrom().nullItem() :
Uni.join().all(
data.getMatches().stream().map(m -> {
MatchModel mm = findMatch(in.poule.getMatchs(), m.getId());
if (mm == null) {
mm = new MatchModel();
mm.setId(null);
mm.setSystem(system);
mm.setSystemId(m.getId());
}
mm.setPoule(in.poule);
mm.setPoule_ord(m.getPoule_ord());
mm.setC1_str(m.getC1_str());
mm.setC2_str(m.getC2_str());
mm.setC1_id(in.membres.getOrDefault(m.getC1_id(), null));
mm.setC2_id(in.membres.getOrDefault(m.getC2_id(), null));
mm.setEnd(m.isEnd());
mm.setGroupe(m.getGroupe());
mm.getScores().clear();
mm.getScores().addAll(m.getScores());
MatchModel finalMm = mm;
return Panache.withTransaction(() -> matchRepository.persist(finalMm)
.invoke(o -> in.match.add(o)));
}).toList())
.andCollectFailures())
.call(in -> data.getTrees().isEmpty() ? Uni.createFrom().nullItem() :
Uni.join().all(data.getTrees().stream()
.map(m -> persisteTree(m, in.poule.getTree(), in.poule, in.match)).toList())
.andCollectFailures())
.map(__ -> "OK");
}
private static class WorkData {
PouleModel poule;
HashMap<Long, MembreModel> membres = new HashMap<>();
List<MatchModel> match = new ArrayList<>();
List<Long> toRmMatch;
List<TreeModel> unlinkNode;
List<Long> toRmNode;
}
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("Poule not found"))
.invoke(data -> checkPerm.accept(data.getCompet().getClub()))
.call(o -> Mutiny.fetch(o.getMatchs()))
.call(o -> Mutiny.fetch(o.getTree())
.call(o2 -> o2.isEmpty() ? Uni.createFrom().nullItem() :
Uni.createFrom().item(o2.stream().peek(m -> {
m.setRight(null);
m.setLeft(null);
}).toList())
.call(in -> Panache.withTransaction(() -> treeRepository.persist(in)))
.map(in -> in.stream().map(TreeModel::getId).toList())
.call(in -> in.isEmpty() ? Uni.createFrom().nullItem() :
Panache.withTransaction(() -> treeRepository.delete("id IN ?1", in)))
)
)
.call(o -> o.getMatchs().isEmpty() ? Uni.createFrom().nullItem() :
Panache.withTransaction(() -> Uni.join().all(
o.getMatchs().stream().map(l -> matchRepository.deleteById(l.getId())).toList())
.andCollectFailures()))
.chain(model -> Panache.withTransaction(() -> repository.delete("id", model.getId())));
}
}

View File

@ -0,0 +1,4 @@
package fr.titionfire.ffsaf.domain.service;
public class TreeService {
}

View File

@ -6,6 +6,7 @@ import fr.titionfire.ffsaf.rest.data.MatchData;
import fr.titionfire.ffsaf.rest.exception.DForbiddenException;
import fr.titionfire.ffsaf.utils.CompetitionSystem;
import fr.titionfire.ffsaf.utils.GroupeUtils;
import fr.titionfire.ffsaf.utils.ScoreEmbeddable;
import io.quarkus.security.Authenticated;
import io.quarkus.security.identity.SecurityIdentity;
import io.smallrye.mutiny.Uni;
@ -60,10 +61,17 @@ public class MatchEndpoints {
return service.addOrUpdate(checkPerm, system, data);
}
@POST
@Path("score/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Uni<?> updateScore(@PathParam("id") Long id, List<ScoreEmbeddable> scores) {
return service.updateScore(system, id, scores);
}
@DELETE
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Uni<?> delete(@PathParam("id") Long id) {
return service.delete(checkPerm, id);
return service.delete(checkPerm, system, id);
}
}

View File

@ -3,10 +3,10 @@ package fr.titionfire.ffsaf.rest;
import fr.titionfire.ffsaf.data.model.ClubModel;
import fr.titionfire.ffsaf.domain.service.PouleService;
import fr.titionfire.ffsaf.rest.data.PouleData;
import fr.titionfire.ffsaf.rest.data.PouleFullData;
import fr.titionfire.ffsaf.rest.exception.DForbiddenException;
import fr.titionfire.ffsaf.utils.CompetitionSystem;
import fr.titionfire.ffsaf.utils.GroupeUtils;
import io.quarkus.security.Authenticated;
import io.quarkus.security.identity.SecurityIdentity;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.unchecked.Unchecked;
@ -18,7 +18,7 @@ import org.eclipse.microprofile.jwt.JsonWebToken;
import java.util.List;
import java.util.function.Consumer;
@Authenticated
// @Authenticated
@Path("api/poule/{system}/")
public class PouleEndpoints {
@ -54,15 +54,23 @@ public class PouleEndpoints {
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Uni<PouleData> addOrUpdate(PouleData data) {
return service.addOrUpdate(checkPerm, system, data);
}
@POST
@Path("sync")
@Consumes(MediaType.APPLICATION_JSON)
public Uni<?> syncPoule(PouleFullData data) {
return service.syncPoule(system, data);
}
@DELETE
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Uni<?> delete(@PathParam("id") Long id) {
return service.delete(checkPerm, id);
return service.delete(checkPerm, system, id);
}
}

View File

@ -19,13 +19,18 @@ public class MatchData {
private String c2_str;
private Long poule;
private long poule_ord;
private boolean isEnd = true;
private char groupe;
private List<ScoreEmbeddable> scores;
public static MatchData fromModel(MatchModel model) {
if (model == null)
return null;
return new MatchData(model.getSystemId(), model.getC1_id().getId(), model.getC1_str(), model.getC2_id().getId(),
model.getC2_str(), model.getPoule(), model.getPoule_ord(), model.getScores());
return new MatchData(model.getSystemId(),
(model.getC1_id() == null) ? null : model.getC1_id().getId(), model.getC1_str(),
(model.getC2_id() == null) ? null : model.getC2_id().getId(), model.getC2_str(),
model.getPoule().getId(), model.getPoule_ord(), model.isEnd(), model.getGroupe(),
model.getScores());
}
}

View File

@ -0,0 +1,28 @@
package fr.titionfire.ffsaf.rest.data;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.List;
@Data
@AllArgsConstructor
public class PouleFullData {
private Long id;
private String name;
private Long compet;
private Integer type;
private List<MatchData> matches;
private List<TreeData> trees;
/*public static PouleFullData fromModel(PouleModel pouleModel) {
if (pouleModel == null)
return null;
PouleEntity pouleEntity = PouleEntity.fromModel(pouleModel);
return new PouleFullData(pouleEntity.getId(), pouleEntity.getName(), pouleEntity.getCompet().getId(),
pouleEntity.getType(), pouleModel.getMatchs().stream().map(MatchData::fromModel).toList(),
pouleEntity.getTrees().stream().map(TreeData::fromEntity).toList());
}*/
}

View File

@ -12,17 +12,15 @@ public class TreeData {
private Long id;
private Long poule;
private Integer level;
private MatchData match;
private Long match;
private TreeData left;
private TreeData right;
private TreeData associatedNode;
public static TreeData fromModel(TreeModel model) {
if (model == null)
return null;
return new TreeData(model.getId(), model.getPoule(), model.getLevel(), MatchData.fromModel(model.getMatch()),
fromModel(model.getLeft()),
fromModel(model.getRight()), null);
return new TreeData(model.getId(), model.getPoule(), model.getLevel(), model.getMatch().getId(),
fromModel(model.getLeft()), fromModel(model.getRight()));
}
}

View File

@ -1,12 +1,13 @@
package fr.titionfire.ffsaf.utils;
import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.persistence.Embeddable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import jakarta.persistence.*;
@Getter
@Setter
@AllArgsConstructor