🎨 优化代码结构
This commit is contained in:
parent
dff2145c0f
commit
bb9c20997f
@ -15,6 +15,7 @@ import com.rymcu.forest.service.TagService;
|
|||||||
import com.rymcu.forest.service.UserService;
|
import com.rymcu.forest.service.UserService;
|
||||||
import com.rymcu.forest.util.*;
|
import com.rymcu.forest.util.*;
|
||||||
import com.rymcu.forest.web.api.exception.BaseApiException;
|
import com.rymcu.forest.web.api.exception.BaseApiException;
|
||||||
|
import com.rymcu.forest.web.api.exception.ErrorCode;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.commons.text.StringEscapeUtils;
|
import org.apache.commons.text.StringEscapeUtils;
|
||||||
@ -46,24 +47,21 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
|
|
||||||
@Value("${resource.domain}")
|
@Value("${resource.domain}")
|
||||||
private 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";
|
private static final String DEFAULT_STATUS = "0";
|
||||||
private static final String defaultTopicUri = "news";
|
private static final String DEFAULT_TOPIC_URI = "news";
|
||||||
|
private static final int ADMIN_ROLE_WEIGHTS = 2;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ArticleDTO> findArticles(ArticleSearchDTO searchDTO) {
|
public List<ArticleDTO> findArticles(ArticleSearchDTO searchDTO) {
|
||||||
List<ArticleDTO> list;
|
List<ArticleDTO> list;
|
||||||
if (StringUtils.isNotBlank(searchDTO.getTopicUri()) && !defaultTopicUri.equals(searchDTO.getTopicUri())) {
|
if (StringUtils.isNotBlank(searchDTO.getTopicUri()) && !DEFAULT_TOPIC_URI.equals(searchDTO.getTopicUri())) {
|
||||||
list = articleMapper.selectArticlesByTopicUri(searchDTO.getTopicUri());
|
list = articleMapper.selectArticlesByTopicUri(searchDTO.getTopicUri());
|
||||||
} else {
|
} else {
|
||||||
list = articleMapper.selectArticles(searchDTO.getSearchText(), searchDTO.getTag(), searchDTO.getTopicUri());
|
list = articleMapper.selectArticles(searchDTO.getSearchText(), searchDTO.getTag(), searchDTO.getTopicUri());
|
||||||
}
|
}
|
||||||
list.forEach(article -> {
|
list.forEach(articleDTO -> genArticle(articleDTO,0));
|
||||||
genArticle(article, 0);
|
|
||||||
});
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,31 +71,26 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
if (articleDTO == null) {
|
if (articleDTO == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
articleDTO = genArticle(articleDTO, type);
|
genArticle(articleDTO, type);
|
||||||
return articleDTO;
|
return articleDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ArticleDTO> findArticlesByTopicUri(String name) {
|
public List<ArticleDTO> findArticlesByTopicUri(String name) {
|
||||||
List<ArticleDTO> articleDTOS = articleMapper.selectArticlesByTopicUri(name);
|
List<ArticleDTO> list = articleMapper.selectArticlesByTopicUri(name);
|
||||||
articleDTOS.forEach(articleDTO -> {
|
list.forEach(articleDTO -> genArticle(articleDTO,0));
|
||||||
genArticle(articleDTO, 0);
|
return list;
|
||||||
});
|
|
||||||
return articleDTOS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ArticleDTO> findArticlesByTagName(String name) {
|
public List<ArticleDTO> findArticlesByTagName(String name) {
|
||||||
List<ArticleDTO> articleDTOS = articleMapper.selectArticlesByTagName(name);
|
return articleMapper.selectArticlesByTagName(name);
|
||||||
return articleDTOS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ArticleDTO> findUserArticlesByIdUser(Integer idUser) {
|
public List<ArticleDTO> findUserArticlesByIdUser(Integer idUser) {
|
||||||
List<ArticleDTO> list = articleMapper.selectUserArticles(idUser);
|
List<ArticleDTO> list = articleMapper.selectUserArticles(idUser);
|
||||||
list.forEach(article -> {
|
list.forEach(articleDTO -> genArticle(articleDTO,0));
|
||||||
genArticle(article, 0);
|
|
||||||
});
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,11 +112,14 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
String articleContent = article.getArticleContent();
|
String articleContent = article.getArticleContent();
|
||||||
String articleContentHtml = article.getArticleContentHtml();
|
String articleContentHtml = article.getArticleContentHtml();
|
||||||
User user = UserUtils.getCurrentUserByToken();
|
User user = UserUtils.getCurrentUserByToken();
|
||||||
|
if (Objects.isNull(user)) {
|
||||||
|
throw new BaseApiException(ErrorCode.INVALID_TOKEN);
|
||||||
|
}
|
||||||
String reservedTag = checkTags(articleTags);
|
String reservedTag = checkTags(articleTags);
|
||||||
boolean notification = false;
|
boolean notification = false;
|
||||||
if (StringUtils.isNotBlank(reservedTag)) {
|
if (StringUtils.isNotBlank(reservedTag)) {
|
||||||
Integer roleWeights = userService.findRoleWeightsByUser(user.getIdUser());
|
Integer roleWeights = userService.findRoleWeightsByUser(user.getIdUser());
|
||||||
if (roleWeights > 2) {
|
if (roleWeights > ADMIN_ROLE_WEIGHTS) {
|
||||||
map.put("message", StringEscapeUtils.unescapeJava(reservedTag) + "标签为系统保留标签!");
|
map.put("message", StringEscapeUtils.unescapeJava(reservedTag) + "标签为系统保留标签!");
|
||||||
return map;
|
return map;
|
||||||
} else {
|
} else {
|
||||||
@ -144,10 +140,8 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
} else {
|
} else {
|
||||||
newArticle = articleMapper.selectByPrimaryKey(article.getIdArticle());
|
newArticle = articleMapper.selectByPrimaryKey(article.getIdArticle());
|
||||||
// 如果文章之前状态为草稿则应视为新发布文章
|
// 如果文章之前状态为草稿则应视为新发布文章
|
||||||
if (defaultStatus.equals(newArticle.getArticleStatus())) {
|
if (DEFAULT_STATUS.equals(newArticle.getArticleStatus())) {
|
||||||
isUpdate = true;
|
isUpdate = true;
|
||||||
} else {
|
|
||||||
isUpdate = false;
|
|
||||||
}
|
}
|
||||||
if (!user.getIdUser().equals(newArticle.getArticleAuthorId())) {
|
if (!user.getIdUser().equals(newArticle.getArticleAuthorId())) {
|
||||||
map.put("message", "非法访问!");
|
map.put("message", "非法访问!");
|
||||||
@ -161,13 +155,13 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 发送相关通知
|
// 发送相关通知
|
||||||
if (defaultStatus.equals(newArticle.getArticleStatus())) {
|
if (DEFAULT_STATUS.equals(newArticle.getArticleStatus())) {
|
||||||
// 发送系统通知
|
// 发送系统通知
|
||||||
if (notification) {
|
if (notification) {
|
||||||
NotificationUtils.sendAnnouncement(newArticle.getIdArticle(), NotificationConstant.Article, newArticle.getArticleTitle());
|
NotificationUtils.sendAnnouncement(newArticle.getIdArticle(), NotificationConstant.Article, newArticle.getArticleTitle());
|
||||||
} else {
|
} else {
|
||||||
// 发送关注通知
|
// 发送关注通知
|
||||||
StringBuffer dataSummary = new StringBuffer();
|
StringBuilder dataSummary = new StringBuilder();
|
||||||
if (isUpdate) {
|
if (isUpdate) {
|
||||||
dataSummary.append(user.getNickname()).append("更新了文章: ").append(newArticle.getArticleTitle());
|
dataSummary.append(user.getNickname()).append("更新了文章: ").append(newArticle.getArticleTitle());
|
||||||
NotificationUtils.sendArticlePush(newArticle.getIdArticle(), NotificationConstant.UpdateArticle, dataSummary.toString(), newArticle.getArticleAuthorId());
|
NotificationUtils.sendArticlePush(newArticle.getIdArticle(), NotificationConstant.UpdateArticle, dataSummary.toString(), newArticle.getArticleAuthorId());
|
||||||
@ -176,10 +170,7 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
NotificationUtils.sendArticlePush(newArticle.getIdArticle(), NotificationConstant.PostArticle, dataSummary.toString(), newArticle.getArticleAuthorId());
|
NotificationUtils.sendArticlePush(newArticle.getIdArticle(), NotificationConstant.PostArticle, dataSummary.toString(), newArticle.getArticleAuthorId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// 草稿不更新索引
|
// 草稿不更新索引
|
||||||
if ("0".equals(article.getArticleStatus())) {
|
|
||||||
System.out.println("开始增加索引");
|
|
||||||
if (isUpdate) {
|
if (isUpdate) {
|
||||||
log.info("更新文章索引,id={}", newArticle.getIdArticle());
|
log.info("更新文章索引,id={}", newArticle.getIdArticle());
|
||||||
luceneService.updateArticle(newArticle.getIdArticle().toString());
|
luceneService.updateArticle(newArticle.getIdArticle().toString());
|
||||||
@ -187,16 +178,15 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
log.info("写入文章索引,id={}", newArticle.getIdArticle());
|
log.info("写入文章索引,id={}", newArticle.getIdArticle());
|
||||||
luceneService.writeArticle(newArticle.getIdArticle().toString());
|
luceneService.writeArticle(newArticle.getIdArticle().toString());
|
||||||
}
|
}
|
||||||
}
|
// 更新文章链接
|
||||||
tagService.saveTagArticle(newArticle, articleContentHtml);
|
|
||||||
|
|
||||||
if (defaultStatus.equals(newArticle.getArticleStatus())) {
|
|
||||||
newArticle.setArticlePermalink(domain + "/article/" + newArticle.getIdArticle());
|
newArticle.setArticlePermalink(domain + "/article/" + newArticle.getIdArticle());
|
||||||
newArticle.setArticleLink("/article/" + newArticle.getIdArticle());
|
newArticle.setArticleLink("/article/" + newArticle.getIdArticle());
|
||||||
} else {
|
} else {
|
||||||
|
// 更新文章链接
|
||||||
newArticle.setArticlePermalink(domain + "/draft/" + newArticle.getIdArticle());
|
newArticle.setArticlePermalink(domain + "/draft/" + newArticle.getIdArticle());
|
||||||
newArticle.setArticleLink("/draft/" + newArticle.getIdArticle());
|
newArticle.setArticleLink("/draft/" + newArticle.getIdArticle());
|
||||||
}
|
}
|
||||||
|
tagService.saveTagArticle(newArticle, articleContentHtml);
|
||||||
|
|
||||||
if (StringUtils.isNotBlank(articleContentHtml)) {
|
if (StringUtils.isNotBlank(articleContentHtml)) {
|
||||||
String previewContent = Html2TextUtil.getContent(articleContentHtml);
|
String previewContent = Html2TextUtil.getContent(articleContentHtml);
|
||||||
@ -249,15 +239,18 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
Map<String, String> map = new HashMap(1);
|
Map<String, String> map = new HashMap(1);
|
||||||
// 鉴权
|
// 鉴权
|
||||||
User user = UserUtils.getCurrentUserByToken();
|
User user = UserUtils.getCurrentUserByToken();
|
||||||
|
if (Objects.isNull(user)) {
|
||||||
|
throw new BaseApiException(ErrorCode.INVALID_TOKEN);
|
||||||
|
}
|
||||||
Integer roleWeights = userService.findRoleWeightsByUser(user.getIdUser());
|
Integer roleWeights = userService.findRoleWeightsByUser(user.getIdUser());
|
||||||
if (roleWeights > 2) {
|
if (roleWeights > ADMIN_ROLE_WEIGHTS) {
|
||||||
Article article = articleMapper.selectByPrimaryKey(id);
|
Article article = articleMapper.selectByPrimaryKey(id);
|
||||||
if (!user.getIdUser().equals(article.getArticleAuthorId())) {
|
if (!user.getIdUser().equals(article.getArticleAuthorId())) {
|
||||||
map.put("message", "非法访问!");
|
map.put("message", "非法访问!");
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Integer result;
|
int result;
|
||||||
// 判断是否有评论
|
// 判断是否有评论
|
||||||
boolean isHavComment = articleMapper.existsCommentWithPrimaryKey(id);
|
boolean isHavComment = articleMapper.existsCommentWithPrimaryKey(id);
|
||||||
if (isHavComment) {
|
if (isHavComment) {
|
||||||
@ -296,6 +289,9 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
public Map share(Integer id) throws BaseApiException {
|
public Map share(Integer id) throws BaseApiException {
|
||||||
Article article = articleMapper.selectByPrimaryKey(id);
|
Article article = articleMapper.selectByPrimaryKey(id);
|
||||||
User user = UserUtils.getCurrentUserByToken();
|
User user = UserUtils.getCurrentUserByToken();
|
||||||
|
if (Objects.isNull(user)) {
|
||||||
|
throw new BaseApiException(ErrorCode.INVALID_TOKEN);
|
||||||
|
}
|
||||||
StringBuilder shareUrl = new StringBuilder(article.getArticlePermalink());
|
StringBuilder shareUrl = new StringBuilder(article.getArticlePermalink());
|
||||||
shareUrl.append("?s=").append(user.getNickname());
|
shareUrl.append("?s=").append(user.getNickname());
|
||||||
Map map = new HashMap(1);
|
Map map = new HashMap(1);
|
||||||
@ -306,28 +302,25 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
@Override
|
@Override
|
||||||
public List<ArticleDTO> findDrafts() throws BaseApiException {
|
public List<ArticleDTO> findDrafts() throws BaseApiException {
|
||||||
User user = UserUtils.getCurrentUserByToken();
|
User user = UserUtils.getCurrentUserByToken();
|
||||||
|
if (Objects.isNull(user)) {
|
||||||
|
throw new BaseApiException(ErrorCode.INVALID_TOKEN);
|
||||||
|
}
|
||||||
List<ArticleDTO> list = articleMapper.selectDrafts(user.getIdUser());
|
List<ArticleDTO> list = articleMapper.selectDrafts(user.getIdUser());
|
||||||
list.forEach(article -> {
|
list.forEach(articleDTO -> genArticle(articleDTO,0));
|
||||||
genArticle(article, 0);
|
|
||||||
});
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ArticleDTO> findArticlesByIdPortfolio(Integer idPortfolio) {
|
public List<ArticleDTO> findArticlesByIdPortfolio(Integer idPortfolio) {
|
||||||
List<ArticleDTO> list = articleMapper.selectArticlesByIdPortfolio(idPortfolio);
|
List<ArticleDTO> list = articleMapper.selectArticlesByIdPortfolio(idPortfolio);
|
||||||
list.forEach(article -> {
|
list.forEach(articleDTO -> genArticle(articleDTO,0));
|
||||||
genArticle(article, 0);
|
|
||||||
});
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ArticleDTO> selectUnbindArticles(Integer idPortfolio, String searchText, Integer idUser) {
|
public List<ArticleDTO> selectUnbindArticles(Integer idPortfolio, String searchText, Integer idUser) {
|
||||||
List<ArticleDTO> list = articleMapper.selectUnbindArticlesByIdPortfolio(idPortfolio, searchText, idUser);
|
List<ArticleDTO> list = articleMapper.selectUnbindArticlesByIdPortfolio(idPortfolio, searchText, idUser);
|
||||||
list.forEach(article -> {
|
list.forEach(articleDTO -> genArticle(articleDTO,0));
|
||||||
genArticle(article, 0);
|
|
||||||
});
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -364,9 +357,7 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
@Override
|
@Override
|
||||||
public List<ArticleDTO> findAnnouncements() {
|
public List<ArticleDTO> findAnnouncements() {
|
||||||
List<ArticleDTO> list = articleMapper.selectAnnouncements();
|
List<ArticleDTO> list = articleMapper.selectAnnouncements();
|
||||||
list.forEach(article -> {
|
list.forEach(articleDTO -> genArticle(articleDTO,0));
|
||||||
genArticle(article, 0);
|
|
||||||
});
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -385,7 +376,7 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
|
|||||||
article.setArticleContent(articleContent.getArticleContentHtml());
|
article.setArticleContent(articleContent.getArticleContentHtml());
|
||||||
// 获取所属作品集列表数据
|
// 获取所属作品集列表数据
|
||||||
List<PortfolioArticleDTO> portfolioArticleDTOList = articleMapper.selectPortfolioArticles(article.getIdArticle());
|
List<PortfolioArticleDTO> portfolioArticleDTOList = articleMapper.selectPortfolioArticles(article.getIdArticle());
|
||||||
portfolioArticleDTOList.forEach(portfolioArticleDTO -> genPortfolioArticles(portfolioArticleDTO));
|
portfolioArticleDTOList.forEach(this::genPortfolioArticles);
|
||||||
article.setPortfolios(portfolioArticleDTOList);
|
article.setPortfolios(portfolioArticleDTOList);
|
||||||
} else if (type.equals(articleEdit)) {
|
} else if (type.equals(articleEdit)) {
|
||||||
article.setArticleContent(articleContent.getArticleContent());
|
article.setArticleContent(articleContent.getArticleContent());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user