Skip to content

Commit

Permalink
feat: search with only field specified (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-frage-ouest-france authored Nov 26, 2024
1 parent 08ff6c9 commit 6bdad29
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ public class SelectFilter implements Filter, FilterVisitor {
* @return select filter
*/
public static Filter of(List<Attribute> selectAttributes) {
return new SelectFilter(selectAttributes, true);
return new SelectFilter(selectAttributes, !isOnly(selectAttributes));
}

public static Filter only(List<Attribute> selectAttributes) {
return new SelectFilter(selectAttributes, false);
}

private static boolean isOnly(List<Attribute> selectAttributes) {
if(selectAttributes == null || selectAttributes.isEmpty()) {
return false;
}
return selectAttributes.stream().findFirst().map(Attribute::isOnlyAttribute).orElse(false);
}

public Filter append(List<Attribute> selectAttributes) {
if(selectAttributes == null || selectAttributes.isEmpty()) {
return this;
Expand Down Expand Up @@ -75,5 +82,10 @@ public static class Attribute {
*/
private final String[] value;

/**
* only attribute
*/
private final boolean onlyAttribute;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static List<Attribute> 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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ class PostgrestRepositoryGetMockTest extends AbstractRepositoryMockTest {

private PostgrestRepository<Post> repository;

private PostgrestRepository<Post> repositoryLight;

@BeforeEach
void beforeEach() {
repository = new PostRepository(webClient);
repositoryLight = new PostLightRepository(webClient);
}

@Test
Expand Down Expand Up @@ -88,6 +91,19 @@ void shouldSearchWithPaginate() {
assertEquals(0, search.getPageable().previous().getPageNumber());
}

@Test
void shouldSearchWithLightRepository() {
ArgumentCaptor<Map<String, List<String>>> queryArgs = multiMapCaptor();
ArgumentCaptor<Map<String, List<String>>> 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<String, List<String>> queries = queryArgs.getValue();
assertEquals("userId,id,title,body", queries.get("select").stream().findFirst().orElseThrow());

}

@Test
void shouldFindById() {
ArgumentCaptor<Map<String, List<String>>> queryArgs = multiMapCaptor();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Post> {

public PostLightRepository(PostgrestClient client) {
super(client);
}

}

0 comments on commit 6bdad29

Please sign in to comment.