feat(search): extended search, sort options, live tag filter, result count #183

Merged
marcel merged 26 commits from feat/issue-180-extended-search-sort into main 2026-04-06 19:18:12 +02:00
2 changed files with 41 additions and 1 deletions
Showing only changes of commit bc397048b7 - Show all commits

View File

@@ -291,11 +291,15 @@ public class DocumentService {
.and(hasTagPartial(tagQ))
.and(hasStatus(status));
Sort springSort = resolveSort(sort, dir);
if (sort == DocumentSort.RECEIVER) {
List<Document> results = documentRepository.findAll(spec);
return sortByFirstReceiver(results, dir);
}
if (sort == DocumentSort.SENDER) {
List<Document> results = documentRepository.findAll(spec);
return sortBySender(results, dir);
}
Sort springSort = resolveSort(sort, dir);
return documentRepository.findAll(spec, springSort);
}
@@ -312,6 +316,22 @@ public class DocumentService {
};
}
private List<Document> sortBySender(List<Document> documents, String dir) {
boolean ascending = "ASC".equalsIgnoreCase(dir);
Comparator<String> nullSafeComparator = (a, b) -> {
if (a.isEmpty() && b.isEmpty()) return 0;
if (a.isEmpty()) return ascending ? 1 : -1;
if (b.isEmpty()) return ascending ? -1 : 1;
return ascending ? a.compareTo(b) : b.compareTo(a);
};
return documents.stream()
.sorted(Comparator.comparing(doc -> {
Person s = doc.getSender();
return s != null ? s.getLastName() + " " + s.getFirstName() : "";
}, nullSafeComparator))
.toList();
}
private List<Document> sortByFirstReceiver(List<Document> documents, String dir) {
boolean ascending = "ASC".equalsIgnoreCase(dir);
Comparator<String> nullSafeComparator = (a, b) -> {

View File

@@ -10,6 +10,7 @@ import org.raddatz.familienarchiv.dto.DocumentUpdateDTO;
import org.raddatz.familienarchiv.dto.IncompleteDocumentDTO;
import org.raddatz.familienarchiv.exception.DomainException;
import org.raddatz.familienarchiv.model.Document;
import org.raddatz.familienarchiv.model.DocumentSort;
import org.raddatz.familienarchiv.model.DocumentStatus;
import org.raddatz.familienarchiv.model.Person;
import org.raddatz.familienarchiv.model.Tag;
@@ -1273,4 +1274,23 @@ class DocumentServiceTest {
verify(documentRepository).findConversation(eq(senderId), eq(receiverId), any(), any(), eq(sort));
verify(documentRepository, never()).findSinglePersonCorrespondence(any(), any(), any(), any());
}
// ─── searchDocuments — SENDER sort includes documents with null sender ─────
@Test
void searchDocuments_senderSort_includesDocumentsWithNullSender() {
Person alice = Person.builder().id(UUID.randomUUID()).firstName("Alice").lastName("Ziegler").build();
Document withSender = Document.builder().id(UUID.randomUUID()).title("Has Sender").sender(alice).build();
Document noSender = Document.builder().id(UUID.randomUUID()).title("No Sender").build();
// The repository returns both documents (no filtering by sender)
when(documentRepository.findAll(any(org.springframework.data.jpa.domain.Specification.class)))
.thenReturn(List.of(withSender, noSender));
List<Document> result = documentService.searchDocuments(
null, null, null, null, null, null, null, null, DocumentSort.SENDER, "asc");
assertThat(result).hasSize(2);
assertThat(result).extracting(Document::getTitle).containsExactly("Has Sender", "No Sender");
}
}