Commit a83fa510 by 段启岩

修改博客功能修复

parent dac935fc
......@@ -142,7 +142,7 @@ public class BlogApi {
blog.setBlogId(blogId);
//更新项目
try {
blogService.updateBlog(blog, updateBlogForm.getContent(),updateBlogForm.getTopicIds(),updateBlogForm.getTagIds());
blogService.updateBlog(blog, updateBlogForm.getContent(), updateBlogForm.getTopicIds(),updateBlogForm.getTagIds());
return Response.success();
} catch (BlogServiceException | BlogCategoryServiceException e) {
e.printStackTrace();
......
......@@ -15,17 +15,20 @@ import java.util.List;
@Data
public class PublishBlogForm {
@NotNull(message = "标题不能为空")
@NotEmpty(message = "标题不能为空")
private String blogTitle;
private String originLink;
@NotNull(message = "摘要不能为空")
@NotEmpty(message = "摘要不能为空")
private String blogAbstract;
@NotNull(message = "博客类型不能为空")
private Integer categoryId;
@NotNull(message = "内容不能为空")
@NotEmpty(message = "内容不能为空")
private String blogContent;
......@@ -42,6 +45,7 @@ public class PublishBlogForm {
@NotNull(message = "转载权限不能为空")
private Boolean allowForward;
@NotNull(message = "封面图不能为空")
@NotEmpty(message = "封面图不能为空")
private String cover;
}
......@@ -3,7 +3,6 @@ package cn.meteor.beyondclouds.modules.blog.form;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
......@@ -14,15 +13,18 @@ import java.util.List;
@Data
public class UpdateBlogForm {
@NotEmpty(message = "标题不能为空")
private String blogTitle;
private String originLink;
@NotEmpty(message = "摘要不能为空")
private String blogAbstract;
private Integer categoryId;
private String Content;
@NotEmpty(message = "内容不能为空")
private String content;
private List<String> topicIds;
......@@ -34,6 +36,7 @@ public class UpdateBlogForm {
private Boolean allowForward;
@NotEmpty(message = "封面图不能为空")
private String cover;
}
......@@ -326,18 +326,20 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
.eq("user_id", blog.getUserId());
Blog blogInDb = getOne(blogQueryWrapper);
if (null == blogInDb) {
throw new BlogServiceException(BlogErrorCode.BLOG_NOT_FOUND);
throw new BlogServiceException(BlogErrorCode.USER_BLOG_NOT_FOUND);
}
//判断博客类型是否正确
BlogCategory blogCategory = null;
if (blog.getCategoryId() != null) {
BlogCategory blogCategory = blogCategoryService.getById(blog.getCategoryId());
blogCategory = blogCategoryService.getById(blog.getCategoryId());
if (null == blogCategory) {
throw new BlogCategoryServiceException(BlogCategoryErrorCode.INCORRECT_CATEGORY);
}
}
//2.更新博客基本信息
blog.setCategory(blogCategory.getCategory());
updateById(blog);
//3.更新博客内容
......@@ -350,36 +352,55 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
blogExtService.update(blogExt,blogExtQueryWrapper);
}
//4.更新标签
if (!StringUtils.isEmpty(tagIds)) {
QueryWrapper tagIdQueryWrapperTag = new QueryWrapper();
tagIdQueryWrapperTag.eq("blog_id", blog.getBlogId());
blogTagService.remove(tagIdQueryWrapperTag);
for (String tagId : tagIds) {
BlogTag blogTag = new BlogTag();
blogTag.setBlogId(blog.getBlogId());
blogTag.setTagId(tagId);
blogTagService.save(blogTag);
}
}
//4.判断是否引用话题
// 删除旧话题引用
QueryWrapper<TopicReference> topicReferenceQueryWrapper = new QueryWrapper<>();
topicReferenceQueryWrapper.eq("referencer_id", blog.getBlogId());
topicReferenceService.remove(topicReferenceQueryWrapper);
//5.更新话题
if (!StringUtils.isEmpty(topicIds)) {
QueryWrapper topicIdQueryWrapper = new QueryWrapper();
topicIdQueryWrapper.eq("referencer_id", blog.getBlogId());
topicReferenceService.remove(topicIdQueryWrapper);
for (String topicId : topicIds) {
// 添加新话题引用
if (!CollectionUtils.isEmpty(topicIds)) {
// 获取要引用的话题,不正确的话题ID会被自动过滤
List<Topic> topicList = topicService.listByIds(topicIds);
List<String> existsTopicIds = topicList.stream().map(Topic::getTopicId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(existsTopicIds)) {
List<TopicReference> topicReferenceList = new ArrayList<>();
for (String topicId : existsTopicIds) {
TopicReference topicReference = new TopicReference();
topicReference.setTopicId(topicId);
topicReference.setReferencerId(blog.getBlogId());
topicReference.setReferencerType(0);
topicReferenceService.save(topicReference);
topicReferenceList.add(topicReference);
}
// 批量保存
topicReferenceService.saveBatch(topicReferenceList);
// 更新话题引用次数
existsTopicIds.forEach(topicId -> {
topicService.increaseReferenceCount(topicId, 1);
});
}
}
//5.判断是否引用标签
// 删除旧标签
QueryWrapper<BlogTag> blogTagQueryWrapper = new QueryWrapper<>();
blogTagQueryWrapper.eq("blog_id", blog.getBlogId());
blogTagService.remove(blogTagQueryWrapper);
// 添加新标签
if (!CollectionUtils.isEmpty(tagIds)) {
List<String> existsTagIds = tagService.listByIds(tagIds).stream().map(Tag::getTagId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(existsTagIds)) {
List<BlogTag> blogTagList = new ArrayList<>();
for (String tagId : existsTagIds) {
BlogTag blogTag = new BlogTag();
blogTag.setBlogId(blog.getBlogId());
blogTag.setTagId(tagId);
blogTagList.add(blogTag);
}
blogTagService.saveBatch(blogTagList);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment