Add an API to update categories in batch (#1657)

* feat: Add a api of update categories in batch

* fix: filter condition
pull/1666/head
guqing 2022-02-18 15:50:02 +08:00 committed by GitHub
parent 15bde8eef7
commit acf6a98d25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -1,10 +1,11 @@
package run.halo.app.controller.admin.api;
import static org.springframework.data.domain.Sort.Direction.ASC;
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;
import org.springframework.data.web.SortDefault;
@ -89,6 +90,20 @@ public class CategoryController {
return categoryService.convertTo(categoryService.update(categoryToUpdate));
}
@PutMapping("/batch")
@ApiOperation("Updates category in batch")
public List<CategoryDTO> updateBatchBy(@RequestBody List<@Valid CategoryParam> categoryParams) {
List<Category> categoriesToUpdate = categoryParams.stream()
.filter(categoryParam -> Objects.nonNull(categoryParam.getId()))
.map(categoryParam -> {
Category categoryToUpdate = categoryService.getById(categoryParam.getId());
categoryParam.update(categoryToUpdate);
return categoryToUpdate;
})
.collect(Collectors.toList());
return categoryService.convertTo(categoryService.updateInBatch(categoriesToUpdate));
}
@DeleteMapping("{categoryId:\\d+}")
@ApiOperation("Deletes category")
public void deletePermanently(@PathVariable("categoryId") Integer categoryId) {

View File

@ -14,11 +14,14 @@ import run.halo.app.utils.SlugUtils;
*
* @author johnniang
* @author ryanwang
* @author guqing
* @date 2019-03-21
*/
@Data
public class CategoryParam implements InputConverter<Category> {
private Integer id;
@NotBlank(message = "分类名称不能为空")
@Size(max = 255, message = "分类名称的字符长度不能超过 {max}")
private String name;