Commit 3ea00dc7 by Author name

1.31博客的创建删除和发表评论

parent 5bd26bb1
package cn.meteor.beyondclouds.modules.blog.api;
import cn.meteor.beyondclouds.common.exception.OssException;
import cn.meteor.beyondclouds.common.helper.IOssHelper;
import cn.meteor.beyondclouds.core.annotation.Anonymous;
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.modules.blog.entity.Blog;
import cn.meteor.beyondclouds.modules.blog.entity.BlogExt;
import cn.meteor.beyondclouds.modules.blog.exception.BlogServiceException;
import cn.meteor.beyondclouds.modules.blog.form.PublishBlogForm;
import cn.meteor.beyondclouds.modules.blog.service.IBlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.coyote.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/**
* @author gaoTong
* @date 2020/1/31 9:27
*/
@Api(tags = "微博API")
@RestController
@RequestMapping("/api")
public class BlogApi {
private IBlogService blogService;
@Autowired
public void setBlogService(IBlogService blogService) {
this.blogService = blogService;
}
@ApiOperation("发布博客")
@PostMapping("/blog")
public Response publishBlog (@RequestBody @Valid PublishBlogForm publishBlogForm, BindingResult result, @CurrentSubject Subject subject) throws OssException {
if (result.hasErrors()) {
return Response.fieldError(result.getFieldError());
}
Blog blog = new Blog();
blog.setUserId((String) subject.getId());
blog.setBlogTitle(publishBlogForm.getBlogTitle());
blog.setViewPrivileges(publishBlogForm.getViewPrivileges());
blog.setAllowComment(publishBlogForm.getAllowComment());
blog.setAllowForward(publishBlogForm.getAllowForward());
blog.setOriginLink(publishBlogForm.getOriginLink());
blog.setBlogAbstract(publishBlogForm.getBlogAbstract());
blog.setCategoryId(publishBlogForm.getCategoryId());
blog.setCover(publishBlogForm.getCover());
BlogExt blogExt = new BlogExt();
blogExt.setContent(publishBlogForm.getBlogContent());
blogService.publishBlog(blog, blogExt,
publishBlogForm.getTopicIds(), publishBlogForm.getTagIds());
return Response.success();
}
@ApiOperation("删除博客")
@PostMapping("/blog/{blogId}")
public Response deleteBlog(@PathVariable("blogId") String blogId,@CurrentSubject Subject subject) {
try {
blogService.deleteBlog((String) subject.getId(), blogId);
return Response.success();
} catch (BlogServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
}
package cn.meteor.beyondclouds.modules.blog.api;
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.modules.blog.form.BlogCommentForm;
import cn.meteor.beyondclouds.modules.blog.service.IBlogCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/**
* @author gaoTong
* @date 2020/1/31 16:13
*/
@Api(tags = "博客评论API")
@RestController
@RequestMapping("/api")
public class BlogCommentApi {
private IBlogCommentService blogCommentService;
public BlogCommentApi(IBlogCommentService blogCommentService) {
this.blogCommentService = blogCommentService;
}
@ApiOperation("创建评论")
@PostMapping("/blog/{blogId}/comment")
public Response commentCreate (@PathVariable("blogId") String blogId, @RequestBody @Valid BlogCommentForm blogCommentForm, @CurrentSubject Subject subject){
blogCommentService.commentCreate(blogId, blogCommentForm.getParentId(),
blogCommentForm.getComment(), (String) subject.getId());
return Response.success();
}
}
package cn.meteor.beyondclouds.modules.blog.enums;
import cn.meteor.beyondclouds.core.IErrorCode;
/**
* @author gaoTong
* @date 2020/1/31 9:21
*/
public enum BlogCommentErrorCode implements IErrorCode {
;
private long code;
private String msg;
BlogCommentErrorCode(long code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public long code() {
return code;
}
@Override
public String msg() {
return msg;
}
}
package cn.meteor.beyondclouds.modules.blog.enums;
import cn.meteor.beyondclouds.core.IErrorCode;
/**
* @author gaoTong
* @date 2020/1/31 9:20
*/
public enum BlogErrorCode implements IErrorCode {
USERID_AUTH_ERROR(8001,"不是当前博客的拥有者"),
BLOG_NOT_EXIST(8002,"当前博客已经被不存在")
;
private long code;
private String msg;
BlogErrorCode(long code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public long code() {
return code;
}
@Override
public String msg() {
return msg;
}
}
package cn.meteor.beyondclouds.modules.blog.exception;
import cn.meteor.beyondclouds.core.IErrorCode;
import cn.meteor.beyondclouds.core.exception.ServiceException;
/**
* @author gaoTong
* @date 2020/1/31 9:18
*/
public class BlogCategoryServiceException extends ServiceException {
public BlogCategoryServiceException(long errorCode, String errorMsg) {
super(errorCode, errorMsg);
}
public BlogCategoryServiceException(IErrorCode errorCode) {
super(errorCode.code() , errorCode.msg());
}
}
package cn.meteor.beyondclouds.modules.blog.exception;
import cn.meteor.beyondclouds.core.IErrorCode;
import cn.meteor.beyondclouds.core.exception.ServiceException;
/**
* @author gaoTong
* @date 2020/1/31 9:17
*/
public class BlogCommentServiceException extends ServiceException {
public BlogCommentServiceException(long errorCode, String errorMsg) {
super(errorCode, errorMsg);
}
public BlogCommentServiceException(IErrorCode errorCode) {
super(errorCode.code() , errorCode.msg());
}
}
package cn.meteor.beyondclouds.modules.blog.exception;
import cn.meteor.beyondclouds.core.IErrorCode;
import cn.meteor.beyondclouds.core.exception.ServiceException;
/**
* @author gaoTong
* @date 2020/1/31 9:16
*/
public class BlogServiceException extends ServiceException {
public BlogServiceException(long errorCode, String errorMsg) {
super(errorCode, errorMsg);
}
public BlogServiceException(IErrorCode errorCode) {
super(errorCode);
}
}
package cn.meteor.beyondclouds.modules.blog.form;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* @author gaoTong
* @date 2020/1/31 16:15
*/
@Data
public class BlogCommentForm {
private Integer parentId;
@NotEmpty(message = "评论内容不能为空")
private String comment;
}
package cn.meteor.beyondclouds.modules.blog.form;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
/**
* @author gaoTong
* @date 2020/1/31 9:36
*/
/*
blogTitle:标题
blogType:文章类型
originLink:原文链接
blogAbstract:摘要(自动或手动)
blogContent:内容
topicIds: [博客引用的话题]
tagIds:[博客引用的标签]
attribute:属性(是否原创)
type:文章类型
viewPrivileges:浏览权限
allowComment:评论权限
allowForward:转载权限
cover:封面图
*/
@Data
public class PublishBlogForm {
@NotEmpty(message = "标题不能为空")
private String blogTitle;
@NotNull(message = "文章类型不能为空")
private Integer blogType;
private String originLink;
@NotEmpty(message = "摘要不能为空")
private String blogAbstract;
@NotNull(message = "文章类型不能为空")
private Integer categoryId;
@NotEmpty(message = "内容不能为空")
private String blogContent;
private List<String> topicIds;
private List<String> tagIds;
@NotNull(message = "浏览权限不能为空")
private Integer viewPrivileges;
@NotNull(message = "评论权限不能为空")
private Boolean allowComment;
@NotNull(message = "转载权限不能为空")
private Boolean allowForward;
@NotEmpty(message = "封面图不能为空")
private String cover;
}
...@@ -13,4 +13,6 @@ import com.baomidou.mybatisplus.extension.service.IService; ...@@ -13,4 +13,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/ */
public interface IBlogCommentService extends IService<BlogComment> { public interface IBlogCommentService extends IService<BlogComment> {
void commentCreate(String blogId,Integer parentId,String comment,String userId);
} }
package cn.meteor.beyondclouds.modules.blog.service; package cn.meteor.beyondclouds.modules.blog.service;
import cn.meteor.beyondclouds.modules.blog.entity.Blog; import cn.meteor.beyondclouds.modules.blog.entity.Blog;
import cn.meteor.beyondclouds.modules.blog.entity.BlogExt;
import cn.meteor.beyondclouds.modules.blog.entity.BlogTag;
import cn.meteor.beyondclouds.modules.blog.exception.BlogServiceException;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/** /**
* <p> * <p>
* 博客表 服务类 * 博客表 服务类
...@@ -13,4 +18,19 @@ import com.baomidou.mybatisplus.extension.service.IService; ...@@ -13,4 +18,19 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/ */
public interface IBlogService extends IService<Blog> { public interface IBlogService extends IService<Blog> {
/**
* 发布博客
* @param blog
* @param blogExt
* @param topicIds
* @param tagIds
*/
void publishBlog(Blog blog , BlogExt blogExt , List<String> topicIds , List<String> tagIds);
/**
* 删除博客
* @param userId
* @param BlogId
*/
void deleteBlog(String userId , String BlogId) throws BlogServiceException;
} }
...@@ -3,6 +3,7 @@ package cn.meteor.beyondclouds.modules.blog.service.impl; ...@@ -3,6 +3,7 @@ package cn.meteor.beyondclouds.modules.blog.service.impl;
import cn.meteor.beyondclouds.modules.blog.entity.BlogComment; import cn.meteor.beyondclouds.modules.blog.entity.BlogComment;
import cn.meteor.beyondclouds.modules.blog.mapper.BlogCommentMapper; import cn.meteor.beyondclouds.modules.blog.mapper.BlogCommentMapper;
import cn.meteor.beyondclouds.modules.blog.service.IBlogCommentService; import cn.meteor.beyondclouds.modules.blog.service.IBlogCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -17,4 +18,46 @@ import org.springframework.stereotype.Service; ...@@ -17,4 +18,46 @@ import org.springframework.stereotype.Service;
@Service @Service
public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogComment> implements IBlogCommentService { public class BlogCommentServiceImpl extends ServiceImpl<BlogCommentMapper, BlogComment> implements IBlogCommentService {
@Override
public void commentCreate(String blogId, Integer parentId, String comment, String userId) {
//定义路径
String thread;
int depth;
//1.判断为第几级评论
if(parentId == null){
depth = 0;
}else {
QueryWrapper<BlogComment> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("comment_id", parentId);
BlogComment blogCommentParent = getOne(queryWrapper);
depth = blogCommentParent.getDepth()+1;
}
//2.创建评论
BlogComment blogComment = new BlogComment();
blogComment.setBlogId(blogId);
blogComment.setComment(comment);
blogComment.setDepth(depth);
blogComment.setParentId(parentId);
blogComment.setUserId(userId);
save(blogComment);
//3.查找上一层目录
if(blogComment.getParentId() == null) {
thread = "/"+ blogComment.getCommentId();
}else {
QueryWrapper<BlogComment> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("comment_id", blogComment.getParentId());
//重新创建对象 存储上一级目录
BlogComment blogCommentPatent = getOne(queryWrapper);
thread = blogCommentPatent.getThread()+"/"+blogComment.getCommentId();
}
blogComment.setThread(thread);
//4.更新路径信息
updateById(blogComment);
}
} }
package cn.meteor.beyondclouds.modules.blog.service.impl; package cn.meteor.beyondclouds.modules.blog.service.impl;
import cn.meteor.beyondclouds.modules.blog.entity.Blog; import cn.meteor.beyondclouds.modules.blog.entity.Blog;
import cn.meteor.beyondclouds.modules.blog.entity.BlogExt;
import cn.meteor.beyondclouds.modules.blog.entity.BlogTag;
import cn.meteor.beyondclouds.modules.blog.enums.BlogErrorCode;
import cn.meteor.beyondclouds.modules.blog.exception.BlogServiceException;
import cn.meteor.beyondclouds.modules.blog.mapper.BlogMapper; import cn.meteor.beyondclouds.modules.blog.mapper.BlogMapper;
import cn.meteor.beyondclouds.modules.blog.service.IBlogCommentService;
import cn.meteor.beyondclouds.modules.blog.service.IBlogExtService;
import cn.meteor.beyondclouds.modules.blog.service.IBlogService; import cn.meteor.beyondclouds.modules.blog.service.IBlogService;
import cn.meteor.beyondclouds.modules.blog.service.IBlogTagService;
import cn.meteor.beyondclouds.modules.topic.entity.TopicReference;
import cn.meteor.beyondclouds.modules.topic.service.ITopicReferenceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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 java.util.List;
/** /**
* <p> * <p>
* 博客表 服务实现类 * 博客表 服务实现类
...@@ -17,4 +31,92 @@ import org.springframework.stereotype.Service; ...@@ -17,4 +31,92 @@ import org.springframework.stereotype.Service;
@Service @Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService { public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {
private IBlogTagService blogTagService;
private ITopicReferenceService topicReferenceService;
private IBlogExtService blogExtService;
private IBlogCommentService blogCommentService;
public BlogServiceImpl(IBlogTagService blogTagService, ITopicReferenceService topicReferenceService, IBlogExtService blogExtService, IBlogCommentService blogCommentService) {
this.blogTagService = blogTagService;
this.topicReferenceService = topicReferenceService;
this.blogExtService = blogExtService;
this.blogCommentService = blogCommentService;
}
@Override
public void publishBlog(Blog blog, BlogExt blogExt, List<String> topicIds, List<String> tagIds) {
//1.发布博客
save(blog);
System.out.println(blog.getBlogId());
//2.存入内容
blogExt.setBlogId(blog.getBlogId());
blogExtService.save(blogExt);
//3.判断是否引用话题
if (null != topicIds) {
for (String topicId : topicIds) {
TopicReference topicReference = new TopicReference();
topicReference.setTopicId(topicId);
topicReference.setReferencerId(blog.getBlogId());
topicReference.setReferencerType(0);
topicReferenceService.save(topicReference);
}
}
//4.判断是否引用标签
if (null != tagIds) {
for (String tagId : tagIds) {
BlogTag blogTag = new BlogTag();
blogTag.setBlogId(blog.getBlogId());
blogTag.setTagId(tagId);
blogTagService.save(blogTag);
}
}
}
@Override
public void deleteBlog(String userId, String blogId) throws BlogServiceException {
//1.判断要删除的博客是否是当前用户所有
Blog blog = getById(blogId);
if (!userId.equals(blog.getUserId())) {
throw new BlogServiceException(BlogErrorCode.USERID_AUTH_ERROR);
}
//2.判断当前博客是否存在
if (getById(blogId) == null) {
throw new BlogServiceException(BlogErrorCode.BLOG_NOT_EXIST);
}
//3.删除博客评论表
QueryWrapper queryWrapperComment = new QueryWrapper();
queryWrapperComment.eq("blog_id",blogId);
blogCommentService.remove(queryWrapperComment);
//4.删除博客内容
QueryWrapper queryWrapperExt = new QueryWrapper();
queryWrapperComment.eq("blog_id", blogId);
blogExtService.remove(queryWrapperExt);
//5.删除博客标签
QueryWrapper queryWrapperTag = new QueryWrapper();
queryWrapperTag.eq("blog_id", blogId);
blogTagService.remove(queryWrapperTag);
//7.删除博客引用的话题
QueryWrapper queryWrapperTopic = new QueryWrapper();
queryWrapperTopic.eq("referencer_id", blogId);
topicReferenceService.remove(queryWrapperTopic);
//8.删除博客表里的数据
removeById(blogId);
}
} }
...@@ -3,7 +3,7 @@ spring: ...@@ -3,7 +3,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true url: jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username: root username: root
password: 100Centa30821%mysql password: 123123
swagger: swagger:
enable: true enable: true
......
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