JavaWeb_LeadNews_Day3作业-素材管理, 文章管理

素材管理

图片删除

  • 实现思路
    1. 检验参数, 参数为空, 返回无效参数
    2. 查询图片是否存在, 不存在, 返回数据不存在
    3. 查询图片是否被引用, 被引用, 返回文件使用中, 删除失败
    4. 查询图片是否被收藏, 被收藏, 返回文件收藏中, 删除失败
    5. 删除图片, 删除minio中的图片
    6. 删除图片, 删除数据库中的图片数据
  • 具体实现
    @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地址没有删除, 结果报错.

收藏与取消

  • 实现思路
    1. 检验参数, 参数为空, 返回无效参数
    2. 通过id获取WmMaterial对象
    3. WmMaterial对象的isCollection属性设置为对于的值
    4. 通过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个方法

文章管理

查看文章详情

  • 实现思路
    1. 检验参数, 参数为空, 返回无效参数
    2. 根据id获取文章对象, 对象为空, 返回文章不存在
    3. 返回文章对象
  • 具体实现
    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);
    }
    
  • 总结
    • 查看文章详情用于编辑文章, 回显数据

文章删除

  • 实现思路
    1. 检验参数, 参数为空, 返回文章id不可缺少
    2. 查询文章, 文章为空, 返回文章不存在
    3. 查询文章状态, 已发布, 不能删除
    4. 查询文章状态, 草稿, 没有关系数据, 删除文章数据, 返回
    5. 删除文章, 删除文章素材关系表中的关系数据
    6. 删除文章, 删除文章表中的数据
  • 具体实现
    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);
    }
    
  • 总结
    • 查询文章状态步, 若为草稿, 应提取结束, 则需添加删除文章表数据, 但这样删除文章表数据就写了两次, 可将删除文章数据提前, 但如此, 步骤的逻辑顺序又不大合适, 删除文章数据在删除文章关系数据之前, 删除文章数据在查询文章状态之中.

文章上下架

  • 实现思路
    1. 检验参数, 参数为空, 返回文章id不可缺少
    2. 检验参数, 上下架参数不合法, 默认为上架
    3. 查询文章, 文章对象为空, 返回文章不存在
    4. 查询文章状态, 状态不是已发布, 返回当前文章不是发布状态, 不能上下架
    5. 更新文章的上下架属性
  • 具体实现
    // 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会有初始值
    • 下架后, 出现删除按钮, 但是下架后仍是已发布状态, 不能删除, 下架应该修改状态, 但没有给出状态码

来源

黑马程序员. 黑马头条

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y_cen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值