From c94b5397b34eeec4d8ecdf7ea29c4af18478509b Mon Sep 17 00:00:00 2001 From: ronger Date: Mon, 30 Mar 2020 17:38:21 +0800 Subject: [PATCH] Merge branch 'master' into dev # Conflicting files # src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java # src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java # src/main/java/com/rymcu/vertical/util/BaiDuUtils.java # src/main/java/com/rymcu/vertical/web/api/comment/CommentController.java --- .../service/impl/ArticleServiceImpl.java | 73 +++++++++++++++---- .../vertical/service/impl/TagServiceImpl.java | 29 ++++++++ .../com/rymcu/vertical/util/BaiDuUtils.java | 4 +- .../web/api/comment/CommentController.java | 33 +++++++++ 4 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/rymcu/vertical/web/api/comment/CommentController.java 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..802c65a 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -1,16 +1,19 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.constant.NotificationConstant; +import com.rymcu.vertical.core.constant.ProjectConstant; 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,11 +45,16 @@ 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; + private static final String defaultStatus = "0"; @Override public List findArticles(String searchText, String tag) { @@ -58,8 +66,11 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } @Override - public ArticleDTO findArticleDTOById(Integer id, int type) { - ArticleDTO articleDTO = articleMapper.selectArticleDTOById(id); + public ArticleDTO findArticleDTOById(Integer id, Integer type) { + ArticleDTO articleDTO = articleMapper.selectArticleDTOById(id,type); + if (articleDTO == null) { + return null; + } articleDTO = genArticle(articleDTO,type); return articleDTO; } @@ -67,6 +78,9 @@ public class ArticleServiceImpl extends AbstractService
implements Arti @Override public List findArticlesByTopicUri(String name) { List articleDTOS = articleMapper.selectArticlesByTopicUri(name); + articleDTOS.forEach(articleDTO -> { + genArticle(articleDTO,0); + }); return articleDTOS; } @@ -121,11 +135,12 @@ public class ArticleServiceImpl extends AbstractService
implements Arti newArticle.setArticleTags(articleTags); newArticle.setCreatedTime(new Date()); newArticle.setUpdatedTime(newArticle.getCreatedTime()); + newArticle.setArticleStatus(article.getArticleStatus()); articleMapper.insertSelective(newArticle); - newArticle.setArticlePermalink(domain + "/article/"+newArticle.getIdArticle()); - newArticle.setArticleLink("/article/"+newArticle.getIdArticle()); articleMapper.insertArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml); - BaiDuUtils.sendSEOData(newArticle.getArticlePermalink()); + if (!ProjectConstant.ENV.equals(env) && defaultStatus.equals(newArticle.getArticleStatus())) { + BaiDuUtils.sendSEOData(newArticle.getArticlePermalink()); + } } else { newArticle = articleMapper.selectByPrimaryKey(article.getIdArticle()); if(!user.getIdUser().equals(newArticle.getArticleAuthorId())){ @@ -142,16 +157,27 @@ public class ArticleServiceImpl extends AbstractService
implements Arti String articlePreviewContent = articleContentHtml.substring(0,length); newArticle.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); } + newArticle.setArticleStatus(article.getArticleStatus()); newArticle.setUpdatedTime(new Date()); articleMapper.updateArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml); - BaiDuUtils.updateSEOData(newArticle.getArticlePermalink()); + if (!ProjectConstant.ENV.equals(env) && defaultStatus.equals(newArticle.getArticleStatus())) { + BaiDuUtils.updateSEOData(newArticle.getArticlePermalink()); + } } - if (notification) { + if (notification && defaultStatus.equals(newArticle.getArticleStatus())) { NotificationUtils.sendAnnouncement(newArticle.getIdArticle(), NotificationConstant.Article, newArticle.getArticleTitle()); } tagService.saveTagArticle(newArticle); + + if (defaultStatus.equals(newArticle.getArticleStatus())) { + newArticle.setArticlePermalink(domain + "/article/" + newArticle.getIdArticle()); + newArticle.setArticleLink("/article/" + newArticle.getIdArticle()); + } else { + newArticle.setArticlePermalink(domain + "/draft/" + newArticle.getIdArticle()); + newArticle.setArticleLink("/draft/" + newArticle.getIdArticle()); + } articleMapper.updateByPrimaryKeySelective(newArticle); map.put("id", newArticle.getIdArticle()); @@ -216,22 +242,41 @@ public class ArticleServiceImpl extends AbstractService
implements Arti articleMapper.updateArticleViewCount(article.getIdArticle(), articleViewCount); } + @Override + public Map share(Integer id) throws BaseApiException { + Article article = articleMapper.selectByPrimaryKey(id); + User user = UserUtils.getWxCurrentUser(); + StringBuilder shareUrl = new StringBuilder(article.getArticlePermalink()); + shareUrl.append("?s=").append(user.getNickname()); + Map map = new HashMap(1); + map.put("shareUrl", shareUrl); + return map; + } + + @Override + public List findDrafts() throws BaseApiException { + User user = UserUtils.getWxCurrentUser(); + List list = articleMapper.selectDrafts(user.getIdUser()); + list.forEach(article->{ + genArticle(article,0); + }); + return list; + } + private ArticleDTO genArticle(ArticleDTO article,Integer type) { Author author = articleMapper.selectAuthor(article.getArticleAuthorId()); article.setArticleAuthor(author); article.setTimeAgo(Utils.getTimeAgo(article.getUpdatedTime())); List tags = articleMapper.selectTags(article.getIdArticle()); article.setTags(tags); - if(type == 1){ - ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); + ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); + if (type == 1){ article.setArticleContent(articleContent.getArticleContentHtml()); - } else if(type == 2){ - ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); + } else if (type == 2) { article.setArticleContent(articleContent.getArticleContent()); } if(StringUtils.isBlank(article.getArticlePreviewContent())){ - ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); Integer length = articleContent.getArticleContentHtml().length(); if(length > MAX_PREVIEW){ length = 200; @@ -239,6 +284,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/TagServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java index 4c02be0..16b25fd 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java @@ -1,11 +1,16 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.core.service.redis.RedisService; +import com.rymcu.vertical.dto.ArticleTagDTO; +import com.rymcu.vertical.dto.LabelModel; 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.CacheUtils; import com.rymcu.vertical.util.UserUtils; import com.rymcu.vertical.web.api.exception.BaseApiException; import org.apache.commons.lang.StringUtils; @@ -29,6 +34,10 @@ public class TagServiceImpl extends AbstractService implements TagService { @Resource private TagMapper tagMapper; + @Resource + private ArticleMapper articleMapper; + @Resource + private RedisService redisService; @Override @Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class }) @@ -37,6 +46,7 @@ public class TagServiceImpl extends AbstractService implements TagService { String articleTags = article.getArticleTags(); if(StringUtils.isNotBlank(articleTags)){ String[] tags = articleTags.split(","); + List articleTagDTOList = articleMapper.selectTags(article.getIdArticle()); for (int i = 0; i < tags.length; i++) { boolean addTagArticle = false; boolean addUserTag = false; @@ -54,6 +64,12 @@ public class TagServiceImpl extends AbstractService implements TagService { addTagArticle = true; addUserTag = true; } else { + for(int m=0,n=articleTagDTOList.size()-1;m implements TagService { addUserTag = true; } } + articleTagDTOList.forEach(articleTagDTO -> { + articleMapper.deleteUnusedArticleTag(articleTagDTO.getIdArticleTag()); + }); if(addTagArticle){ tagMapper.insertTagArticle(tag.getIdTag(),article.getIdArticle()); } @@ -125,4 +144,14 @@ public class TagServiceImpl extends AbstractService implements TagService { } return map; } + + @Override + public List findTagLabels() { + List list = (List) CacheUtils.get("tags"); + if (list == null) { + list = tagMapper.selectTagLabels(); + CacheUtils.put("tags", list); + } + return list; + } } diff --git a/src/main/java/com/rymcu/vertical/util/BaiDuUtils.java b/src/main/java/com/rymcu/vertical/util/BaiDuUtils.java index 5e83cd0..4a803c7 100644 --- a/src/main/java/com/rymcu/vertical/util/BaiDuUtils.java +++ b/src/main/java/com/rymcu/vertical/util/BaiDuUtils.java @@ -77,7 +77,7 @@ public class BaiDuUtils { },executor); } - public static void main(String agrs[]){ - sendSEOData("https://rymcu.com/article/31"); + public static void main(String[] args){ + sendSEOData("https://rymcu.com"); } } 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..07999fe --- /dev/null +++ b/src/main/java/com/rymcu/vertical/web/api/comment/CommentController.java @@ -0,0 +1,33 @@ +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.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); + } +}