|
|
@@ -1,23 +1,11 @@
|
|
|
package cn.reghao.tnb.search.app.blog.service;
|
|
|
|
|
|
import cn.reghao.tnb.search.app.blog.db.repository.ArticleRepository;
|
|
|
-import cn.reghao.tnb.search.app.blog.db.repository.ArticleTagRepository;
|
|
|
-import cn.reghao.tnb.search.app.blog.db.repository.CategoryRepository;
|
|
|
import cn.reghao.tnb.search.app.blog.model.dto.QuestionUpdateDto;
|
|
|
import cn.reghao.tnb.search.app.blog.model.po.Article;
|
|
|
-import cn.reghao.tnb.search.app.blog.model.po.ArticleTag;
|
|
|
-import cn.reghao.tnb.search.app.blog.model.po.Category;
|
|
|
-import cn.reghao.tnb.search.app.blog.model.vo.QuestionCount;
|
|
|
-import cn.reghao.tnb.search.app.blog.model.vo.QuestionView;
|
|
|
-import cn.reghao.tnb.search.app.blog.util.MarkdownUtil;
|
|
|
-import jakarta.persistence.criteria.Predicate;
|
|
|
-import org.springframework.data.domain.*;
|
|
|
-import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -26,14 +14,9 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
|
public class QuestionService {
|
|
|
private final ArticleRepository articleRepository;
|
|
|
- private final CategoryRepository categoryRepository;
|
|
|
- private final ArticleTagRepository articleTagRepository;
|
|
|
|
|
|
- public QuestionService(ArticleRepository articleRepository, CategoryRepository categoryRepository,
|
|
|
- ArticleTagRepository articleTagRepository) {
|
|
|
+ public QuestionService(ArticleRepository articleRepository) {
|
|
|
this.articleRepository = articleRepository;
|
|
|
- this.categoryRepository = categoryRepository;
|
|
|
- this.articleTagRepository = articleTagRepository;
|
|
|
}
|
|
|
|
|
|
public void updateQuestion(QuestionUpdateDto questionUpdateDto) {
|
|
|
@@ -72,59 +55,4 @@ public class QuestionService {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
- public Page<QuestionView> findQuestionByPage(int pageSize, int pageNumber, int categoryId) {
|
|
|
- Specification<Article> specification = (root, query, cb) -> {
|
|
|
- List<Predicate> predicates = new ArrayList<>();
|
|
|
- if (categoryId != 0) {
|
|
|
- predicates.add(cb.equal(root.get("categoryId"), categoryId));
|
|
|
- }
|
|
|
-
|
|
|
- return cb.and(predicates.toArray(new Predicate[0]));
|
|
|
- };
|
|
|
-
|
|
|
- Pageable pageable = PageRequest.of(pageNumber-1, pageSize, Sort.by(Sort.Direction.DESC, "weight"));
|
|
|
- Page<Article> page = articleRepository.findAll(specification, pageable);
|
|
|
- List<QuestionView> list1 = page.stream().map(question -> {
|
|
|
- Category category = categoryRepository.findById(question.getCategoryId()).orElse(null);
|
|
|
- String categoryName = category.getName();
|
|
|
- String tagName = getTagName(question.getArticleId());
|
|
|
- return new QuestionView(question, categoryName, tagName);
|
|
|
- }).collect(Collectors.toList());
|
|
|
-
|
|
|
- return new PageImpl<>(list1, pageable, page.getTotalElements());
|
|
|
- }
|
|
|
-
|
|
|
- private String getTagName(String questionId) {
|
|
|
- String tagName = "";
|
|
|
- List<ArticleTag> articleTags = articleTagRepository.findByArticleId(questionId);
|
|
|
- if (!articleTags.isEmpty()) {
|
|
|
- Category category1 = categoryRepository.findById(articleTags.get(0).getTagId()).orElse(null);
|
|
|
- tagName = category1 != null ? category1.getName() : "";
|
|
|
- }
|
|
|
-
|
|
|
- return tagName;
|
|
|
- }
|
|
|
-
|
|
|
- public List<Category> getCategories() {
|
|
|
- List<QuestionCount> questionCounts = articleRepository.findQuestionCountByGroup();
|
|
|
- List<Integer> ids = questionCounts.stream().map(QuestionCount::getCategoryId).collect(Collectors.toList());
|
|
|
- return categoryRepository.findAllById(ids);
|
|
|
- }
|
|
|
-
|
|
|
- public Article findByQuestionId(String questionId) {
|
|
|
- return articleRepository.findByArticleId(questionId);
|
|
|
- }
|
|
|
-
|
|
|
- public QuestionView getQuestionView(String questionId) {
|
|
|
- Article question = articleRepository.findByArticleId(questionId);
|
|
|
- Category category = categoryRepository.findById(question.getCategoryId()).orElse(null);
|
|
|
- String categoryName = category.getName();
|
|
|
- String tagName = getTagName(question.getArticleId());
|
|
|
-
|
|
|
- QuestionView questionView = new QuestionView(question, categoryName, tagName);
|
|
|
- String html = MarkdownUtil.getHtml(question.getContent());
|
|
|
- questionView.setContent(html);
|
|
|
- return questionView;
|
|
|
- }
|
|
|
}
|