Refactor category tree concrete

pull/146/head
johnniang 2019-05-01 21:53:42 +08:00
parent 4c8d38ae07
commit 1b2f5270e2
1 changed files with 13 additions and 15 deletions

View File

@ -1,5 +1,6 @@
package run.halo.app.service.impl;
import com.google.common.base.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.lang.NonNull;
@ -101,23 +102,20 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
return;
}
// Create children container for removing after
List<Category> children = new LinkedList<>();
categories.forEach(category -> {
if (parentCategory.getId().equals(category.getParentId())) {
// Save child category
children.add(category);
// Get children for removing after
List<Category> children = categories.stream()
.filter(category -> Objects.equal(parentCategory.getId(), category.getParentId()))
.collect(Collectors.toList());
children.forEach(category -> {
// Convert to child category vo
CategoryVO child = new CategoryVO().convertFrom(category);
// Init children if absent
if (parentCategory.getChildren() == null) {
parentCategory.setChildren(new LinkedList<>());
}
// Add child
parentCategory.getChildren().add(child);
}
});
// Remove all child categories