Skip to content

Commit

Permalink
Add an API to update categories in batch (halo-dev#1657)
Browse files Browse the repository at this point in the history
* feat: Add a api of update categories in batch

* fix: filter condition
  • Loading branch information
guqing authored and winar-jin committed Mar 24, 2022
1 parent 3093f88 commit 5fbfd52
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<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) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/run/halo/app/model/params/CategoryParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
*
* @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;
Expand Down

0 comments on commit 5fbfd52

Please sign in to comment.