Skip to content
Merged

Next #89

Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore : latest implementation for includeReference
  • Loading branch information
reeshika-h committed Sep 12, 2024
commit 447c3d9b8af2199fda2c1eabd3d15a9a5b55f4f7
29 changes: 20 additions & 9 deletions src/main/java/com/contentstack/cms/stack/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,30 @@ protected Entry clearParams() {
return this;
}

public Call<ResponseBody> includeReference(@NotNull Object referenceFields) {
List<String> referenceList = new ArrayList<>();
if (referenceFields instanceof String) {
referenceList.add((String) referenceFields);
} else if (referenceFields instanceof String[]) {
referenceList.addAll(Arrays.asList((String[]) referenceFields));
protected Entry addToParams(@NotNull String key, @NotNull Object value){
if (key.equals("include[]")) {
if (value instanceof String[]) {
for (String item : (String[]) value) {
this.params.put(key + includeCounter++, item);
}
} else if (value instanceof String) {
this.params.put(key, value);
}
} else {
this.params.put(key, value);
}
return this;
}

public Call<ResponseBody> includeReference(@NotNull Object referenceField){
if (referenceField instanceof String || referenceField instanceof String[]) {
addToParams("include[]", referenceField);
} else {
throw new IllegalArgumentException("Reference fields must be a String or an array of Strings");
}
validateCT();
validateEntry();
return this.service.referCall(this.headers, this.contentTypeUid, referenceList);
}
return this.service.fetch(this.headers, this.contentTypeUid, this.params);
}
/**
* <b>Fetches the list of all the entries of a particular content type.</b>
* It also returns the content of each entry in JSON format. You can also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ public interface EntryService {
Call<ResponseBody> fetch(
@HeaderMap Map<String, Object> headers,
@Path("content_type_uid") String contentTypeUid,
@QueryMap(encoded = true) Map<String, Object> queryParameter);

@GET("content_types/{content_type_uid}/entries")
Call<ResponseBody> referCall(
@HeaderMap Map<String, Object> headers,
@Path("content_type_uid") String contentTypeUid,
@Query("include[]") List<String> values);
@QueryMap(encoded = true) Map<String, Object> queryParameter);

@Headers("Content-Type: application/json")
@GET("content_types/{content_type_uid}/entries/{entry_uid}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,28 @@ void testSingleEntryQuery() {
Request resp = entryInstance.fetch().request();
Assertions.assertEquals("include_publish_details=true&locale=en-us&include_workflow=false", resp.url().query());
}

@Test
void testIncludeReferenceSingleReference(){
Request request = entryInstance.includeReference("reference").request();
Assertions.assertEquals("include[]=reference", request.url().query());
}
@Test
void testIncludeReferenceMultipleReferences() {
String[] array = {"reference","navigation_menu.page_reference"};
Request req = entryInstance.includeReference(array).request();
Assertions.assertEquals("include[]=reference&include[]=navigation_menu.page_reference", req.url().query());
Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", req.url().query());
}

@Test
void testIncludeReferenceSingleReference() {
Request req = entryInstance.includeReference("reference").request();

Assertions.assertEquals("include[]=reference", req.url().query());
void testIncludeReferenceMultipleReferencesWithParams() throws IOException {
entryInstance.clearParams();
entryInstance.addParam("locale", "en-us");
String[] array = {"reference","navigation_menu.page_reference"};
entryInstance.addParam("include_workflow", false);
entryInstance.addParam("include_publish_details", true);
Request req = entryInstance.includeReference(array).request();
Assertions.assertEquals("include[]1=reference&include_publish_details=true&include[]2=navigation_menu.page_reference&locale=en-us&include_workflow=false", req.url().query());

}

@Test
Expand Down