From a0bc52789f227fd5325d2c355210173491422942 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Wed, 11 Sep 2024 16:03:49 +0530 Subject: [PATCH] chore: implementation of includeReference --- .../com/contentstack/cms/stack/Entry.java | 32 +++++++++++-------- .../contentstack/cms/stack/EntryService.java | 7 ++++ .../cms/stack/EntryFieldUnitTests.java | 18 +++-------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/contentstack/cms/stack/Entry.java b/src/main/java/com/contentstack/cms/stack/Entry.java index f3774d10..138dc39d 100644 --- a/src/main/java/com/contentstack/cms/stack/Entry.java +++ b/src/main/java/com/contentstack/cms/stack/Entry.java @@ -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; @@ -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 params) { - for (Map.Entry entry : params.entrySet()) { - addParam(entry.getKey(), entry.getValue()); - } + this.params.putAll(params); return this; } @@ -135,6 +126,19 @@ protected Entry clearParams() { return this; } + public Call includeReference(@NotNull Object referenceFields) { + List 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); +} /** * Fetches the list of all the entries of a particular content type. * It also returns the content of each entry in JSON format. You can also diff --git a/src/main/java/com/contentstack/cms/stack/EntryService.java b/src/main/java/com/contentstack/cms/stack/EntryService.java index 3fe6d220..875a62ba 100644 --- a/src/main/java/com/contentstack/cms/stack/EntryService.java +++ b/src/main/java/com/contentstack/cms/stack/EntryService.java @@ -5,6 +5,7 @@ import retrofit2.Call; import retrofit2.http.*; +import java.util.List; import java.util.Map; public interface EntryService { @@ -14,6 +15,12 @@ Call fetch( @HeaderMap Map headers, @Path("content_type_uid") String contentTypeUid, @QueryMap(encoded = true) Map queryParameter); + + @GET("content_types/{content_type_uid}/entries") + Call referCall( + @HeaderMap Map headers, + @Path("content_type_uid") String contentTypeUid, + @Query("include[]") List values); @Headers("Content-Type: application/json") @GET("content_types/{content_type_uid}/entries/{entry_uid}") diff --git a/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java b/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java index b8efbf50..746803fa 100644 --- a/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java +++ b/src/test/java/com/contentstack/cms/stack/EntryFieldUnitTests.java @@ -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 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