Skip to content

Commit

Permalink
chore: implementation of includeReference
Browse files Browse the repository at this point in the history
  • Loading branch information
reeshika-h committed Sep 11, 2024
1 parent 1b22e12 commit a0bc527
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
32 changes: 18 additions & 14 deletions src/main/java/com/contentstack/cms/stack/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import retrofit2.Call;
import retrofit2.Retrofit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -85,26 +88,14 @@ public Entry addHeader(@NotNull String key, @NotNull String value) {
*/
@Override
public Entry addParam(@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 + includeCounter++, value);
}
} else {
this.params.put(key, value);
}
this.params.put(key, value);
return this;
}


@Override
public Entry addParams(@NotNull HashMap<String, Object> params) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
addParam(entry.getKey(), entry.getValue());
}
this.params.putAll(params);
return this;
}

Expand Down Expand Up @@ -135,6 +126,19 @@ 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));
} 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);
}
/**
* <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
7 changes: 7 additions & 0 deletions src/main/java/com/contentstack/cms/stack/EntryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import retrofit2.Call;
import retrofit2.http.*;

import java.util.List;
import java.util.Map;

public interface EntryService {
Expand All @@ -14,6 +15,12 @@ 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);

@Headers("Content-Type: application/json")
@GET("content_types/{content_type_uid}/entries/{entry_uid}")
Expand Down
18 changes: 4 additions & 14 deletions src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,12 @@ void testSingleEntryQuery() {
Request resp = entryInstance.fetch().request();
Assertions.assertEquals("include_publish_details=true&locale=en-us&include_workflow=false", resp.url().query());
}

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

@Test
void testaddParams() throws IOException{
HashMap<String, Object> paramslist = new HashMap<>();
paramslist.put("include[]", new String[]{"reference", "navigation_menu.page_reference"});
entryInstance.addParams(paramslist);
Request request = entryInstance.find().request();
Assertions.assertEquals("include[]1=reference&include[]2=navigation_menu.page_reference", request.url().query());
void testIncludeReference() {
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());
}

@Test
Expand Down

0 comments on commit a0bc527

Please sign in to comment.