From afd0b2caf7656b423e0b487b70ab2030585e8bc4 Mon Sep 17 00:00:00 2001 From: ronger Date: Sat, 7 Mar 2020 21:18:22 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E8=AF=84=E8=AE=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/constant/NotificationConstant.java | 2 + .../com/rymcu/vertical/dto/ArticleDTO.java | 2 + .../com/rymcu/vertical/dto/CommentDTO.java | 40 ++++++++ .../com/rymcu/vertical/entity/Comment.java | 2 +- .../rymcu/vertical/mapper/CommentMapper.java | 36 +++++++ .../vertical/service/CommentService.java | 16 +++ .../service/impl/ArticleServiceImpl.java | 18 +++- .../service/impl/CommentServiceImpl.java | 97 +++++++++++++++++++ .../vertical/service/impl/TagServiceImpl.java | 5 + .../vertical/util/NotificationUtils.java | 2 +- .../web/api/article/ArticleController.java | 13 +++ .../web/api/comment/CommentController.java | 34 +++++++ 12 files changed, 262 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/rymcu/vertical/dto/CommentDTO.java create mode 100644 src/main/java/com/rymcu/vertical/mapper/CommentMapper.java create mode 100644 src/main/java/com/rymcu/vertical/service/CommentService.java create mode 100644 src/main/java/com/rymcu/vertical/service/impl/CommentServiceImpl.java create mode 100644 src/main/java/com/rymcu/vertical/web/api/comment/CommentController.java diff --git a/src/main/java/com/rymcu/vertical/core/constant/NotificationConstant.java b/src/main/java/com/rymcu/vertical/core/constant/NotificationConstant.java index 41e53a1..19bbb61 100644 --- a/src/main/java/com/rymcu/vertical/core/constant/NotificationConstant.java +++ b/src/main/java/com/rymcu/vertical/core/constant/NotificationConstant.java @@ -10,4 +10,6 @@ public class NotificationConstant { public static String Follow = "1"; + public static String Comment = "2"; + } diff --git a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java index ce315ea..ea40f7f 100644 --- a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java +++ b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java @@ -47,4 +47,6 @@ public class ArticleDTO { private Author articleAuthor; private List tags; + + private List articleComments; } diff --git a/src/main/java/com/rymcu/vertical/dto/CommentDTO.java b/src/main/java/com/rymcu/vertical/dto/CommentDTO.java new file mode 100644 index 0000000..0375d6c --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/CommentDTO.java @@ -0,0 +1,40 @@ +package com.rymcu.vertical.dto; + +import lombok.Data; + +import javax.persistence.Column; +import java.util.Date; + +/** + * @author ronger + */ +@Data +public class CommentDTO { + private Integer idComment; + /** 评论内容 */ + private String commentContent; + /** 作者 id */ + private Integer commentAuthorId; + /** 文章 id */ + private Integer commentArticleId; + /** 锚点 url */ + private String commentSharpUrl; + /** 父评论 id */ + private Integer commentOriginalCommentId; + /** 父评论作者头像 */ + private String commentOriginalAuthorThumbnailURL; + /** 状态 */ + private String commentStatus; + /** 0:公开回帖,1:匿名回帖 */ + private String commentAnonymous; + /** 回帖计数 */ + private Integer commentReplyCount; + /** 0:所有人可见,1:仅楼主和自己可见 */ + private String commentVisible; + /** 创建时间 */ + private Date createdTime; + + private Author commenter; + + private String timeAgo; +} diff --git a/src/main/java/com/rymcu/vertical/entity/Comment.java b/src/main/java/com/rymcu/vertical/entity/Comment.java index e8fb312..d9b71cc 100644 --- a/src/main/java/com/rymcu/vertical/entity/Comment.java +++ b/src/main/java/com/rymcu/vertical/entity/Comment.java @@ -19,7 +19,7 @@ public class Comment implements Serializable,Cloneable { @Id @GeneratedValue(generator = "JDBC") @Column(name = "id") - private Integer id; + private Integer idComment; /** 评论内容 */ @Column(name = "comment_content") private String commentContent; diff --git a/src/main/java/com/rymcu/vertical/mapper/CommentMapper.java b/src/main/java/com/rymcu/vertical/mapper/CommentMapper.java new file mode 100644 index 0000000..fba4a79 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/mapper/CommentMapper.java @@ -0,0 +1,36 @@ +package com.rymcu.vertical.mapper; + +import com.rymcu.vertical.core.mapper.Mapper; +import com.rymcu.vertical.dto.Author; +import com.rymcu.vertical.dto.CommentDTO; +import com.rymcu.vertical.entity.Comment; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface CommentMapper extends Mapper { + /** + * @param idArticle + * @return + */ + List selectArticleComments(@Param("idArticle") Integer idArticle); + + /** + * @param commentAuthorId + * @return + */ + Author selectAuthor(@Param("commentAuthorId") Integer commentAuthorId); + + /** + * @param commentOriginalCommentId + * @return + */ + Author selectCommentOriginalAuthor(@Param("commentOriginalCommentId") Integer commentOriginalCommentId); + + /** + * @param idComment + * @param toString + * @return + */ + Integer updateCommentSharpUrl(@Param("idComment") Integer idComment, @Param("commentSharpUrl") String commentSharpUrl); +} diff --git a/src/main/java/com/rymcu/vertical/service/CommentService.java b/src/main/java/com/rymcu/vertical/service/CommentService.java new file mode 100644 index 0000000..5324a42 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/CommentService.java @@ -0,0 +1,16 @@ +package com.rymcu.vertical.service; + +import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.dto.CommentDTO; +import com.rymcu.vertical.entity.Comment; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; +import java.util.Map; + +public interface CommentService extends Service { + + List getArticleComments(Integer idArticle); + + Map postComment(Comment comment, HttpServletRequest request); +} diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index 1178ab0..4b7ada1 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -5,12 +5,14 @@ import com.rymcu.vertical.core.service.AbstractService; import com.rymcu.vertical.dto.ArticleDTO; import com.rymcu.vertical.dto.ArticleTagDTO; import com.rymcu.vertical.dto.Author; +import com.rymcu.vertical.dto.CommentDTO; import com.rymcu.vertical.entity.Article; import com.rymcu.vertical.entity.ArticleContent; import com.rymcu.vertical.entity.Tag; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.mapper.ArticleMapper; import com.rymcu.vertical.service.ArticleService; +import com.rymcu.vertical.service.CommentService; import com.rymcu.vertical.service.TagService; import com.rymcu.vertical.service.UserService; import com.rymcu.vertical.util.*; @@ -42,9 +44,13 @@ public class ArticleServiceImpl extends AbstractService
implements Arti private TagService tagService; @Resource private UserService userService; + @Resource + private CommentService commentService; @Value("${resource.domain}") - private static String domain; + private String domain; + @Value("${env}") + private String env; private static final int MAX_PREVIEW = 200; @@ -125,7 +131,9 @@ public class ArticleServiceImpl extends AbstractService
implements Arti newArticle.setArticlePermalink(domain + "/article/"+newArticle.getIdArticle()); newArticle.setArticleLink("/article/"+newArticle.getIdArticle()); articleMapper.insertArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml); - BaiDuUtils.sendSEOData(newArticle.getArticlePermalink()); + if (!"dev".equals(env)) { + BaiDuUtils.sendSEOData(newArticle.getArticlePermalink()); + } } else { newArticle = articleMapper.selectByPrimaryKey(article.getIdArticle()); if(!user.getIdUser().equals(newArticle.getArticleAuthorId())){ @@ -144,7 +152,9 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } newArticle.setUpdatedTime(new Date()); articleMapper.updateArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml); - BaiDuUtils.updateSEOData(newArticle.getArticlePermalink()); + if (!"dev".equals(env)) { + BaiDuUtils.updateSEOData(newArticle.getArticlePermalink()); + } } if (notification) { @@ -239,6 +249,8 @@ public class ArticleServiceImpl extends AbstractService
implements Arti String articlePreviewContent = articleContent.getArticleContentHtml().substring(0,length); article.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); } + List commentDTOList = commentService.getArticleComments(article.getIdArticle()); + article.setArticleComments(commentDTOList); return article; } } diff --git a/src/main/java/com/rymcu/vertical/service/impl/CommentServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/CommentServiceImpl.java new file mode 100644 index 0000000..2aef75d --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/impl/CommentServiceImpl.java @@ -0,0 +1,97 @@ +package com.rymcu.vertical.service.impl; + +import com.rymcu.vertical.core.constant.NotificationConstant; +import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.dto.Author; +import com.rymcu.vertical.dto.CommentDTO; +import com.rymcu.vertical.entity.Article; +import com.rymcu.vertical.entity.Comment; +import com.rymcu.vertical.mapper.CommentMapper; +import com.rymcu.vertical.service.ArticleService; +import com.rymcu.vertical.service.CommentService; +import com.rymcu.vertical.util.Html2TextUtil; +import com.rymcu.vertical.util.NotificationUtils; +import com.rymcu.vertical.util.Utils; +import org.apache.commons.lang.StringUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.util.*; + +@Service +public class CommentServiceImpl extends AbstractService implements CommentService { + + @Resource + private CommentMapper commentMapper; + @Resource + private ArticleService articleService; + + private static final int MAX_PREVIEW = 200; + + @Override + public List getArticleComments(Integer idArticle) { + List commentDTOList = commentMapper.selectArticleComments(idArticle); + commentDTOList.forEach(commentDTO -> { + commentDTO.setTimeAgo(Utils.getTimeAgo(commentDTO.getCreatedTime())); + if (commentDTO.getCommentAuthorId() != null) { + Author author = commentMapper.selectAuthor(commentDTO.getCommentAuthorId()); + if (author != null) { + commentDTO.setCommenter(author); + } + } + if (commentDTO.getCommentOriginalCommentId() != null) { + Author commentOriginalAuthor = commentMapper.selectCommentOriginalAuthor(commentDTO.getCommentOriginalCommentId()); + if (commentOriginalAuthor != null) { + commentDTO.setCommentOriginalAuthorThumbnailURL(commentOriginalAuthor.getUserAvatarURL()); + } + } + }); + return commentDTOList; + } + + @Override + public Map postComment(Comment comment, HttpServletRequest request) { + Map map = new HashMap(1); + if(comment.getCommentArticleId() == null){ + map.put("message","非法访问,文章主键异常!"); + return map; + } + if(comment.getCommentAuthorId() == null){ + map.put("message","非法访问,用户未登录!"); + return map; + } + if(StringUtils.isBlank(comment.getCommentContent())){ + map.put("message","回帖内容不能为空!"); + return map; + } + Article article = articleService.findById(comment.getCommentArticleId().toString()); + if (article == null) { + map.put("message","文章不存在!"); + return map; + } + String ip = Utils.getIpAddress(request); + String ua = request.getHeader("user-agent"); + comment.setCommentIP(ip); + comment.setCommentUA(ua); + comment.setCreatedTime(new Date()); + commentMapper.insertSelective(comment); + StringBuilder commentSharpUrl = new StringBuilder(article.getArticlePermalink()); + commentSharpUrl.append("/comment/").append(comment.getIdComment()); + commentMapper.updateCommentSharpUrl(comment.getIdComment(), commentSharpUrl.toString()); + + String commentContent = comment.getCommentContent(); + if(StringUtils.isNotBlank(commentContent)){ + Integer length = commentContent.length(); + if(length > MAX_PREVIEW){ + length = 200; + } + String commentPreviewContent = commentContent.substring(0,length); + commentContent = Html2TextUtil.getContent(commentPreviewContent); + } + + NotificationUtils.saveNotification(article.getArticleAuthorId(),comment.getIdComment(), NotificationConstant.Comment, commentContent); + + return map; + } +} diff --git a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java index 4c02be0..256e56a 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java @@ -1,9 +1,11 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.dto.ArticleTagDTO; import com.rymcu.vertical.entity.Article; import com.rymcu.vertical.entity.Tag; import com.rymcu.vertical.entity.User; +import com.rymcu.vertical.mapper.ArticleMapper; import com.rymcu.vertical.mapper.TagMapper; import com.rymcu.vertical.service.TagService; import com.rymcu.vertical.util.UserUtils; @@ -29,6 +31,8 @@ public class TagServiceImpl extends AbstractService implements TagService { @Resource private TagMapper tagMapper; + @Resource + private ArticleMapper articleMapper; @Override @Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class }) @@ -43,6 +47,7 @@ public class TagServiceImpl extends AbstractService implements TagService { Tag tag = new Tag(); tag.setTagTitle(tags[i]); tag = tagMapper.selectOne(tag); + List articleTagDTOList = articleMapper.selectTags(article.getIdArticle()); if(tag == null){ tag = new Tag(); tag.setTagTitle(tags[i]); diff --git a/src/main/java/com/rymcu/vertical/util/NotificationUtils.java b/src/main/java/com/rymcu/vertical/util/NotificationUtils.java index 112f20b..7a42c0c 100644 --- a/src/main/java/com/rymcu/vertical/util/NotificationUtils.java +++ b/src/main/java/com/rymcu/vertical/util/NotificationUtils.java @@ -42,7 +42,7 @@ public class NotificationUtils { },executor); } - private static void saveNotification(Integer idUser, Integer dataId, String dataType, String dataSummary) { + public static void saveNotification(Integer idUser, Integer dataId, String dataType, String dataSummary) { ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); CompletableFuture.supplyAsync(()-> { try { diff --git a/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java b/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java index 631e1c5..5874af4 100644 --- a/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java +++ b/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java @@ -3,7 +3,9 @@ package com.rymcu.vertical.web.api.article; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.dto.ArticleDTO; +import com.rymcu.vertical.dto.CommentDTO; import com.rymcu.vertical.service.ArticleService; +import com.rymcu.vertical.service.CommentService; import com.rymcu.vertical.web.api.exception.BaseApiException; import org.springframework.web.bind.annotation.*; @@ -11,6 +13,7 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -22,6 +25,8 @@ public class ArticleController { @Resource private ArticleService articleService; + @Resource + private CommentService commentService; @@ -51,4 +56,12 @@ public class ArticleController { return GlobalResultGenerator.genSuccessResult(map); } + @GetMapping("/{id}/comments") + public GlobalResult> commons(@PathVariable Integer id){ + List commentDTOList = commentService.getArticleComments(id); + Map map = new HashMap<>(1); + map.put("comments", commentDTOList); + return GlobalResultGenerator.genSuccessResult(map); + } + } diff --git a/src/main/java/com/rymcu/vertical/web/api/comment/CommentController.java b/src/main/java/com/rymcu/vertical/web/api/comment/CommentController.java new file mode 100644 index 0000000..814b625 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/web/api/comment/CommentController.java @@ -0,0 +1,34 @@ +package com.rymcu.vertical.web.api.comment; + +import com.rymcu.vertical.core.result.GlobalResult; +import com.rymcu.vertical.core.result.GlobalResultGenerator; +import com.rymcu.vertical.dto.ArticleDTO; +import com.rymcu.vertical.entity.Comment; +import com.rymcu.vertical.service.CommentService; +import com.rymcu.vertical.web.api.exception.BaseApiException; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.io.UnsupportedEncodingException; +import java.util.Map; + +/** + * @author ronger + */ +@RestController +@RequestMapping("/api/v1/comment") +public class CommentController { + + @Resource + private CommentService commentService; + + @PostMapping("/post") + public GlobalResult postComment(@RequestBody Comment comment, HttpServletRequest request) throws BaseApiException, UnsupportedEncodingException { + Map map = commentService.postComment(comment,request); + return GlobalResultGenerator.genSuccessResult(map); + } +}