diff --git a/src/main/java/run/halo/app/controller/admin/api/CategoryController.java b/src/main/java/run/halo/app/controller/admin/api/CategoryController.java index 523a9703e5..b4a6864dd9 100644 --- a/src/main/java/run/halo/app/controller/admin/api/CategoryController.java +++ b/src/main/java/run/halo/app/controller/admin/api/CategoryController.java @@ -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 CategoryDTO updateBy(@PathVariable("categoryId") Integer categoryId, return categoryService.convertTo(categoryService.update(categoryToUpdate)); } + @PutMapping("/batch") + @ApiOperation("Updates category in batch") + public List updateBatchBy(@RequestBody List<@Valid CategoryParam> categoryParams) { + List 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) { diff --git a/src/main/java/run/halo/app/model/params/CategoryParam.java b/src/main/java/run/halo/app/model/params/CategoryParam.java index 98bfb12b92..1799c8eb01 100644 --- a/src/main/java/run/halo/app/model/params/CategoryParam.java +++ b/src/main/java/run/halo/app/model/params/CategoryParam.java @@ -14,11 +14,14 @@ * * @author johnniang * @author ryanwang + * @author guqing * @date 2019-03-21 */ @Data public class CategoryParam implements InputConverter { + private Integer id; + @NotBlank(message = "分类名称不能为空") @Size(max = 255, message = "分类名称的字符长度不能超过 {max}") private String name;