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
This commit is contained in:
ronger 2020-03-30 17:38:21 +08:00
parent 766105022c
commit c94b5397b3
4 changed files with 124 additions and 15 deletions

View File

@ -1,16 +1,19 @@
package com.rymcu.vertical.service.impl; package com.rymcu.vertical.service.impl;
import com.rymcu.vertical.core.constant.NotificationConstant; 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.core.service.AbstractService;
import com.rymcu.vertical.dto.ArticleDTO; import com.rymcu.vertical.dto.ArticleDTO;
import com.rymcu.vertical.dto.ArticleTagDTO; import com.rymcu.vertical.dto.ArticleTagDTO;
import com.rymcu.vertical.dto.Author; import com.rymcu.vertical.dto.Author;
import com.rymcu.vertical.dto.CommentDTO;
import com.rymcu.vertical.entity.Article; import com.rymcu.vertical.entity.Article;
import com.rymcu.vertical.entity.ArticleContent; import com.rymcu.vertical.entity.ArticleContent;
import com.rymcu.vertical.entity.Tag; import com.rymcu.vertical.entity.Tag;
import com.rymcu.vertical.entity.User; import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.mapper.ArticleMapper; import com.rymcu.vertical.mapper.ArticleMapper;
import com.rymcu.vertical.service.ArticleService; import com.rymcu.vertical.service.ArticleService;
import com.rymcu.vertical.service.CommentService;
import com.rymcu.vertical.service.TagService; import com.rymcu.vertical.service.TagService;
import com.rymcu.vertical.service.UserService; import com.rymcu.vertical.service.UserService;
import com.rymcu.vertical.util.*; import com.rymcu.vertical.util.*;
@ -42,11 +45,16 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
private TagService tagService; private TagService tagService;
@Resource @Resource
private UserService userService; private UserService userService;
@Resource
private CommentService commentService;
@Value("${resource.domain}") @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 int MAX_PREVIEW = 200;
private static final String defaultStatus = "0";
@Override @Override
public List<ArticleDTO> findArticles(String searchText, String tag) { public List<ArticleDTO> findArticles(String searchText, String tag) {
@ -58,8 +66,11 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
} }
@Override @Override
public ArticleDTO findArticleDTOById(Integer id, int type) { public ArticleDTO findArticleDTOById(Integer id, Integer type) {
ArticleDTO articleDTO = articleMapper.selectArticleDTOById(id); ArticleDTO articleDTO = articleMapper.selectArticleDTOById(id,type);
if (articleDTO == null) {
return null;
}
articleDTO = genArticle(articleDTO,type); articleDTO = genArticle(articleDTO,type);
return articleDTO; return articleDTO;
} }
@ -67,6 +78,9 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
@Override @Override
public List<ArticleDTO> findArticlesByTopicUri(String name) { public List<ArticleDTO> findArticlesByTopicUri(String name) {
List<ArticleDTO> articleDTOS = articleMapper.selectArticlesByTopicUri(name); List<ArticleDTO> articleDTOS = articleMapper.selectArticlesByTopicUri(name);
articleDTOS.forEach(articleDTO -> {
genArticle(articleDTO,0);
});
return articleDTOS; return articleDTOS;
} }
@ -121,11 +135,12 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
newArticle.setArticleTags(articleTags); newArticle.setArticleTags(articleTags);
newArticle.setCreatedTime(new Date()); newArticle.setCreatedTime(new Date());
newArticle.setUpdatedTime(newArticle.getCreatedTime()); newArticle.setUpdatedTime(newArticle.getCreatedTime());
newArticle.setArticleStatus(article.getArticleStatus());
articleMapper.insertSelective(newArticle); articleMapper.insertSelective(newArticle);
newArticle.setArticlePermalink(domain + "/article/"+newArticle.getIdArticle());
newArticle.setArticleLink("/article/"+newArticle.getIdArticle());
articleMapper.insertArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml); articleMapper.insertArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml);
if (!ProjectConstant.ENV.equals(env) && defaultStatus.equals(newArticle.getArticleStatus())) {
BaiDuUtils.sendSEOData(newArticle.getArticlePermalink()); BaiDuUtils.sendSEOData(newArticle.getArticlePermalink());
}
} else { } else {
newArticle = articleMapper.selectByPrimaryKey(article.getIdArticle()); newArticle = articleMapper.selectByPrimaryKey(article.getIdArticle());
if(!user.getIdUser().equals(newArticle.getArticleAuthorId())){ if(!user.getIdUser().equals(newArticle.getArticleAuthorId())){
@ -142,16 +157,27 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
String articlePreviewContent = articleContentHtml.substring(0,length); String articlePreviewContent = articleContentHtml.substring(0,length);
newArticle.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); newArticle.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent));
} }
newArticle.setArticleStatus(article.getArticleStatus());
newArticle.setUpdatedTime(new Date()); newArticle.setUpdatedTime(new Date());
articleMapper.updateArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml); articleMapper.updateArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml);
if (!ProjectConstant.ENV.equals(env) && defaultStatus.equals(newArticle.getArticleStatus())) {
BaiDuUtils.updateSEOData(newArticle.getArticlePermalink()); BaiDuUtils.updateSEOData(newArticle.getArticlePermalink());
} }
}
if (notification) { if (notification && defaultStatus.equals(newArticle.getArticleStatus())) {
NotificationUtils.sendAnnouncement(newArticle.getIdArticle(), NotificationConstant.Article, newArticle.getArticleTitle()); NotificationUtils.sendAnnouncement(newArticle.getIdArticle(), NotificationConstant.Article, newArticle.getArticleTitle());
} }
tagService.saveTagArticle(newArticle); 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); articleMapper.updateByPrimaryKeySelective(newArticle);
map.put("id", newArticle.getIdArticle()); map.put("id", newArticle.getIdArticle());
@ -216,22 +242,41 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
articleMapper.updateArticleViewCount(article.getIdArticle(), articleViewCount); 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<ArticleDTO> findDrafts() throws BaseApiException {
User user = UserUtils.getWxCurrentUser();
List<ArticleDTO> list = articleMapper.selectDrafts(user.getIdUser());
list.forEach(article->{
genArticle(article,0);
});
return list;
}
private ArticleDTO genArticle(ArticleDTO article,Integer type) { private ArticleDTO genArticle(ArticleDTO article,Integer type) {
Author author = articleMapper.selectAuthor(article.getArticleAuthorId()); Author author = articleMapper.selectAuthor(article.getArticleAuthorId());
article.setArticleAuthor(author); article.setArticleAuthor(author);
article.setTimeAgo(Utils.getTimeAgo(article.getUpdatedTime())); article.setTimeAgo(Utils.getTimeAgo(article.getUpdatedTime()));
List<ArticleTagDTO> tags = articleMapper.selectTags(article.getIdArticle()); List<ArticleTagDTO> tags = articleMapper.selectTags(article.getIdArticle());
article.setTags(tags); 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()); article.setArticleContent(articleContent.getArticleContentHtml());
} else if(type == 2){ } else if (type == 2) {
ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle());
article.setArticleContent(articleContent.getArticleContent()); article.setArticleContent(articleContent.getArticleContent());
} }
if(StringUtils.isBlank(article.getArticlePreviewContent())){ if(StringUtils.isBlank(article.getArticlePreviewContent())){
ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle());
Integer length = articleContent.getArticleContentHtml().length(); Integer length = articleContent.getArticleContentHtml().length();
if(length > MAX_PREVIEW){ if(length > MAX_PREVIEW){
length = 200; length = 200;
@ -239,6 +284,8 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
String articlePreviewContent = articleContent.getArticleContentHtml().substring(0,length); String articlePreviewContent = articleContent.getArticleContentHtml().substring(0,length);
article.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); article.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent));
} }
List<CommentDTO> commentDTOList = commentService.getArticleComments(article.getIdArticle());
article.setArticleComments(commentDTOList);
return article; return article;
} }
} }

View File

@ -1,11 +1,16 @@
package com.rymcu.vertical.service.impl; package com.rymcu.vertical.service.impl;
import com.rymcu.vertical.core.service.AbstractService; 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.Article;
import com.rymcu.vertical.entity.Tag; import com.rymcu.vertical.entity.Tag;
import com.rymcu.vertical.entity.User; import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.mapper.ArticleMapper;
import com.rymcu.vertical.mapper.TagMapper; import com.rymcu.vertical.mapper.TagMapper;
import com.rymcu.vertical.service.TagService; import com.rymcu.vertical.service.TagService;
import com.rymcu.vertical.util.CacheUtils;
import com.rymcu.vertical.util.UserUtils; import com.rymcu.vertical.util.UserUtils;
import com.rymcu.vertical.web.api.exception.BaseApiException; import com.rymcu.vertical.web.api.exception.BaseApiException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -29,6 +34,10 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
@Resource @Resource
private TagMapper tagMapper; private TagMapper tagMapper;
@Resource
private ArticleMapper articleMapper;
@Resource
private RedisService redisService;
@Override @Override
@Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class }) @Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class })
@ -37,6 +46,7 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
String articleTags = article.getArticleTags(); String articleTags = article.getArticleTags();
if(StringUtils.isNotBlank(articleTags)){ if(StringUtils.isNotBlank(articleTags)){
String[] tags = articleTags.split(","); String[] tags = articleTags.split(",");
List<ArticleTagDTO> articleTagDTOList = articleMapper.selectTags(article.getIdArticle());
for (int i = 0; i < tags.length; i++) { for (int i = 0; i < tags.length; i++) {
boolean addTagArticle = false; boolean addTagArticle = false;
boolean addUserTag = false; boolean addUserTag = false;
@ -54,6 +64,12 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
addTagArticle = true; addTagArticle = true;
addUserTag = true; addUserTag = true;
} else { } else {
for(int m=0,n=articleTagDTOList.size()-1;m<n; m++) {
ArticleTagDTO articleTag = articleTagDTOList.get(m);
if (articleTag.getIdTag().equals(tag.getIdTag())) {
articleTagDTOList.remove(articleTag);
}
}
Integer count = tagMapper.selectCountTagArticleById(tag.getIdTag(),article.getIdArticle()); Integer count = tagMapper.selectCountTagArticleById(tag.getIdTag(),article.getIdArticle());
if(count == 0){ if(count == 0){
tag.setTagArticleCount(tag.getTagArticleCount() + 1); tag.setTagArticleCount(tag.getTagArticleCount() + 1);
@ -65,6 +81,9 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
addUserTag = true; addUserTag = true;
} }
} }
articleTagDTOList.forEach(articleTagDTO -> {
articleMapper.deleteUnusedArticleTag(articleTagDTO.getIdArticleTag());
});
if(addTagArticle){ if(addTagArticle){
tagMapper.insertTagArticle(tag.getIdTag(),article.getIdArticle()); tagMapper.insertTagArticle(tag.getIdTag(),article.getIdArticle());
} }
@ -125,4 +144,14 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
} }
return map; return map;
} }
@Override
public List<LabelModel> findTagLabels() {
List<LabelModel> list = (List<LabelModel>) CacheUtils.get("tags");
if (list == null) {
list = tagMapper.selectTagLabels();
CacheUtils.put("tags", list);
}
return list;
}
} }

View File

@ -77,7 +77,7 @@ public class BaiDuUtils {
},executor); },executor);
} }
public static void main(String agrs[]){ public static void main(String[] args){
sendSEOData("https://rymcu.com/article/31"); sendSEOData("https://rymcu.com");
} }
} }

View File

@ -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);
}
}