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

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

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

问题

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

@Setter
@Getter
public class ProductIsAvailableDto {
    private String productId;
    private Boolean isAvailable;
}

private final CMAClient contentfulClientForUpdate;

for (ProductIsAvailableDto product : productIsAvailableDtoList) {
    CMAEntry cmaEntry = contentfulClientForUpdate.entries().fetchOne(product.getProductId());
    cmaEntry.setField(ContentfulFieldsConstants.IS_AVAILABLE, DEFAULT_LOCALE, product.getIsAvailable());
    cmaEntry = contentfulClientForUpdate.entries().update(cmaEntry);
    contentfulClientForUpdate.entries().publish(cmaEntry);
}

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

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

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

HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("ids", new Gson().toJson(productIsAvailableDtoList.stream()
        .map(ProductIsAvailableDto::getProductId)
        .collect(Collectors.toList())
));
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.

@Setter
@Getter
public class ProductIsAvailableDto {
    private String productId;
    private Boolean isAvailable;
}

private final CMAClient contentfulClientForUpdate;

for (ProductIsAvailableDto product : productIsAvailableDtoList) {
    CMAEntry cmaEntry = contentfulClientForUpdate.entries().fetchOne(product.getProductId());
    cmaEntry.setField(ContentfulFieldsConstants.IS_AVAILABLE, DEFAULT_LOCALE, product.getIsAvailable());
    cmaEntry = contentfulClientForUpdate.entries().update(cmaEntry);
            contentfulClientForUpdate.entries().publish(cmaEntry);
}

I faced a method as below.

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.

HashMap&lt;String, String&gt; hashMap = new HashMap&lt;&gt;();
        hashMap.put(&quot;ids&quot;, new Gson().toJson(productIsAvailableDtoList.stream()
                .map(ProductIsAvailableDto::getProductId)
                .collect(Collectors.toList())
        ));
        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

HashMap<String, String> hashMap = new HashMap<>();
hashMap.put(
  "sys.id[in]", 
  productIsAvailableDtoList.stream()
    .map(ProductIsAvailableDto::getProductId)
    .collect(Collectors.joining(","))
);
CMAArray<CMAEntry> cmaArray = contentfulClientForUpdate.entries().fetchAll(hashMap);
英文:
HashMap&lt;String, String&gt; hashMap = new HashMap&lt;&gt;();
hashMap.put(
  &quot;sys.id[in]&quot;, 
  productIsAvailableDtoList.stream()
    .map(ProductIsAvailableDto::getProductId)
    .collect(Collectors.joining(&quot;,&quot;)
));
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:

确定