『博客开发日记-后台』之删除文章接口的实现

本文最后更新于 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
// 批量删除文章(逻辑删除),将文章中的图片有的图片从oss里的正式文件夹转移到 deleted 文件夹中
// 如果后续需要有恢复文章的功能,则可以利用逻辑删除这个特性,将文章恢复并且将图片或文件从 deleted 文件夹移动到正式文件夹中
@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) {
//找出不存在的 文章id
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);
}

// 遍历每个文章ID,将相关图片移动到deleted文件夹,并同步删除文件表记录
for (Long articleId : idList)
{
Article article = articleService.getById(articleId);

// 查找需要移动的图片URL并去重
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);

// 批量移动图片到 deleted 文件夹
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());
}

//此段注释掉,逻辑删除文章 + 上删除文章和标签关联关系会破坏逻辑删除的构想,如何后续开发回收站就可以连着关联关系一起删
// // 删除文章标签关联关系
// LambdaQueryWrapper<ArticleTag> deleteWrapper = new LambdaQueryWrapper<>();
// deleteWrapper.eq(ArticleTag::getArticleId, articleId);
// int deletedCount = articleTagMapper.delete(deleteWrapper);
// log.info("删除文章ID: {} 的标签关联记录,共 {} 条", articleId, deletedCount);
}

// 逻辑删除文章(将 del_flag 从 0 > 1)
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/


『博客开发日记-后台』之删除文章接口的实现
http://example.com/2026/04/16/『博客开发日记-后台』之删除文章接口的实现/
作者
云梦泽
发布于
2026年4月16日
更新于
2026年5月15日
许可协议