From 6bdad297079bac543da759f01ef110277321f937 Mon Sep 17 00:00:00 2001 From: anthony-frage-ouest-france <156292537+anthony-frage-ouest-france@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:27:08 +0100 Subject: [PATCH] feat: search with only field specified (#47) --- .../postgrest/annotations/Select.java | 6 ++++++ .../postgrest/model/impl/SelectFilter.java | 14 +++++++++++++- .../querydsl/postgrest/utils/FilterUtils.java | 2 +- .../PostgrestRepositoryGetMockTest.java | 16 ++++++++++++++++ .../postgrest/app/PostLightRepository.java | 19 +++++++++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/app/PostLightRepository.java diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/annotations/Select.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/annotations/Select.java index a63d878..7404a3e 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/annotations/Select.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/annotations/Select.java @@ -23,4 +23,10 @@ * @return alias name */ String alias() default ""; + + /** + * Select only the fields specified + * @return true if only the fields specified + */ + boolean only() default false; } diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/impl/SelectFilter.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/impl/SelectFilter.java index 30a28a2..8fe7cb4 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/impl/SelectFilter.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/model/impl/SelectFilter.java @@ -34,13 +34,20 @@ public class SelectFilter implements Filter, FilterVisitor { * @return select filter */ public static Filter of(List selectAttributes) { - return new SelectFilter(selectAttributes, true); + return new SelectFilter(selectAttributes, !isOnly(selectAttributes)); } public static Filter only(List selectAttributes) { return new SelectFilter(selectAttributes, false); } + private static boolean isOnly(List selectAttributes) { + if(selectAttributes == null || selectAttributes.isEmpty()) { + return false; + } + return selectAttributes.stream().findFirst().map(Attribute::isOnlyAttribute).orElse(false); + } + public Filter append(List selectAttributes) { if(selectAttributes == null || selectAttributes.isEmpty()) { return this; @@ -75,5 +82,10 @@ public static class Attribute { */ private final String[] value; + /** + * only attribute + */ + private final boolean onlyAttribute; + } } diff --git a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/utils/FilterUtils.java b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/utils/FilterUtils.java index 7db212c..e141afd 100644 --- a/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/utils/FilterUtils.java +++ b/querydsl-postgrest/src/main/java/fr/ouestfrance/querydsl/postgrest/utils/FilterUtils.java @@ -27,7 +27,7 @@ public static List getSelectAttributes(Object... objects) { if(object != null) { Select[] clazzAnnotation = object.getClass().getAnnotationsByType(Select.class); if (clazzAnnotation.length > 0) { - attributes.addAll(Arrays.stream(clazzAnnotation).map(x -> new SelectFilter.Attribute(x.alias(), x.value())).toList()); + attributes.addAll(Arrays.stream(clazzAnnotation).map(x -> new SelectFilter.Attribute(x.alias(), x.value(), x.only())).toList()); } } } diff --git a/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepositoryGetMockTest.java b/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepositoryGetMockTest.java index d9d88fc..e1a31bf 100644 --- a/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepositoryGetMockTest.java +++ b/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/PostgrestRepositoryGetMockTest.java @@ -30,9 +30,12 @@ class PostgrestRepositoryGetMockTest extends AbstractRepositoryMockTest { private PostgrestRepository repository; + private PostgrestRepository repositoryLight; + @BeforeEach void beforeEach() { repository = new PostRepository(webClient); + repositoryLight = new PostLightRepository(webClient); } @Test @@ -88,6 +91,19 @@ void shouldSearchWithPaginate() { assertEquals(0, search.getPageable().previous().getPageNumber()); } + @Test + void shouldSearchWithLightRepository() { + ArgumentCaptor>> queryArgs = multiMapCaptor(); + ArgumentCaptor>> headerArgs = multiMapCaptor(); + when(webClient.search(anyString(), queryArgs.capture(), headerArgs.capture(), eq(Post.class))).thenReturn(RangeResponse.of(new Post(), new Post())); + repositoryLight.search(new PostRequest(), Pageable.ofSize(10)); + + // Assert query captors + Map> queries = queryArgs.getValue(); + assertEquals("userId,id,title,body", queries.get("select").stream().findFirst().orElseThrow()); + + } + @Test void shouldFindById() { ArgumentCaptor>> queryArgs = multiMapCaptor(); diff --git a/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/app/PostLightRepository.java b/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/app/PostLightRepository.java new file mode 100644 index 0000000..2b05ba7 --- /dev/null +++ b/querydsl-postgrest/src/test/java/fr/ouestfrance/querydsl/postgrest/app/PostLightRepository.java @@ -0,0 +1,19 @@ +package fr.ouestfrance.querydsl.postgrest.app; + +import fr.ouestfrance.querydsl.postgrest.PostgrestClient; +import fr.ouestfrance.querydsl.postgrest.PostgrestRepository; +import fr.ouestfrance.querydsl.postgrest.annotations.Header; +import fr.ouestfrance.querydsl.postgrest.annotations.PostgrestConfiguration; +import fr.ouestfrance.querydsl.postgrest.annotations.Select; +import fr.ouestfrance.querydsl.postgrest.model.Prefer; + +@PostgrestConfiguration(resource = "posts") +@Select(value = {"userId","id", "title", "body"},only = true) +@Header(key = Prefer.HEADER, value = Prefer.Return.REPRESENTATION) +public class PostLightRepository extends PostgrestRepository { + + public PostLightRepository(PostgrestClient client) { + super(client); + } + +}