Commit ce68c2ff by 段启岩

Merge remote-tracking branch 'origin/gaotong02-03'

# Conflicts:
#	src/main/resources/application.yml
parents 17bf3245 f4cd44fb
...@@ -46,6 +46,11 @@ public class BlogApi { ...@@ -46,6 +46,11 @@ public class BlogApi {
if (result.hasErrors()) { if (result.hasErrors()) {
return Response.fieldError(result.getFieldError()); return Response.fieldError(result.getFieldError());
} }
//如果为不可查看则默认为不可评论和转载
if (0 == publishBlogForm.getViewPrivileges()) {
publishBlogForm.setAllowComment(false);
publishBlogForm.setAllowForward(false);
}
Blog blog = new Blog(); Blog blog = new Blog();
BeanUtils.copyProperties(publishBlogForm, blog); BeanUtils.copyProperties(publishBlogForm, blog);
...@@ -106,7 +111,7 @@ public class BlogApi { ...@@ -106,7 +111,7 @@ public class BlogApi {
@ApiOperation("我的博客列表") @ApiOperation("我的博客列表")
@GetMapping("/my/blogs") @GetMapping("/my/blogs")
public Response<PageVO<Blog>> getMyBlogs (@Valid PageForm pageForm , @CurrentSubject Subject subject) { public Response<PageVO<Blog>> getMyBlogs (@Valid PageForm pageForm , @CurrentSubject Subject subject) {
IPage<Blog> blogPage = blogService.getUserBlogPage(pageForm.getPage(), pageForm.getSize(), (String) subject.getId()); IPage<Blog> blogPage = blogService.getMyBlogPage(pageForm.getPage(), pageForm.getSize(), (String) subject.getId());
PageVO<Blog> blogPageVO = new PageVO<>(blogPage); PageVO<Blog> blogPageVO = new PageVO<>(blogPage);
return Response.success(blogPageVO); return Response.success(blogPageVO);
} }
......
...@@ -38,7 +38,7 @@ public class BlogCommentApi { ...@@ -38,7 +38,7 @@ public class BlogCommentApi {
public Response createComment (@PathVariable("blogId") String blogId, @RequestBody @Valid BlogCommentForm blogCommentForm, @CurrentSubject Subject subject) { public Response createComment (@PathVariable("blogId") String blogId, @RequestBody @Valid BlogCommentForm blogCommentForm, @CurrentSubject Subject subject) {
try { try {
blogCommentService.commentCreate(blogId, blogCommentForm.getParentId(), blogCommentService.createComment(blogId, blogCommentForm.getParentId(),
blogCommentForm.getComment(), (String) subject.getId()); blogCommentForm.getComment(), (String) subject.getId());
return Response.success(); return Response.success();
} catch (BlogServiceException | BlogCommentServiceException e) { } catch (BlogServiceException | BlogCommentServiceException e) {
......
...@@ -10,6 +10,7 @@ public enum BlogCommentErrorCode implements IErrorCode { ...@@ -10,6 +10,7 @@ public enum BlogCommentErrorCode implements IErrorCode {
COMMENT_NOT_FOUND(8003,"该评论不存在"), COMMENT_NOT_FOUND(8003,"该评论不存在"),
NO_DELETE_PRIVILEGES(8004,"没有权限删除评论"), NO_DELETE_PRIVILEGES(8004,"没有权限删除评论"),
PARENT_COMMENT_NOT_FOUND(8006,"父评论不存在"), PARENT_COMMENT_NOT_FOUND(8006,"父评论不存在"),
NO_COMMENT_PRIVILEGES(8007,"没有评论权限")
; ;
private long code; private long code;
......
...@@ -24,7 +24,7 @@ public interface IBlogCommentService extends IService<BlogComment> { ...@@ -24,7 +24,7 @@ public interface IBlogCommentService extends IService<BlogComment> {
* @param comment * @param comment
* @param userId * @param userId
*/ */
void commentCreate(String blogId,Integer parentId,String comment,String userId) throws BlogServiceException, BlogCommentServiceException; void createComment(String blogId,Integer parentId,String comment,String userId) throws BlogServiceException, BlogCommentServiceException;
/** /**
* 删除评论 * 删除评论
......
...@@ -63,6 +63,14 @@ public interface IBlogService extends IService<Blog> { ...@@ -63,6 +63,14 @@ public interface IBlogService extends IService<Blog> {
*/ */
IPage<Blog> getUserBlogPage(Integer pageNumber , Integer pageSize , String userId); IPage<Blog> getUserBlogPage(Integer pageNumber , Integer pageSize , String userId);
/**
* 我的博客列表
* @param pageNumber
* @param pageSize
* @param userId
* @return
*/
IPage<Blog> getMyBlogPage(Integer pageNumber , Integer pageSize , String userId);
/** /**
* 更新博客 * 更新博客
......
...@@ -45,12 +45,16 @@ public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogC ...@@ -45,12 +45,16 @@ public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogC
* @throws BlogCommentServiceException * @throws BlogCommentServiceException
*/ */
@Override @Override
public void commentCreate(String blogId, Integer parentId, String comment, String userId) throws BlogServiceException, BlogCommentServiceException { public void createComment(String blogId, Integer parentId, String comment, String userId) throws BlogServiceException, BlogCommentServiceException {
//判断博客是否存在 //判断博客是否存在,并且有评论权限
Blog blog = blogService.getById(blogId); Blog blog = blogService.getById(blogId);
if (null == blog) { if (null != blog) {
if (!blog.getAllowComment()) {
throw new BlogCommentServiceException(BlogCommentErrorCode.NO_COMMENT_PRIVILEGES);
}
} else {
throw new BlogServiceException(BlogErrorCode.BLOG_NOT_FOUND); throw new BlogServiceException(BlogErrorCode.BLOG_NOT_FOUND);
} }
......
...@@ -172,11 +172,14 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB ...@@ -172,11 +172,14 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
//1.获取博客 //1.获取博客
Blog blog = getById(blogId); Blog blog = getById(blogId);
//若找不到该博客,则抛出业务异常 //判断是否存在此博客,如果存在判断是否有权限查看
if (null == blog) { if (null != blog) {
if (0 == blog.getViewPrivileges()) {
throw new BlogServiceException(BlogErrorCode.BLOG_NOT_FOUND);
}
} else {
throw new BlogServiceException(BlogErrorCode.BLOG_NOT_FOUND); throw new BlogServiceException(BlogErrorCode.BLOG_NOT_FOUND);
} }
//2.获取项目内容 //2.获取项目内容
QueryWrapper<BlogExt> blogExtQueryWrapper = new QueryWrapper(); QueryWrapper<BlogExt> blogExtQueryWrapper = new QueryWrapper();
blogExtQueryWrapper.eq("blog_id", blogId); blogExtQueryWrapper.eq("blog_id", blogId);
...@@ -209,7 +212,10 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB ...@@ -209,7 +212,10 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
@Override @Override
public IPage<Blog> getBlogPage(Integer pageNumber, Integer pageSize) { public IPage<Blog> getBlogPage(Integer pageNumber, Integer pageSize) {
IPage<Blog> page = new Page<>(pageNumber, pageSize); IPage<Blog> page = new Page<>(pageNumber, pageSize);
return page(page); QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("view_privileges", 1);
queryWrapper.orderByDesc("create_time");
return page(page,queryWrapper);
} }
/** /**
...@@ -221,8 +227,26 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB ...@@ -221,8 +227,26 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IB
*/ */
@Override @Override
public IPage<Blog> getUserBlogPage(Integer pageNumber, Integer pageSize, String userId) { public IPage<Blog> getUserBlogPage(Integer pageNumber, Integer pageSize, String userId) {
QueryWrapper blogQueryWrapper = new QueryWrapper();
blogQueryWrapper.eq("user_id", userId);
blogQueryWrapper.eq("view_privileges", 1);
blogQueryWrapper.orderByDesc("create_time");
IPage<Blog> page = new Page<>(pageNumber, pageSize);
return page(page, blogQueryWrapper);
}
/**
* 我的博客列表
* @param pageNumber
* @param pageSize
* @param userId
* @return
*/
@Override
public IPage<Blog> getMyBlogPage(Integer pageNumber, Integer pageSize, String userId) {
QueryWrapper myBlogQueryWrapper = new QueryWrapper(); QueryWrapper myBlogQueryWrapper = new QueryWrapper();
myBlogQueryWrapper.eq("user_id", userId); myBlogQueryWrapper.eq("user_id", userId);
myBlogQueryWrapper.orderByDesc("create_time");
IPage<Blog> page = new Page<>(pageNumber, pageSize); IPage<Blog> page = new Page<>(pageNumber, pageSize);
return page(page, myBlogQueryWrapper); return page(page, myBlogQueryWrapper);
} }
......
...@@ -53,6 +53,10 @@ public class PostApi { ...@@ -53,6 +53,10 @@ public class PostApi {
@PostMapping("/post") @PostMapping("/post")
public Response publishPost(@RequestBody @Valid PostForm postForm , BindingResult result , public Response publishPost(@RequestBody @Valid PostForm postForm , BindingResult result ,
@CurrentSubject Subject subject) { @CurrentSubject Subject subject) {
if (result.hasErrors()) {
return Response.fieldError(result.getFieldError());
}
//对图片进行类型转换 //对图片进行类型转换
Post post = new Post(); Post post = new Post();
BeanUtils.copyProperties(postForm, post); BeanUtils.copyProperties(postForm, post);
......
package cn.meteor.beyondclouds.modules.post.api; package cn.meteor.beyondclouds.modules.post.api;
import cn.meteor.beyondclouds.common.form.PageForm;
import cn.meteor.beyondclouds.common.vo.PageVO;
import cn.meteor.beyondclouds.core.annotation.CurrentSubject;
import cn.meteor.beyondclouds.core.api.Response;
import cn.meteor.beyondclouds.core.bean.Subject;
import cn.meteor.beyondclouds.core.exception.ServiceException;
import cn.meteor.beyondclouds.modules.post.entity.PostComment;
import cn.meteor.beyondclouds.modules.post.exception.PostCommentServiceException;
import cn.meteor.beyondclouds.modules.post.exception.PostServiceException;
import cn.meteor.beyondclouds.modules.post.form.PostCommentForm;
import cn.meteor.beyondclouds.modules.post.service.IPostCommentService;
import cn.meteor.beyondclouds.modules.tag.entity.Tag;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.nio.channels.Pipe;
/** /**
* @author gaoTong * @author gaoTong
* @date 2020/2/2 9:42 * @date 2020/2/2 9:42
*/ */
@Api(tags = "动态评论API")
@RestController
@RequestMapping("/api")
public class PostCommentApi { public class PostCommentApi {
private IPostCommentService postCommentService;
@Autowired
public void setPostCommentService(IPostCommentService postCommentService) {
this.postCommentService = postCommentService;
}
@ApiOperation("发布评论")
@PostMapping("/post/{postId}/comment")
public Response publishComment(@PathVariable("postId") String postId , @RequestBody @Valid PostCommentForm postCommentForm ,
BindingResult result , @CurrentSubject Subject subject) {
if (result.hasErrors()) {
return Response.fieldError(result.getFieldError());
}
try {
postCommentService.createComment(postId, postCommentForm.getParentId(),
postCommentForm.getComment(),
(String) subject.getId());
return Response.success();
} catch (ServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
@ApiOperation("删除评论")
@DeleteMapping("/post/comment/{commentId}")
public Response deleteComment(@PathVariable("commentId") Integer commentId , @CurrentSubject Subject subject) {
try {
postCommentService.deleteComment(commentId, (String) subject.getId());
return Response.success();
} catch (PostCommentServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
@ApiOperation("评论列表")
@GetMapping("/post/{postId}/comments")
public Response<PageVO<PostComment>> getPostCommentPage(@PathVariable("postId") String postId ,
@Valid PageForm pageForm ,
@RequestParam(value = "parent_id",required = false) Integer parentId) {
try {
IPage<PostComment> postCommentIPage = postCommentService.getCommentPage(pageForm.getPage(), pageForm.getSize(), postId, parentId);
PageVO<PostComment> postCommentPageVO = new PageVO<>(postCommentIPage);
return Response.success(postCommentPageVO);
} catch (PostCommentServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
} }
...@@ -7,6 +7,10 @@ import cn.meteor.beyondclouds.core.IErrorCode; ...@@ -7,6 +7,10 @@ import cn.meteor.beyondclouds.core.IErrorCode;
* @date 2020/2/2 14:43 * @date 2020/2/2 14:43
*/ */
public enum PostCommentErrorCode implements IErrorCode { public enum PostCommentErrorCode implements IErrorCode {
COMMENT_NOT_FOUND(10003,"该评论不存在"),
NO_DELETE_PRIVILEGES(10004,"没有权限删除评论"),
PARENT_COMMENT_NOT_FOUND(10006,"父评论不存在"),
SUN_COMMENT_NOT_FOUND(1007,"子级评论不存在")
; ;
private long code; private long code;
......
package cn.meteor.beyondclouds.modules.post.form;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
/**
* @author gaoTong
* @date 2020/2/2 21:28
*/
@Data
public class PostCommentForm {
private Integer parentId;
@NotEmpty(message = "评论内容不能为空")
private String comment;
}
package cn.meteor.beyondclouds.modules.post.service; package cn.meteor.beyondclouds.modules.post.service;
import cn.meteor.beyondclouds.modules.blog.entity.BlogComment;
import cn.meteor.beyondclouds.modules.post.entity.PostComment; import cn.meteor.beyondclouds.modules.post.entity.PostComment;
import cn.meteor.beyondclouds.modules.post.exception.PostCommentServiceException;
import cn.meteor.beyondclouds.modules.post.exception.PostServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
/** /**
...@@ -13,4 +17,33 @@ import com.baomidou.mybatisplus.extension.service.IService; ...@@ -13,4 +17,33 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/ */
public interface IPostCommentService extends IService<PostComment> { public interface IPostCommentService extends IService<PostComment> {
/**
* 创建评论
* @param postId
* @param parentId
* @param comment
* @param userId
*/
void createComment(String postId,Integer parentId,String comment,String userId) throws PostServiceException, PostCommentServiceException;
/**
* 删除评论
* @param commentId
* @param userId
*/
void deleteComment(Integer commentId, String userId) throws PostCommentServiceException;
/**
* 评论列表
* @param pageNumber
* @param size
* @param postId
* @param parentId
* @return
*/
IPage<PostComment> getCommentPage(Integer pageNumber, Integer size, String postId, Integer parentId) throws PostCommentServiceException;
} }
package cn.meteor.beyondclouds.modules.post.service.impl; package cn.meteor.beyondclouds.modules.post.service.impl;
import cn.meteor.beyondclouds.modules.post.entity.Post;
import cn.meteor.beyondclouds.modules.post.entity.PostComment; import cn.meteor.beyondclouds.modules.post.entity.PostComment;
import cn.meteor.beyondclouds.modules.post.enums.PostCommentErrorCode;
import cn.meteor.beyondclouds.modules.post.enums.PostErrorCode;
import cn.meteor.beyondclouds.modules.post.exception.PostCommentServiceException;
import cn.meteor.beyondclouds.modules.post.exception.PostServiceException;
import cn.meteor.beyondclouds.modules.post.mapper.PostCommentMapper; import cn.meteor.beyondclouds.modules.post.mapper.PostCommentMapper;
import cn.meteor.beyondclouds.modules.post.service.IPostCommentService; import cn.meteor.beyondclouds.modules.post.service.IPostCommentService;
import cn.meteor.beyondclouds.modules.post.service.IPostService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
/** /**
* <p> * <p>
...@@ -17,4 +28,129 @@ import org.springframework.stereotype.Service; ...@@ -17,4 +28,129 @@ import org.springframework.stereotype.Service;
@Service @Service
public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment> implements IPostCommentService { public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment> implements IPostCommentService {
private IPostService postService;
@Autowired
public void setPostService(IPostService postService) {
this.postService = postService;
}
/**
* 发布评论
* @param postId
* @param parentId
* @param comment
* @param userId
*/
@Override
public void createComment(String postId, Integer parentId, String comment, String userId) throws PostServiceException, PostCommentServiceException {
//1.判断动态是否存在
Post post = postService.getById(postId);
if (null == post) {
throw new PostServiceException(PostErrorCode.POST_NOT_FOUND);
}
//2.如果有parentId判断父评论是否存在
PostComment parentComment = null;
if (null != parentId) {
QueryWrapper<PostComment> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("post_id", postId);
queryWrapper.eq("comment_id", parentId);
parentComment = getOne(queryWrapper);
if (null == parentComment) {
throw new PostCommentServiceException(PostCommentErrorCode.PARENT_COMMENT_NOT_FOUND);
}
}
//3.保存评论
PostComment postComment = new PostComment();
postComment.setParentId(parentId);
postComment.setComment(comment);
postComment.setUserId(userId);
postComment.setPostId(postId);
save(postComment);
//更新深度和路径
if (null == parentComment) {
//一级评论
postComment.setDepth(0);
postComment.setThread("/"+postComment.getCommentId());
} else {
//子级评论
postComment.setDepth(parentComment.getDepth()+1);
postComment.setThread(parentComment.getThread()+"/"+postComment.getCommentId());
}
updateById(postComment);
}
/**
* 删除评论
* @param commentId
* @param userId
* @throws PostCommentServiceException
*/
@Override
public void deleteComment(Integer commentId, String userId) throws PostCommentServiceException {
Assert.notNull(commentId, "commentId must not be null");
Assert.notNull(userId, "userId must not be null");
//1.判断是不是自己的评论
PostComment postComment = getById(commentId);
if (null == postComment) {
throw new PostCommentServiceException(PostCommentErrorCode.COMMENT_NOT_FOUND);
}
//2.判断是不是自己的评论
if (!userId.equals(postComment.getUserId())) {
//3.判断是不是自己的博客
Post post = postService.getById(postComment.getPostId());
if (!post.getUserId().equals(userId)) {
throw new PostCommentServiceException(PostCommentErrorCode.NO_DELETE_PRIVILEGES);
}
}
QueryWrapper postCommentQueryWrapper = new QueryWrapper();
postCommentQueryWrapper.like("thread", postComment.getThread());
remove(postCommentQueryWrapper);
}
/**
* 评论列表
* @param pageNumber
* @param size
* @param postId
* @param parentId
* @return
* @throws PostCommentServiceException
*/
@Override
public IPage<PostComment> getCommentPage(Integer pageNumber, Integer size, String postId, Integer parentId) throws PostCommentServiceException {
Assert.notNull(postId, "postId must not null");
IPage<PostComment> page = new Page(pageNumber, size);
//如果parentId为null,则只获取一级评论
if (null == parentId) {
QueryWrapper postCommentQueryWrapper = new QueryWrapper();
postCommentQueryWrapper.eq("post_id", postId);
postCommentQueryWrapper.eq("depth", 0);
postCommentQueryWrapper.orderByDesc("create_time");
return page(page,postCommentQueryWrapper);
}
//如果parentId不为null,则获取其子查询
PostComment postComment = getById(parentId);
if (null == postComment) {
throw new PostCommentServiceException(PostCommentErrorCode.PARENT_COMMENT_NOT_FOUND);
}
QueryWrapper postCommentQueryWrapper = new QueryWrapper();
postCommentQueryWrapper.eq("parent_id", parentId);
//防止假传parentId
PostComment sunComment = getOne(postCommentQueryWrapper);
if (null == sunComment) {
throw new PostCommentServiceException(PostCommentErrorCode.SUN_COMMENT_NOT_FOUND);
}
return page(page,postCommentQueryWrapper);
}
} }
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