mirror of https://github.com/halo-dev/halo
Fix the problem of updating categories and menus in batch (#1526)
* fix: batch updatepull/1534/head
parent
c790df3c75
commit
62c84e9215
|
@ -5,6 +5,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
|
|||
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
@ -103,6 +104,7 @@ public class MenuController {
|
|||
public List<MenuDTO> updateBatchBy(@RequestBody @Valid List<MenuParam> menuParams) {
|
||||
List<Menu> menus = menuParams
|
||||
.stream()
|
||||
.filter(menuParam -> Objects.nonNull(menuParam.getId()))
|
||||
.map(InputConverter::convertTo)
|
||||
.collect(Collectors.toList());
|
||||
return menuService.updateInBatch(menus).stream()
|
||||
|
|
|
@ -10,6 +10,7 @@ import java.util.LinkedList;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -661,15 +662,21 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
|
|||
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public List<Category> updateInBatch(Collection<Category> categories) {
|
||||
if (CollectionUtils.isEmpty(categories)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
ArrayList<Category> resultList = new ArrayList<>();
|
||||
for (Category category : categories) {
|
||||
resultList.add(update(category));
|
||||
}
|
||||
return resultList;
|
||||
Set<Integer> categoryIds = ServiceUtils.fetchProperty(categories, Category::getId);
|
||||
Map<Integer, Category> idCategoryParamMap =
|
||||
ServiceUtils.convertToMap(categories, Category::getId);
|
||||
return categoryRepository.findAllById(categoryIds)
|
||||
.stream()
|
||||
.map(categoryToUpdate -> {
|
||||
Category categoryParam = idCategoryParamMap.get(categoryToUpdate.getId());
|
||||
BeanUtils.updateProperties(categoryParam, categoryToUpdate);
|
||||
return update(categoryToUpdate);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package run.halo.app.service.impl;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
@ -9,6 +10,7 @@ import java.util.stream.Collectors;
|
|||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import run.halo.app.model.dto.MenuDTO;
|
||||
|
@ -19,6 +21,7 @@ import run.halo.app.model.vo.MenuVO;
|
|||
import run.halo.app.repository.MenuRepository;
|
||||
import run.halo.app.service.MenuService;
|
||||
import run.halo.app.service.base.AbstractCrudService;
|
||||
import run.halo.app.utils.BeanUtils;
|
||||
import run.halo.app.utils.ServiceUtils;
|
||||
|
||||
/**
|
||||
|
@ -153,6 +156,22 @@ public class MenuServiceImpl extends AbstractCrudService<Menu, Integer> implemen
|
|||
return super.update(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public List<Menu> updateInBatch(@NonNull Collection<Menu> menus) {
|
||||
Set<Integer> menuIds = ServiceUtils.fetchProperty(menus, Menu::getId);
|
||||
Map<Integer, Menu> idMenuParamMap = ServiceUtils.convertToMap(menus, Menu::getId);
|
||||
return menuRepository.findAllById(menuIds)
|
||||
.stream()
|
||||
.map(menuToUpdate -> {
|
||||
Menu menuParam = idMenuParamMap.get(menuToUpdate.getId());
|
||||
BeanUtils.updateProperties(menuParam, menuToUpdate);
|
||||
return update(menuToUpdate);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Concrete menu tree.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue