Skip to content

Commit

Permalink
[backend] refact
Browse files Browse the repository at this point in the history
  • Loading branch information
savacano28 authored and johanah29 committed Oct 25, 2024
1 parent a46558b commit 5361ea0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.openbas.utils.AtomicTestingMapper.ExpectationResultsByType;
import io.openbas.utils.ResultUtils;
import io.openbas.utils.pagination.SearchPaginationInput;
import jakarta.persistence.criteria.Join;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -633,11 +634,14 @@ public List<ExerciseSimple> exercises() {
@LogExecutionTime
@PostMapping(EXERCISE_URI + "/search")
public Page<ExerciseSimple> exercises(@RequestBody @Valid final SearchPaginationInput searchPaginationInput) {
Map<String, Join<Base, Base>> joinMap = new HashMap<>();

if (currentUser().isAdmin()) {
return buildPaginationCriteriaBuilder(
this.exerciseService::exercises,
searchPaginationInput,
Exercise.class
Exercise.class,
joinMap
);
} else {
return buildPaginationCriteriaBuilder(
Expand All @@ -647,7 +651,8 @@ public Page<ExerciseSimple> exercises(@RequestBody @Valid final SearchPagination
pageable
),
searchPaginationInput,
Exercise.class
Exercise.class,
joinMap
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import static io.openbas.config.SessionHelper.currentUser;
import static io.openbas.database.criteria.GenericCriteria.countQuery;
import static io.openbas.utils.Constants.ARTICLES;
import static io.openbas.utils.JpaUtils.createJoinArrayAggOnId;
import static io.openbas.utils.JpaUtils.arrayAggOnId;
import static io.openbas.utils.StringUtils.duplicateString;
import static io.openbas.utils.pagination.SortUtilsCriteriaBuilder.toSortCriteriaBuilder;
import static java.time.Instant.now;
Expand Down Expand Up @@ -92,12 +92,13 @@ public List<ExerciseSimple> exercises() {
public Page<ExerciseSimple> exercises(
Specification<Exercise> specification,
Specification<Exercise> specificationCount,
Pageable pageable) {
Pageable pageable,
Map<String, Join<Base, Base>> joinMap) {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<Exercise> exerciseRoot = cq.from(Exercise.class);
select(cb, cq, exerciseRoot);
select(cb, cq, exerciseRoot, joinMap);

// -- Text Search and Filters --
if (specification != null) {
Expand Down Expand Up @@ -138,11 +139,16 @@ public Page<ExerciseSimple> exercises(

// -- SELECT --

private void select(CriteriaBuilder cb, CriteriaQuery<Tuple> cq, Root<Exercise> exerciseRoot) {
private void select(CriteriaBuilder cb, CriteriaQuery<Tuple> cq, Root<Exercise> exerciseRoot,
Map<String, Join<Base, Base>> joinMap) {
// Array aggregations
Expression<String[]> tagIdsExpression = createJoinArrayAggOnId(cb, exerciseRoot, "tags");
Expression<String[]> injectIdsExpression = createJoinArrayAggOnId(cb, exerciseRoot, "injects");
Join<Base, Base> exerciseTagsJoin = exerciseRoot.join("tags", JoinType.LEFT);
joinMap.put("tags", exerciseTagsJoin);
Expression<String[]> tagIdsExpression = arrayAggOnId(cb, exerciseTagsJoin);

Join<Base, Base> injectsJoin = exerciseRoot.join("injects", JoinType.LEFT);
joinMap.put("injects", injectsJoin);
Expression<String[]> injectIdsExpression = arrayAggOnId(cb, injectsJoin);
// SELECT
cq.multiselect(
exerciseRoot.get("id").alias("exercise_id"),
Expand Down

0 comments on commit 5361ea0

Please sign in to comment.