本文最后更新于 2026年5月15日 晚上
编辑更新文章接口的实现
编辑更新文章接口的需求
根据文章id删除文章
要是逻辑删除( del_flag 从 0 > 1)
支持批量删除
找出文章中上传的图片然后在oss中进行对应的操作 正式文件 > 已删除文件
代码实现
其他操作都差不多
主要在 AdminPostsServiceImpl 中实现功能就好
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
@Override @Transactional public ResponseResult deletePosts(Long[] ids) { if (ids == null || ids.length == 0) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "请选择要删除的文章"); }
List<Long> idList = List.of(ids);
List<Article> articlesToDelete = articleService.listByIds(idList); if (articlesToDelete.size() != ids.length) { Set<Long> existIds = articlesToDelete.stream() .map(Article::getId) .collect(Collectors.toSet());
List<Long> notExistIds = idList.stream() .filter(id -> !existIds.contains(id)) .toList();
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "删除失败!!原因:以下文章 id 不存在:" + notExistIds); }
for (Long articleId : idList) { Article article = articleService.getById(articleId);
Set<String> imageUrlSet = new LinkedHashSet<>(); if (StringUtils.hasText(article.getContent())) { List<String> contentImages = ImageUrlUtils.extractImageUrls(article.getContent()); imageUrlSet.addAll(contentImages); log.info("文章ID: {} 内容中提取到的图片URL: {}", articleId, contentImages); } if (StringUtils.hasText(article.getThumbnail())) { imageUrlSet.add(article.getThumbnail()); log.info("文章ID: {} 缩略图URL: {}", articleId, article.getThumbnail()); }
List<String> imageUrls = new ArrayList<>(imageUrlSet); log.info("文章ID: {} 去重后的图片URL: {}", articleId, imageUrls);
if (!imageUrls.isEmpty()) { int movedCount = ossFileService.batchMoveFilesToDeleted(imageUrls); log.info("文章ID: {} 的图片已移动到deleted文件夹,成功: {}/{}", articleId, movedCount, imageUrls.size());
for (String imageUrl : imageUrls) { deleteFileRecordByUrl(imageUrl); } log.info("文章ID: {} 的图片文件表记录已删除,共 {} 条", articleId, imageUrls.size()); }
}
articleService.removeByIds(idList); log.info("批量逻辑删除文章成功,ID列表: {}", idList);
return ResponseResult.okResult(); }
|
再对异常处理类进行更新一下
增加关于文件上传大小超出限制的异常处理
GlobalExceptionHandler 类
1 2 3 4 5 6 7 8
| @ExceptionHandler(MaxUploadSizeExceededException.class) public ResponseResult maxUploadSizeExceededExceptionHandler(MaxUploadSizeExceededException exception) { log.error("文件上传大小超限!异常详情:{}", exception.getMessage()); return ResponseResult.errorResult(AppHttpCodeEnum.FILE_SIZE_ERROR); }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/