Przeglądaj źródła

search-service 更新 cn.reghao.bnt.web.blog 包

reghao 3 miesięcy temu
rodzic
commit
bb173ffc86

+ 13 - 3
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/db/repository/ArticleRepository.java

@@ -1,18 +1,28 @@
 package cn.reghao.tnb.search.app.blog.db.repository;
 
 import cn.reghao.tnb.search.app.blog.model.po.Article;
+import cn.reghao.tnb.search.app.blog.model.vo.QuestionCount;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.List;
 
 /**
  * @author reghao
  * @date 2024-01-28 13:17:47
  */
 public interface ArticleRepository extends JpaRepository<Article, Integer>, JpaSpecificationExecutor<Article> {
-    int countByCategoryId(int categoryId);
-    Page<Article> findByPublishedIsTrueAndCategoryId(int categoryId, Pageable pageable);
-    Page<Article> findByPublishedIsTrue(Pageable pageable);
+    int countByCategoryIdAndPostType(int categoryId, int postType);
+    Page<Article> findByPublishedIsTrueAndCategoryIdAndPostType(int categoryId, int postType, Pageable pageable);
+    Page<Article> findByPublishedIsTrueAndPostType(int postType, Pageable pageable);
     Article findByArticleId(String articleId);
+    @Query("select new cn.reghao.tnb.search.app.blog.model.vo.QuestionCount(article.categoryId, count(article))\n" +
+            "from Article article\n" +
+            "where article.postType=2\n" +
+            "group by article.categoryId \n" +
+            "order by count(article) desc")
+    List<QuestionCount> findQuestionCountByGroup();
 }

+ 1 - 2
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/db/repository/QuestionRepository.java

@@ -14,10 +14,9 @@ import java.util.List;
  * @author reghao
  * @date 2024-01-28 13:17:47
  */
+@Deprecated
 public interface QuestionRepository extends JpaRepository<Question, Integer>, JpaSpecificationExecutor<Question> {
-    Page<Question> findByCategoryId(int categoryId, Pageable pageable);
     Question findByQuestionId(String questionId);
-    int countByCategoryId(int categoryId);
 
     @Query("select new cn.reghao.tnb.search.app.blog.model.vo.QuestionCount(question.categoryId, count(question))\n" +
             "from Question question\n" +

+ 23 - 0
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/model/ArticleType.java

@@ -0,0 +1,23 @@
+package cn.reghao.tnb.search.app.blog.model;
+
+/**
+ * @author reghao
+ * @date 2025-12-04 16:31:14
+ */
+public enum ArticleType {
+    Article(1),
+    Question(2);
+
+    private final int value;
+    ArticleType(int value) {
+        this.value = value;
+    }
+
+    public String getName() {
+        return this.name();
+    }
+
+    public Integer getValue() {
+        return value;
+    }
+}

+ 1 - 1
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/model/dto/ArticleDto.java

@@ -28,5 +28,5 @@ public class ArticleDto {
     @NotNull
     private Boolean published;
     @NotNull
-    private Integer type;
+    private Integer postType;
 }

+ 16 - 0
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/model/po/Article.java

@@ -50,6 +50,7 @@ public class Article extends BaseEntity {
     private LocalDateTime publishAt;
     private transient List<String> tagIds;
     private Integer weight;
+    private Integer postType;
     private Long owner;
 
     public Article(String articleId, ArticleDto articleDto, String excerpt) {
@@ -62,6 +63,21 @@ public class Article extends BaseEntity {
         this.published = articleDto.getPublished();
         this.publishAt = LocalDateTime.now();
         this.weight = 0;
+        this.postType = articleDto.getPostType();
+        this.owner = UserContext.getUserId();
+    }
+
+    public Article(Question question) {
+        this.articleId = question.getQuestionId();
+        this.title = question.getTitle();
+        this.excerpt = "";
+        this.content = question.getContent();
+        this.editor = "markdown";
+        this.categoryId = question.getCategoryId();
+        this.published = true;
+        this.publishAt = question.getPublishAt();
+        this.weight = question.getWeight();
+        this.postType = 2;
         this.owner = UserContext.getUserId();
     }
 }

+ 1 - 0
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/model/po/Question.java

@@ -27,6 +27,7 @@ import java.time.LocalDateTime;
 @Indexed(index = "question")
 @Entity
 @Table(name = "blog_question")
+@Deprecated
 public class Question extends BaseEntity {
     @KeywordField(projectable = Projectable.YES)
     @Column(nullable = false, unique = true)

+ 3 - 2
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/model/vo/QuestionView.java

@@ -1,5 +1,6 @@
 package cn.reghao.tnb.search.app.blog.model.vo;
 
+import cn.reghao.tnb.search.app.blog.model.po.Article;
 import cn.reghao.tnb.search.app.blog.model.po.Question;
 import lombok.Getter;
 import lombok.Setter;
@@ -19,9 +20,9 @@ public class QuestionView {
     private String content;
     private int weight;
 
-    public QuestionView(Question question, String category, String tag) {
+    public QuestionView(Article question, String category, String tag) {
         this.id = question.getId();
-        this.questionId = question.getQuestionId();
+        this.questionId = question.getArticleId();
         this.title = question.getTitle();
         this.category = category;
         this.tag = tag;

+ 4 - 2
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/service/ArticleQuery.java

@@ -4,6 +4,7 @@ import cn.reghao.tnb.search.app.blog.db.repository.AboutViewRepository;
 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.ArticleType;
 import cn.reghao.tnb.search.app.blog.model.CategoryType;
 import cn.reghao.tnb.search.app.blog.model.po.AboutView;
 import cn.reghao.tnb.search.app.blog.model.po.Article;
@@ -174,7 +175,7 @@ public class ArticleQuery {
     public Page<UserArticle> findUserArticleByPage(int pageSize, int pageNumber) {
         Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
         Pageable pageable = PageRequest.of(pageNumber-1, pageSize, sort);
-        Page<Article> page = articleRepository.findByPublishedIsTrue(pageable);
+        Page<Article> page = articleRepository.findByPublishedIsTrueAndPostType(ArticleType.Article.getValue(), pageable);
 
         List<UserArticle> list1 = page.getContent().stream().map(article -> {
             int categoryId1 = article.getCategoryId();
@@ -199,7 +200,8 @@ public class ArticleQuery {
         Pageable pageable = PageRequest.of(pageNumber-1, pageSize, sort);
         if (type == CategoryType.Category.getValue()) {
             int categoryId = category.getId();
-            Page<Article> page = articleRepository.findByPublishedIsTrueAndCategoryId(categoryId, pageable);
+            int postType = ArticleType.Article.getValue();
+            Page<Article> page = articleRepository.findByPublishedIsTrueAndCategoryIdAndPostType(categoryId, postType, pageable);
             total = page.getTotalElements();
             list1 = page.getContent().stream()
                     .map(article -> {

+ 1 - 8
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/service/ArticleService.java

@@ -31,23 +31,16 @@ public class ArticleService {
     private final ArticleTagRepository articleTagRepository;
     private final CategoryRepository categoryRepository;
     private final IdGenerator idGenerator;
-    private final QuestionService questionService;
 
     public ArticleService(ArticleRepository articleRepository, ArticleTagRepository articleTagRepository,
-                          CategoryRepository categoryRepository, QuestionService questionService) {
+                          CategoryRepository categoryRepository) {
         this.articleRepository = articleRepository;
         this.articleTagRepository = articleTagRepository;
         this.categoryRepository = categoryRepository;
         this.idGenerator = new IdGenerator("article-id");
-        this.questionService = questionService;
     }
 
     public Result addArticle(ArticleDto articleDto) {
-        int type = articleDto.getType();
-        if (type == 2) {
-            return questionService.addQuestion(articleDto);
-        }
-
         int categoryId = articleDto.getCategoryId();
         Category category = categoryRepository.findById(categoryId).orElse(null);
         if (category == null) {

+ 6 - 6
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/service/CategoryService.java

@@ -3,7 +3,7 @@ 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.db.repository.QuestionRepository;
+import cn.reghao.tnb.search.app.blog.model.ArticleType;
 import cn.reghao.tnb.search.app.blog.model.CategoryType;
 import cn.reghao.tnb.search.app.blog.model.po.Category;
 import cn.reghao.tnb.search.app.blog.model.vo.CategoryCount;
@@ -25,14 +25,12 @@ public class CategoryService {
     private final CategoryRepository categoryRepository;
     private final ArticleTagRepository articleTagRepository;
     private final ArticleRepository articleRepository;
-    private final QuestionRepository questionRepository;
 
     public CategoryService(CategoryRepository categoryRepository, ArticleTagRepository articleTagRepository,
-                           ArticleRepository articleRepository, QuestionRepository questionRepository) {
+                           ArticleRepository articleRepository) {
         this.categoryRepository = categoryRepository;
         this.articleTagRepository = articleTagRepository;
         this.articleRepository = articleRepository;
-        this.questionRepository = questionRepository;
     }
 
     public Result add(String name) {
@@ -54,12 +52,14 @@ public class CategoryService {
 
         int type = category.getType();
         if (type == CategoryType.Category.getValue()) {
-            int articleCount = articleRepository.countByCategoryId(id);
+            int postType = ArticleType.Article.getValue();
+            int articleCount = articleRepository.countByCategoryIdAndPostType(id, postType);
             if (articleCount != 0) {
                 return Result.fail("分类中包含有文章");
             }
 
-            int questionCount = questionRepository.countByCategoryId(id);
+            int postType1 = ArticleType.Question.getValue();
+            int questionCount = articleRepository.countByCategoryIdAndPostType(id, postType1);
             if (questionCount != 0) {
                 return Result.fail("Question 中含有此分类");
             }

+ 37 - 39
search/search-service/src/main/java/cn/reghao/tnb/search/app/blog/service/QuestionService.java

@@ -1,16 +1,15 @@
 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.db.repository.QuestionRepository;
-import cn.reghao.tnb.search.app.blog.model.dto.ArticleDto;
 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.po.Question;
 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 cn.reghao.jutil.jdk.string.IdGenerator;
-import cn.reghao.jutil.jdk.web.result.Result;
 import jakarta.persistence.criteria.Predicate;
 import org.springframework.data.domain.*;
 import org.springframework.data.jpa.domain.Specification;
@@ -26,68 +25,56 @@ import java.util.stream.Collectors;
  */
 @Service
 public class QuestionService {
-    private final QuestionRepository questionRepository;
+    private final ArticleRepository articleRepository;
     private final CategoryRepository categoryRepository;
-    private final IdGenerator idGenerator;
+    private final ArticleTagRepository articleTagRepository;
 
-    public QuestionService(QuestionRepository questionRepository, CategoryRepository categoryRepository) {
-        this.questionRepository = questionRepository;
+    public QuestionService(ArticleRepository articleRepository, CategoryRepository categoryRepository,
+                           ArticleTagRepository articleTagRepository) {
+        this.articleRepository = articleRepository;
         this.categoryRepository = categoryRepository;
-        this.idGenerator = new IdGenerator("question-id");
-    }
-
-    public Result addQuestion(ArticleDto articleDto) {
-        int categoryId = articleDto.getCategoryId();
-        Category category = categoryRepository.findById(categoryId).orElse(null);
-        if (category == null) {
-            category = new Category();
-            categoryRepository.save(category);
-        }
-        String questionId = idGenerator.stringId();
-        Question question = new Question(questionId, articleDto, categoryId);
-        questionRepository.save(question);
-        return Result.success();
+        this.articleTagRepository = articleTagRepository;
     }
 
     public void updateQuestion(QuestionUpdateDto questionUpdateDto) {
         String questionId = questionUpdateDto.getQuestionId();
-        Question question = questionRepository.findByQuestionId(questionId);
+        Article question = articleRepository.findByArticleId(questionId);
         if (question != null) {
             question.setTitle(questionUpdateDto.getTitle());
             question.setContent(questionUpdateDto.getContent());
-            questionRepository.save(question);
+            articleRepository.save(question);
         }
     }
 
     public void addWeight(String questionId) {
-        Question question = questionRepository.findByQuestionId(questionId);
+        Article question = articleRepository.findByArticleId(questionId);
         if (question != null) {
             int weight = question.getWeight();
             question.setWeight(weight+1);
-            questionRepository.save(question);
+            articleRepository.save(question);
         }
     }
 
     public void minusWeight(String questionId) {
-        Question question = questionRepository.findByQuestionId(questionId);
+        Article question = articleRepository.findByArticleId(questionId);
         if (question != null) {
             int weight = question.getWeight();
             question.setWeight(weight-1);
-            questionRepository.save(question);
+            articleRepository.save(question);
         }
     }
 
     public void deleteQuestions(List<String> questionIds) {
         questionIds.forEach(questionId -> {
-            Question question = questionRepository.findByQuestionId(questionId);
+            Article question = articleRepository.findByArticleId(questionId);
             if (question != null) {
-                questionRepository.delete(question);
+                articleRepository.delete(question);
             }
         });
     }
 
     public Page<QuestionView> findQuestionByPage(int pageSize, int pageNumber, int categoryId) {
-        Specification<Question> specification = (root, query, cb) -> {
+        Specification<Article> specification = (root, query, cb) -> {
             List<Predicate> predicates = new ArrayList<>();
             if (categoryId != 0) {
                 predicates.add(cb.equal(root.get("categoryId"), categoryId));
@@ -97,32 +84,43 @@ public class QuestionService {
         };
 
         Pageable pageable = PageRequest.of(pageNumber-1, pageSize, Sort.by(Sort.Direction.DESC, "weight"));
-        Page<Question> page = questionRepository.findAll(specification, pageable);
+        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 = question.getTag();
+            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 = questionRepository.findQuestionCountByGroup();
+        List<QuestionCount> questionCounts = articleRepository.findQuestionCountByGroup();
         List<Integer> ids = questionCounts.stream().map(QuestionCount::getCategoryId).collect(Collectors.toList());
         return categoryRepository.findAllById(ids);
     }
 
-    public Question findByQuestionId(String questionId) {
-        return questionRepository.findByQuestionId(questionId);
+    public Article findByQuestionId(String questionId) {
+        return articleRepository.findByArticleId(questionId);
     }
 
     public QuestionView getQuestionView(String questionId) {
-        Question question = questionRepository.findByQuestionId(questionId);
+        Article question = articleRepository.findByArticleId(questionId);
         Category category = categoryRepository.findById(question.getCategoryId()).orElse(null);
         String categoryName = category.getName();
-        String tagName = question.getTag();
+        String tagName = getTagName(question.getArticleId());
 
         QuestionView questionView = new QuestionView(question, categoryName, tagName);
         String html = MarkdownUtil.getHtml(question.getContent());

+ 60 - 0
search/search-service/src/test/java/BlogTest.java

@@ -0,0 +1,60 @@
+import cn.reghao.tnb.search.app.SearchApplication;
+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.db.repository.QuestionRepository;
+import cn.reghao.tnb.search.app.blog.model.CategoryType;
+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.po.Question;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2025-12-04 16:14:08
+ */
+@Slf4j
+@ActiveProfiles("dev")
+@SpringBootTest(classes = SearchApplication.class)
+public class BlogTest {
+    @Autowired
+    ArticleRepository articleRepository;
+    @Autowired
+    ArticleTagRepository articleTagRepository;
+    @Autowired
+    CategoryRepository categoryRepository;
+    @Autowired
+    QuestionRepository questionRepository;
+
+    @Test
+    public void articleTest() {
+        List<Question> list = questionRepository.findAll();
+        for (Question question : list) {
+            Article article = new Article(question);
+
+            String tag = question.getTag();
+            Category category = categoryRepository.findByTypeAndName(CategoryType.Tag.getValue(), tag);
+            ArticleTag articleTag = null;
+            if (category != null) {
+                articleTag = new ArticleTag(question.getQuestionId(), category.getId());
+            }
+
+            if (tag != null && category == null) {
+                Category category1 = new Category(CategoryType.Tag.getValue(), tag);
+                categoryRepository.save(category1);
+            }
+
+            articleRepository.save(article);
+            if (articleTag != null) {
+                articleTagRepository.save(articleTag);
+            }
+        }
+    }
+}