Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: search with only field specified #47

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
Loading