素材管理
图片删除
- 实现思路
- 检验参数, 参数为空, 返回
无效参数
- 查询图片是否存在, 不存在, 返回
数据不存在
- 查询图片是否被引用, 被引用, 返回
文件使用中, 删除失败
- 查询图片是否被收藏, 被收藏, 返回
文件收藏中, 删除失败
- 删除图片, 删除minio中的图片
- 删除图片, 删除数据库中的图片数据
- 检验参数, 参数为空, 返回
- 具体实现
@Override public ResponseResult delPicture(Integer id) { // 1. 参数检验 if(id == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); } // 2. 查询图片 WmMaterial material = getOne(Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getId, id)); if(material == null){ return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST); } // 3. 查询图片是否被引用 LambdaQueryWrapper<WmNewsMaterial> queryWrapper1 = new LambdaQueryWrapper<>(); queryWrapper1.eq(WmNewsMaterial::getMaterialId, id); Integer count = wmNewsMaterialMapper.selectCount(queryWrapper1); if(count > 0){ return ResponseResult.errorResult(501, "文件使用中, 删除失败"); } // 4. 查询图片是否被收藏 WmMaterial wmMaterial = getById(id); Short isCollection = wmMaterial.getIsCollection(); if(isCollection == 1){ return ResponseResult.errorResult(501, "文件收藏中, 删除失败"); } // 5. 删除图片 // 5.1 删除minio中的图片 String materialUrl = material.getUrl(); fileStorageService.delete(materialUrl); // 5.2 删除数据库中的图片 removeById(id); return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS); }
- 总结
调用minio依赖, 使用delete方法时报错
: 方法中根据minio当前ip地址删除图片路径开头的ip地址, 但是数据库中部分图片的ip地址与当前不同, 导致图片路径开头的ip地址没有删除, 结果报错.
收藏与取消
- 实现思路
- 检验参数, 参数为空, 返回
无效参数
- 通过
id
获取WmMaterial
对象 - 将
WmMaterial
对象的isCollection
属性设置为对于的值 - 通过
WmMaterial
对象更新数据
- 检验参数, 参数为空, 返回
- 具体实现
public ResponseResult collectFunc(Integer id, Short isCollection) { // 1. 参数检验 if(id == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); } // 2. 更新isCollection属性 WmMaterial wmMaterial = getById(id); wmMaterial.setIsCollection((short) isCollection); updateById(wmMaterial); return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS); }
- 总结
- 收藏和取消函数的方法高度重合, 抽为1个方法
文章管理
查看文章详情
- 实现思路
- 检验参数, 参数为空, 返回
无效参数
- 根据
id
获取文章对象, 对象为空, 返回文章不存在
- 返回文章对象
- 检验参数, 参数为空, 返回
- 具体实现
public ResponseResult getNewsDetail(Integer id) { // 1. 检验参数 if(id == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); } // 2. 获取文章对象 WmNews news = getById(id); if(news == null){ return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "文章不存在"); } return ResponseResult.okResult(news); }
- 总结
- 查看文章详情用于编辑文章, 回显数据
文章删除
- 实现思路
- 检验参数, 参数为空, 返回
文章id不可缺少
- 查询文章, 文章为空, 返回
文章不存在
- 查询文章状态, 已发布, 不能删除
- 查询文章状态, 草稿, 没有关系数据, 删除文章数据, 返回
- 删除文章, 删除文章素材关系表中的关系数据
- 删除文章, 删除文章表中的数据
- 检验参数, 参数为空, 返回
- 具体实现
public ResponseResult delNews(Integer id) { // 1. 检验参数 if(id == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章id不可缺少"); } // 2. 查询文章 WmNews news = getById(id); if(news == null){ return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "文章不存在"); } // 3. 查询文章状态 // 3.1 已发布, 不能删除 if(news.getStatus() == WmNews.Status.PUBLISHED.getCode()){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章已发布, 不能删除"); } // 3.2 草稿, 没有关系数据 if(news.getStatus() == WmNews.Status.NORMAL.getCode()){ removeById(id); return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS); } // 4. 删除文章 // 4.1 删除文章素材关系表中的关系数据 LambdaQueryWrapper<WmNewsMaterial> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(WmNewsMaterial::getNewsId, id); wmNewsMaterialMapper.delete(queryWrapper); // 4.2 删除文章表中的数据 removeById(id); return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS); }
- 总结
- 查询文章状态步, 若为草稿, 应提取结束, 则需添加删除文章表数据, 但这样删除文章表数据就写了两次, 可将删除文章数据提前, 但如此, 步骤的逻辑顺序又不大合适, 删除文章数据在删除文章关系数据之前, 删除文章数据在查询文章状态之中.
文章上下架
- 实现思路
- 检验参数, 参数为空, 返回
文章id不可缺少
- 检验参数, 上下架参数不合法, 默认为
上架
- 查询文章, 文章对象为空, 返回
文章不存在
- 查询文章状态, 状态不是
已发布
, 返回当前文章不是发布状态, 不能上下架
- 更新文章的上下架属性
- 检验参数, 参数为空, 返回
- 具体实现
// dto public class WmNewsDto { private Integer id; /** * 是否上架 * 0: 下架, 1: 上架 */ private Short enable; } // Service public ResponseResult downOrUp(WmNewsDto dto) { // 1. 检验参数 // 1.0 检查文章dto是否为空 if(dto == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章id不可缺少"); } // 1.1 检查文章上架参数是否合法 if(dto.getEnable() != 0 && dto.getEnable() != 1){ // 默认上架 dto.setEnable((short) 1); } // 2. 查询文章 WmNews news = getById(dto.getId()); if(news == null){ return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "文章不存在"); } // 3. 查询文章状态 if(news.getStatus() != WmNews.Status.PUBLISHED.getCode()){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "当前文章不是发布状态, 不能上下架"); } // 4. 上下架 news.setEnable(dto.getEnable()); updateById(news); return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS); }
- 总结
- 判断id是否为空, 应判断dto是否为空, 因为类中属性有初始值, dto不空, 则id会有初始值
- 下架后, 出现删除按钮, 但是下架后仍是已发布状态, 不能删除, 下架应该修改状态, 但没有给出状态码
来源
黑马程序员. 黑马头条