本文最后更新于 2026年4月17日 凌晨
编辑更新文章接口的实现
编辑更新文章接口的需求
根据文章id获取文章信息(必须要有id才能进行更新操作)
要可以更新缩略图或文章内容里的图片或者文件,即正确上传oss中的更新操作
支持更新文章基本信息
更新标签关联关系
代码实现
下面只展示服务类里的实现代码
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 76 77 78 79 80 81 82 83 84
| @Override @Transactional public ResponseResult updatePosts(Long id, UpdatePostsDto updatePostsDto) { if (id == null) { throw new RuntimeException("更新文章时ID不能为空"); }
updatePostsDto.setId(id);
Article oldArticle = articleService.getById(id); if (oldArticle == null) { throw new RuntimeException("文章不存在"); }
String content = updatePostsDto.getContent(); List<String> oldContentImages = new ArrayList<>(); List<String> newContentImages = new ArrayList<>();
if (StringUtils.hasText(oldArticle.getContent())) { oldContentImages = ImageUrlUtils.extractImageUrls(oldArticle.getContent()); }
if (ImageUrlUtils.containsTempImages(content)) { content = ossFileService.processContentImages(content); updatePostsDto.setContent(content); }
if (StringUtils.hasText(content)) { newContentImages = ImageUrlUtils.extractImageUrls(content); }
for (String oldImageUrl : oldContentImages) { if (!newContentImages.contains(oldImageUrl)) { ossFileService.deleteFileByUrl(oldImageUrl); log.info("删除旧的内容图片: {}", oldImageUrl); } }
String newThumbnail = updatePostsDto.getThumbnail(); String oldThumbnail = oldArticle.getThumbnail();
if (StringUtils.hasText(newThumbnail) && newThumbnail.contains("temp/")) { newThumbnail = ossFileService.moveTempToFormal(newThumbnail); updatePostsDto.setThumbnail(newThumbnail); }
if (StringUtils.hasText(oldThumbnail) && StringUtils.hasText(newThumbnail) && !oldThumbnail.equals(newThumbnail)) { ossFileService.deleteFileByUrl(oldThumbnail); log.info("删除旧的缩略图: {}", oldThumbnail); }
Article article = BeanCopyUtils.copyBean(updatePostsDto, Article.class); articleService.updateById(article);
LambdaQueryWrapper<ArticleTag> deleteWrapper = new LambdaQueryWrapper<>(); deleteWrapper.eq(ArticleTag::getArticleId, updatePostsDto.getId()); articleTagMapper.delete(deleteWrapper);
if (updatePostsDto.getTags() != null && !updatePostsDto.getTags().isEmpty()) { List<ArticleTag> articleTags = updatePostsDto.getTags().stream() .map(tagId -> new ArticleTag(article.getId(), tagId)) .collect(Collectors.toList()); articleTagService.saveBatch(articleTags); }
return ResponseResult.okResult(); }
|
新建 UpdatePostsDto
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
|
@Data @ApiModel(description = "更新文章请求对象") public class UpdatePostsDto { @ApiModelProperty(value = "文章ID", required = true, example = "1") private Long id; @ApiModelProperty(value = "文章标题", required = true, example = "文章标题") private String title; @ApiModelProperty(value = "文章内容(HTML格式)", required = true) private String content; @ApiModelProperty(value = "文章摘要", example = "文章摘要") private String summary; @ApiModelProperty(value = "缩略图URL", example = "https://example.com/image.jpg") private String thumbnail; @ApiModelProperty(value = "分类ID", required = true, example = "1") private Long categoryId; @ApiModelProperty(value = "是否置顶(0-否,1-是)", example = "0") private String isTop; @ApiModelProperty(value = "状态(0-已发布,1-草稿)", example = "0") private String status; @ApiModelProperty(value = "是否允许评论(0-否,1-是)", example = "1") private String isComment; @ApiModelProperty(value = "标签ID列表") private List<Long> tags; }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/