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