From 1b7239c2bd907f706e8b91b1fefa100eff1c3c0d Mon Sep 17 00:00:00 2001 From: TIm Cremer Date: Tue, 19 Nov 2024 13:52:49 +0100 Subject: [PATCH] Remove duplicates from the search result --- .../communication/repository/CustomPostRepositoryImpl.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tum/cit/aet/artemis/communication/repository/CustomPostRepositoryImpl.java b/src/main/java/de/tum/cit/aet/artemis/communication/repository/CustomPostRepositoryImpl.java index cfdd5f71443e..aae587022c93 100644 --- a/src/main/java/de/tum/cit/aet/artemis/communication/repository/CustomPostRepositoryImpl.java +++ b/src/main/java/de/tum/cit/aet/artemis/communication/repository/CustomPostRepositoryImpl.java @@ -1,6 +1,8 @@ package de.tum.cit.aet.artemis.communication.repository; +import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; import jakarta.persistence.EntityManager; @@ -50,6 +52,8 @@ public Page findPostIdsWithSpecification(Specification specification query.setMaxResults(pageable.getPageSize()); List postIds = query.getResultList(); + // removes all duplicates from the answer posts + List uniquePostIds = new ArrayList<>(new LinkedHashSet<>(postIds)); // Count query CriteriaQuery countQuery = builder.createQuery(Long.class); @@ -66,6 +70,6 @@ public Page findPostIdsWithSpecification(Specification specification Long countResult = entityManager.createQuery(countQuery).getSingleResult(); long count = countResult != null ? countResult : 0L; - return new PageImpl<>(postIds, pageable, count); + return new PageImpl<>(uniquePostIds, pageable, count); } }