refactor: 简化代码
This commit is contained in:
@@ -304,7 +304,7 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
public PageData<ArticleModel> retrievePageForOpen(int count, int page) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Article> articleList = articleMapper.findAllByOpen(true);
|
||||
PageData<ArticleModel> pageData = new PageData<>(new PageInfo<Article>(articleList));
|
||||
PageData<ArticleModel> pageData = new PageData<>(new PageInfo<>(articleList));
|
||||
|
||||
List<ArticleModel> articleModelList = articleList
|
||||
.stream()
|
||||
@@ -323,9 +323,7 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Article> open = articleMapper.findAllByCategoryIdAndOpen(category.getId());
|
||||
|
||||
List<ArticleModel> modelList = new ArrayList<>();
|
||||
|
||||
modelList = open.stream()
|
||||
List<ArticleModel> modelList = open.stream()
|
||||
.map(article -> ModalTrans.article(article, true))
|
||||
.peek(articleModel -> {
|
||||
articleModel.setNextArticle(null);
|
||||
|
||||
@@ -17,8 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
@@ -68,22 +68,25 @@ public class CategoryServiceImpl implements CategoryService {
|
||||
public PageData<CategoryModel> retrievePage(int page, int count) {
|
||||
PageHelper.startPage(page, count);
|
||||
List<Category> all = categoryMapper.findAll();
|
||||
List<CategoryModel> modelList = new ArrayList<>();
|
||||
all.forEach(e -> {
|
||||
CategoryModel model = ModalTrans.category(e);
|
||||
List<Article> allByCategoryId = articleMapper.findAllByCategoryId(e.getId());
|
||||
List<ArticleModel> articleModelList = new ArrayList<>();
|
||||
allByCategoryId.forEach(article -> {
|
||||
ArticleModel articleModel = ModalTrans.article(article, true);
|
||||
// 遍历没一个category
|
||||
List<CategoryModel> modelList = all
|
||||
.stream()
|
||||
.map(ModalTrans::category)
|
||||
.peek(categoryModel -> {
|
||||
// 根据category去查article,并赋值给categoryModel
|
||||
List<Article> allByCategoryId = articleMapper.findAllByCategoryId(categoryModel.getId());
|
||||
List<ArticleModel> articleModelList = allByCategoryId
|
||||
.stream()
|
||||
.map(article -> ModalTrans.article(article, true))
|
||||
.peek(articleModel -> {
|
||||
// 去除不必要的字段
|
||||
articleModel.setPreArticle(null);
|
||||
articleModel.setNextArticle(null);
|
||||
articleModel.setTags(null);
|
||||
articleModelList.add(articleModel);
|
||||
});
|
||||
model.setArticles(articleModelList);
|
||||
modelList.add(model);
|
||||
});
|
||||
|
||||
return new PageData<CategoryModel>(new PageInfo<Category>(all), modelList);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
categoryModel.setArticles(articleModelList);
|
||||
}).collect(Collectors.toList());
|
||||
return new PageData<>(new PageInfo<>(all), modelList);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.springframework.stereotype.Service;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
@@ -107,14 +108,11 @@ public class CommentServiceImpl implements CommentService {
|
||||
@Override
|
||||
public List<CommentModel> retrievePageByPid(long pid) {
|
||||
List<Comment> allByPagePath = commentMapper.findAllByPid(pid);
|
||||
List<CommentModel> commentModels = new ArrayList<>();
|
||||
allByPagePath.forEach(comment -> {
|
||||
if (comment.getStatus() != CommentStatusEnum.DELETED.getCode()) {
|
||||
commentModels.add(ModalTrans.comment(comment));
|
||||
}
|
||||
});
|
||||
|
||||
return commentModels;
|
||||
return allByPagePath
|
||||
.stream()
|
||||
.filter(comment -> comment.getStatus() != CommentStatusEnum.DELETED.getCode())
|
||||
.map(ModalTrans::comment)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -145,14 +143,11 @@ public class CommentServiceImpl implements CommentService {
|
||||
|
||||
private PageData<CommentModel> pageTrans(List<Comment> commentList, boolean noResponseList) {
|
||||
PageInfo<Comment> p = PageInfo.of(commentList);
|
||||
List<CommentModel> modelList = new ArrayList<>();
|
||||
commentList.forEach(l -> {
|
||||
CommentModel model = ModalTrans.comment(l);
|
||||
if (!noResponseList) {
|
||||
model.setRespComment(this.retrievePageByPid(model.getId()));
|
||||
}
|
||||
modelList.add(model);
|
||||
});
|
||||
return new PageData<CommentModel>(p, modelList);
|
||||
List<CommentModel> modelList = commentList
|
||||
.stream()
|
||||
.map(ModalTrans::comment)
|
||||
.peek(commentModel -> commentModel.setRespComment(this.retrievePageByPid(commentModel.getId())))
|
||||
.collect(Collectors.toList());
|
||||
return new PageData<>(p, modelList);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
@@ -81,20 +82,22 @@ public class TagServiceImpl implements TagService {
|
||||
List<Tag> tagList = tagMapper.findAll();
|
||||
List<TagModel> modelList = new ArrayList<>();
|
||||
tagList.forEach(tag -> modelList.add(ModalTrans.tag(tag)));
|
||||
return new PageData<TagModel>(new PageInfo<Tag>(tagList), modelList);
|
||||
return new PageData<>(new PageInfo<>(tagList), modelList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TagModel> findAll() {
|
||||
List<TagModel> list = new ArrayList<>();
|
||||
tagMapper.findAll().forEach(e -> {
|
||||
TagModel model = ModalTrans.tag(e);
|
||||
List<ArticleTag> articleByTagAndOpen = articleTagMapper.findArticleByTagAndOpen(e.getId());
|
||||
List<ArticleModel> articleModelList = new ArrayList<>();
|
||||
articleByTagAndOpen.forEach(articleTag -> articleModelList.add(ModalTrans.article(articleTag.getArticle(), true)));
|
||||
model.setArticles(articleModelList);
|
||||
list.add(model);
|
||||
});
|
||||
return list;
|
||||
return tagMapper.findAll().stream()
|
||||
.map(ModalTrans::tag)
|
||||
.peek(tagModel -> {
|
||||
List<ArticleTag> articleByTagAndOpen = articleTagMapper.findArticleByTagAndOpen(tagModel.getId());
|
||||
tagModel.setArticles(
|
||||
articleByTagAndOpen
|
||||
.stream()
|
||||
.map(articleTag -> ModalTrans.article(articleTag.getArticle(), true))
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user