Commit a7a32246 by 段启岩

1博客详情添加标签、类别、话题的详细信息

2.修复查看权限为0时拥有者也无法查看的问题
parent 8ed9f595
......@@ -79,9 +79,9 @@ public class BlogApi {
@Anonymous
@ApiOperation("博客详情")
@GetMapping("/blog/{blogId}")
public Response<BlogDetail> getBlog(@PathVariable("blogId") String blogId) {
public Response<BlogDetail> getBlog(@PathVariable("blogId") String blogId, @CurrentSubject Subject subject) {
try {
BlogDetail blogDetail = blogService.getBlog(blogId);
BlogDetail blogDetail = blogService.getBlog(blogId, subject);
return Response.success(blogDetail);
} catch (BlogServiceException e) {
e.printStackTrace();
......
......@@ -14,7 +14,4 @@ import java.util.List;
public class BlogDetail extends Blog {
private String content;
private List<String> tagIds;
}
package cn.meteor.beyondclouds.modules.blog.entity;
import cn.meteor.beyondclouds.modules.tag.entity.Tag;
import cn.meteor.beyondclouds.modules.topic.entity.Topic;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......@@ -62,8 +64,12 @@ public class Blog implements Serializable {
@ApiModelProperty(value = "是否允许转发")
private Boolean allowForward;
@TableField(exist=false)
private List<Tag> tags;
@TableField(exist=false)
private List<Topic> topics;
private LocalDateTime createTime;
private LocalDateTime updateTime;
......
......@@ -8,7 +8,8 @@ import cn.meteor.beyondclouds.core.IErrorCode;
*/
public enum BlogErrorCode implements IErrorCode {
USERID_AUTH_ERROR(8001,"不是当前博客的拥有者"),
BLOG_NOT_FOUND(8002,"当前博客已经不存在")
BLOG_NOT_FOUND(8002,"当前博客已经不存在"),
NO_VIEW_PRIVILEGES(8003,"没有查看权限")
;
......
package cn.meteor.beyondclouds.modules.blog.service;
import cn.meteor.beyondclouds.core.bean.Subject;
import cn.meteor.beyondclouds.modules.blog.bean.BlogDetail;
import cn.meteor.beyondclouds.modules.blog.entity.Blog;
import cn.meteor.beyondclouds.modules.blog.exception.BlogCategoryServiceException;
......@@ -38,10 +39,11 @@ public interface IBlogService extends IService<Blog> {
/**
* 获取博客详情
* @param blogId
* @param subject
* @return
* @throws BlogServiceException
*/
BlogDetail getBlog(String blogId) throws BlogServiceException;
BlogDetail getBlog(String blogId, Subject subject) throws BlogServiceException;
/**
* 博客列表
......
package cn.meteor.beyondclouds.modules.blog.service.impl;
import cn.meteor.beyondclouds.core.bean.Subject;
import cn.meteor.beyondclouds.modules.blog.bean.BlogDetail;
import cn.meteor.beyondclouds.modules.blog.entity.Blog;
import cn.meteor.beyondclouds.modules.blog.entity.BlogCategory;
......@@ -11,8 +12,10 @@ import cn.meteor.beyondclouds.modules.blog.exception.BlogCategoryServiceExceptio
import cn.meteor.beyondclouds.modules.blog.exception.BlogServiceException;
import cn.meteor.beyondclouds.modules.blog.mapper.BlogMapper;
import cn.meteor.beyondclouds.modules.blog.service.*;
import cn.meteor.beyondclouds.modules.tag.service.ITagService;
import cn.meteor.beyondclouds.modules.topic.entity.TopicReference;
import cn.meteor.beyondclouds.modules.topic.service.ITopicReferenceService;
import cn.meteor.beyondclouds.modules.topic.service.ITopicService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
......@@ -53,28 +56,25 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
private BlogMapper blogMapper;
@Autowired
public void setBlogMapper(BlogMapper blogMapper) {
this.blogMapper = blogMapper;
}
private ITagService tagService;
private ITopicService topicService;
@Autowired
public void setBlogCategoryService(IBlogCategoryService blogCategoryService) {
public BlogServiceImpl(IBlogTagService blogTagService, ITopicReferenceService topicReferenceService, IBlogExtService blogExtService, IBlogCategoryService blogCategoryService, BlogMapper blogMapper, ITagService tagService, ITopicService topicService) {
this.blogTagService = blogTagService;
this.topicReferenceService = topicReferenceService;
this.blogExtService = blogExtService;
this.blogCategoryService = blogCategoryService;
this.blogMapper = blogMapper;
this.tagService = tagService;
this.topicService = topicService;
}
@Autowired
public void setBlogCommentService(IBlogCommentService blogCommentService) {
this.blogCommentService = blogCommentService;
}
@Autowired
public BlogServiceImpl(IBlogTagService blogTagService, ITopicReferenceService topicReferenceService, IBlogExtService blogExtService) {
this.blogTagService = blogTagService;
this.topicReferenceService = topicReferenceService;
this.blogExtService = blogExtService;
}
/**
* 发布博客
* @param blog
......@@ -188,38 +188,53 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
* @throws BlogServiceException
*/
@Override
public BlogDetail getBlog(String blogId) throws BlogServiceException {
public BlogDetail getBlog(String blogId, Subject subject) throws BlogServiceException {
Assert.notNull(subject, "subject must not be null");
//1.获取博客
// 1.获取博客
Blog blog = getById(blogId);
if (null == blog) {
throw new BlogServiceException(BlogErrorCode.BLOG_NOT_FOUND);
}
//判断是否存在此博客,如果存在判断是否有权限查看
if (null != blog) {
// 如果当前用户没登录或者当前登录的用户不是博客的发布者,则判断博客是否允许别人查看
if (!subject.isAuthenticated() || !subject.getId().equals(blog.getUserId())) {
if (0 == blog.getViewPrivileges()) {
throw new BlogServiceException(BlogErrorCode.BLOG_NOT_FOUND);
throw new BlogServiceException(BlogErrorCode.NO_VIEW_PRIVILEGES);
}
} else {
throw new BlogServiceException(BlogErrorCode.BLOG_NOT_FOUND);
}
//2.获取项目内容
QueryWrapper<BlogExt> blogExtQueryWrapper = new QueryWrapper();
blogExtQueryWrapper.eq("blog_id", blogId);
BlogExt blogExt = blogExtService.getOne(blogExtQueryWrapper);
//3.获取引用的话题
//3.获取博客里面的标签
QueryWrapper<BlogTag> blogTagQueryWrapper = new QueryWrapper();
blogTagQueryWrapper.eq("blog_id", blogId);
List<BlogTag> blogTagList = blogTagService.list(blogTagQueryWrapper);
String[] tagIds = blogTagList.stream().map(BlogTag::getTagId).collect(Collectors.toList()).toArray(new String[0]);
List<String> tagIds = blogTagList.stream().map(BlogTag::getTagId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(tagIds)) {
blog.setTags(tagService.listByIds(tagIds));
} else {
blog.setTags(List.of());
}
ArrayList<String> blogTags = new ArrayList<>();
for (String tagId : tagIds) {
blogTags.add(tagId);
//4.获取博客引用的话题
QueryWrapper<TopicReference> topicReferenceQueryWrapper = new QueryWrapper();
topicReferenceQueryWrapper.eq("referencer_id", blogId);
topicReferenceQueryWrapper.eq("referencer_type", 0);
List<TopicReference> topicReferenceList = topicReferenceService.list(topicReferenceQueryWrapper);
List<String> topicIds = topicReferenceList.stream().map(TopicReference::getTopicId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(topicIds)) {
blog.setTopics(topicService.listByIds(topicIds));
} else {
blog.setTags(List.of());
}
//装配并返回查询到的数据
BlogDetail blogDetail = new BlogDetail();
BeanUtils.copyProperties(blog, blogDetail);
blogDetail.setTagIds(blogTags);
blogDetail.setContent(blogExt.getContent());
return blogDetail;
}
......
......@@ -2,6 +2,7 @@ package cn.meteor.beyondclouds.modules.tag.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......@@ -31,6 +32,7 @@ public class Tag implements Serializable {
@TableId(value = "tag_id", type = IdType.ASSIGN_UUID)
private String tagId;
@JsonIgnore
@ApiModelProperty(value = "用户id")
private String userId;
......
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