英文:
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<String> 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<java.lang.String,java.lang.String>)
But I really can't understand how to specify the query param. I tried to make smth as below.
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);
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<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);
It works) Thanks for attention.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论