如何使用Contentful CMA从Contentful获取一组CMAEntry,其根据它们的ID进行检索。

huangapple go评论186阅读模式
英文:

How to fetch collection of CMAEntry by their ids from Contentful using Contentful CMA

问题

我使用 Contentful CMA。我有List<String> entriesIds。我想根据它们的ID从 Contentful 空间中获取条目,以便我可以编辑它们并将更改发布到 Contentful。下面的代码完美地工作,但我希望只进行1次请求来获取这些条目。

  1. @Setter
  2. @Getter
  3. public class ProductIsAvailableDto {
  4. private String productId;
  5. private Boolean isAvailable;
  6. }
  7. private final CMAClient contentfulClientForUpdate;
  8. for (ProductIsAvailableDto product : productIsAvailableDtoList) {
  9. CMAEntry cmaEntry = contentfulClientForUpdate.entries().fetchOne(product.getProductId());
  10. cmaEntry.setField(ContentfulFieldsConstants.IS_AVAILABLE, DEFAULT_LOCALE, product.getIsAvailable());
  11. cmaEntry = contentfulClientForUpdate.entries().update(cmaEntry);
  12. contentfulClientForUpdate.entries().publish(cmaEntry);
  13. }

我遇到了一个如下的方法。

  1. com.contentful.java.cma.ModuleEntries#fetchAll(java.util.Map<String,String>)

但我真的不明白如何指定查询参数。我尝试了以下方法。

  1. HashMap<String, String> hashMap = new HashMap<>();
  2. hashMap.put("ids", new Gson().toJson(productIsAvailableDtoList.stream()
  3. .map(ProductIsAvailableDto::getProductId)
  4. .collect(Collectors.toList())
  5. ));
  6. CMAArray<CMAEntry> cmaArray = contentfulClientForUpdate.entries().fetchAll(hashMap);

但它不起作用。在官方文档中,我只找到了按ID获取单个条目的机会,但我需要按ID获取条目列表。如果有人能帮助我,那将是非常棒的。

英文:

I use Contentful CMA. I have List&lt;String&gt; entriesIds. I want to fetch entries by their ids from Contentful space so that I can edit them and publish my changes to Contentful. The code below works perfectly fine, but I want to fetch entries making just 1 request.

  1. @Setter
  2. @Getter
  3. public class ProductIsAvailableDto {
  4. private String productId;
  5. private Boolean isAvailable;
  6. }
  7. private final CMAClient contentfulClientForUpdate;
  8. for (ProductIsAvailableDto product : productIsAvailableDtoList) {
  9. CMAEntry cmaEntry = contentfulClientForUpdate.entries().fetchOne(product.getProductId());
  10. cmaEntry.setField(ContentfulFieldsConstants.IS_AVAILABLE, DEFAULT_LOCALE, product.getIsAvailable());
  11. cmaEntry = contentfulClientForUpdate.entries().update(cmaEntry);
  12. contentfulClientForUpdate.entries().publish(cmaEntry);
  13. }

I faced a method as below.

  1. com.contentful.java.cma.ModuleEntries#fetchAll(java.util.Map&lt;java.lang.String,java.lang.String&gt;)

But I really can't understand how to specify the query param. I tried to make smth as below.

  1. HashMap&lt;String, String&gt; hashMap = new HashMap&lt;&gt;();
  2. hashMap.put(&quot;ids&quot;, new Gson().toJson(productIsAvailableDtoList.stream()
  3. .map(ProductIsAvailableDto::getProductId)
  4. .collect(Collectors.toList())
  5. ));
  6. CMAArray&lt;CMAEntry&gt; cmaArray = contentfulClientForUpdate.entries().fetchAll(hashMap);

But it does not work. In official docs I found only the opportunity to fetch a single entry by id, but I need to fetch a list of entries by ids. It would be great if someone could help me.

答案1

得分: 0

  1. HashMap<String, String> hashMap = new HashMap<>();
  2. hashMap.put(
  3. "sys.id[in]",
  4. productIsAvailableDtoList.stream()
  5. .map(ProductIsAvailableDto::getProductId)
  6. .collect(Collectors.joining(","))
  7. );
  8. CMAArray<CMAEntry> cmaArray = contentfulClientForUpdate.entries().fetchAll(hashMap);
英文:
  1. HashMap&lt;String, String&gt; hashMap = new HashMap&lt;&gt;();
  2. hashMap.put(
  3. &quot;sys.id[in]&quot;,
  4. productIsAvailableDtoList.stream()
  5. .map(ProductIsAvailableDto::getProductId)
  6. .collect(Collectors.joining(&quot;,&quot;)
  7. ));
  8. CMAArray&lt;CMAEntry&gt; cmaArray = contentfulClientForUpdate.entries().fetchAll(hashMap);

It works) Thanks for attention.

huangapple
  • 本文由 发表于 2020年9月28日 23:59:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64105625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定