0

我想做一个类别树,这里是类别结构

class category{
   private String id;
   private String parentId;
   private List<CategoryDto> children;
}

开头的children是空的,如果null是parent,则parentId是parent,否则是children。我想返回这个结果:

 [
  {
    "id": "61eb379e6d797574df47cfc3",
 
    "parentId": "",
   
    "children": [
      {
        "id": "61ea7e5f1323dd3731dd304a",
       
        "parentId": "61eb379e6d797574df47cfc3",
       
        "children": null
      },
      {
        "id": "61ea8f471323dd3731dd304c",
        "status": "PUBLISHED",
     
        "parentId": "61eb379e6d797574df47cfc3",
       
        "children": null
      }
    ]
  }
]

我用经典编程做了我想用兵变 smallrye 迁移到反应式编程这是我的代码:

@Blocking
public Uni<List<CategoryDto>> getAllCategories(Request doc) {
    log.info("REST request to find Categories : {}", doc);
    Map<String, Object> parent = new HashMap<>();

    List<CategoryDto> all = categoryRepository
            .streamAllAggregators(doc).subscribe().asStream().map(it -> aggregatorMapper.categoryToCategoryDto(it, doc.getLang())).collect(toList());

    List<CategoryDto> listParent = categoryRepository
            .findByParentId("").subscribe().asStream().map(it -> aggregatorMapper.categoryToCategoryDto(it, doc.getLang())).collect(toList());
    List<CategoryDto> listChildren = all.stream()
            .filter(n -> n.getParentId() != null)
            .collect(Collectors.toList());
    List<CategoryDto> categoryDtoList = new ArrayList<>();
    for (int i = 0; i < listParent.size(); i++) {
        categoryDtoList = new ArrayList<>();
        for (int j = 0; j < listChildren.size(); j++) {
            if (listChildren.get(j).getParentId().equals(listParent.get(i).getId().toString())) {
                categoryDtoList.add(listChildren.get(j));

            }
        }
        listParent.get(i).setChildren(categoryDtoList);
    }

    return Uni.createFrom().item(listParent);

}
4

1 回答 1

0

https://github.com/smallrye/smallrye-mutiny/issues/823获取我的回复:

像这样的东西会起作用:

public Uni<List<CategoryDto>> getAllCategories(Request doc) {
        Map<String, Object> parent = new HashMap<>();

        Multi<CategoryDto> all = categoryRepository.streamAllAggregators(doc).map(s -> aggregatorMapper.categoryToCategoryDto(s, doc.getLang()));
        Uni<List<CategoryDto>> listParent = categoryRepository
                .findByParentId("").map(it -> aggregatorMapper.categoryToCategoryDto(it, doc.getLang())).collect().asList();

        Uni<List<CategoryDto>> listChildren = all.select().where(n -> n.getParentId() != null).collect().asList();

       return Uni.combine().all().unis(listParent, listChildren).combinedWith((parents, children) -> {
            // Or whatever you need to do.
            List<CategoryDto> categoryDtoList;
            for (int i = 0; i < parents.size(); i++) {
                categoryDtoList = new ArrayList<>();
                for (int j = 0; j < children.size(); j++) {
                    if (children.get(j).getParentId().equals(parents.get(i).getId().toString())) {
                        categoryDtoList.add(children.get(j));

                    }
                }
                parents.get(i).setChildren(categoryDtoList);
            }
            return parents;
        });
    }
于 2022-01-26T08:53:59.263 回答