本文最后更新于 2026年4月18日 晚上
修改分类接口的实现
修改分类接口的需求
根据分类id获得旧的分类信息
检查是否将分类状态从正常改为禁用
更新分类信息
代码实现
创建 UpdateCategoryDto 请求对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
@Data @AllArgsConstructor @NoArgsConstructor @ApiModel(description = "更新分类请求对象") public class UpdateCategoryDto { @ApiModelProperty(value = "分类ID", required = true, example = "1") private Long id; @ApiModelProperty(value = "分类名称", required = true, example = "技术分享") private String name; @ApiModelProperty(value = "分类描述", example = "技术相关的文章") private String description;
@ApiModelProperty(value = "排序", example = "1") private Integer sort;
@ApiModelProperty(value = "状态(0-正常,1-禁用)", example = "0") private String status; }
|
在 AdminCategoryServiceImpl 中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
@Override public ResponseResult updateCategory(Long id, UpdateCategoryDto updateCategoryDto) { updateCategoryDto.setId(id);
Category oldCategory = categoryService.getById(id);
String msg = null;
if (SystemConstants.STATUS_NORMAL.equals(oldCategory.getStatus()) && !"0".equals(updateCategoryDto.getStatus())) { LambdaQueryWrapper<Article> articleQueryWrapper = new LambdaQueryWrapper<>(); articleQueryWrapper.eq(Article::getCategoryId, id) .eq(Article::getDelFlag, SystemConstants.NOT_DELETED); long articleCount = articleService.count(articleQueryWrapper);
msg = String.format("分类【%s】已被禁用,该分类下有 %d 篇文章将不会在 前台和文章列表 中显示", oldCategory.getName(), articleCount); log.info("警告:{}", msg); }
Category category = BeanCopyUtils.copyBean(updateCategoryDto, Category.class); categoryService.updateById(category);
if (msg != null) { return ResponseResult.okResult(msg); }
return ResponseResult.okResult(); }
|
最后测试接口成功
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/