mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-23 06:30:48 +08:00
feat(posts): 优化帖子评论统计性能
- 在 Post 模型中添加 commentCount 和 lastReplyAt 字段 - 在 CommentService 中实现更新帖子评论统计的方法 - 在 PostMapper 中使用 Post 模型中的评论统计字段 - 新增数据库迁移脚本,添加评论统计字段和索引 - 更新相关测试用例
This commit is contained in:
@@ -55,6 +55,10 @@ class PostControllerTest {
|
||||
private UserVisitService userVisitService;
|
||||
@MockBean
|
||||
private PostReadService postReadService;
|
||||
@MockBean
|
||||
private MedalService medalService;
|
||||
@MockBean
|
||||
private com.openisle.repository.PollVoteRepository pollVoteRepository;
|
||||
|
||||
@Test
|
||||
void createAndGetPost() throws Exception {
|
||||
@@ -63,9 +67,13 @@ class PostControllerTest {
|
||||
Category cat = new Category();
|
||||
cat.setId(1L);
|
||||
cat.setName("tech");
|
||||
cat.setDescription("Technology category");
|
||||
cat.setIcon("tech-icon");
|
||||
Tag tag = new Tag();
|
||||
tag.setId(1L);
|
||||
tag.setName("java");
|
||||
tag.setDescription("Java programming language");
|
||||
tag.setIcon("java-icon");
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
post.setTitle("t");
|
||||
@@ -111,9 +119,13 @@ class PostControllerTest {
|
||||
Category cat = new Category();
|
||||
cat.setId(1L);
|
||||
cat.setName("tech");
|
||||
cat.setDescription("Technology category");
|
||||
cat.setIcon("tech-icon");
|
||||
Tag tag = new Tag();
|
||||
tag.setId(1L);
|
||||
tag.setName("java");
|
||||
tag.setDescription("Java programming language");
|
||||
tag.setIcon("java-icon");
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
post.setTitle("t2");
|
||||
@@ -147,9 +159,13 @@ class PostControllerTest {
|
||||
Category cat = new Category();
|
||||
cat.setId(1L);
|
||||
cat.setName("tech");
|
||||
cat.setDescription("Technology category");
|
||||
cat.setIcon("tech-icon");
|
||||
Tag tag = new Tag();
|
||||
tag.setId(1L);
|
||||
tag.setName("java");
|
||||
tag.setDescription("Java programming language");
|
||||
tag.setIcon("java-icon");
|
||||
Post post = new Post();
|
||||
post.setId(2L);
|
||||
post.setTitle("hello");
|
||||
@@ -197,9 +213,13 @@ class PostControllerTest {
|
||||
Category cat = new Category();
|
||||
cat.setId(1L);
|
||||
cat.setName("tech");
|
||||
cat.setDescription("Technology category");
|
||||
cat.setIcon("tech-icon");
|
||||
Tag tag = new Tag();
|
||||
tag.setId(1L);
|
||||
tag.setName("java");
|
||||
tag.setDescription("Java programming language");
|
||||
tag.setIcon("java-icon");
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
post.setTitle("t");
|
||||
@@ -262,6 +282,8 @@ class PostControllerTest {
|
||||
Category cat = new Category();
|
||||
cat.setId(1L);
|
||||
cat.setName("tech");
|
||||
cat.setDescription("Technology category");
|
||||
cat.setIcon("tech-icon");
|
||||
Post post = new Post();
|
||||
post.setId(1L);
|
||||
post.setTitle("t");
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.model.*;
|
||||
import com.openisle.repository.*;
|
||||
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.TestPropertySource;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
@TestPropertySource(locations = "classpath:application.properties")
|
||||
@Transactional
|
||||
public class PostCommentStatsTest {
|
||||
|
||||
@Autowired
|
||||
private PostRepository postRepository;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private CategoryRepository categoryRepository;
|
||||
|
||||
@Autowired
|
||||
private TagRepository tagRepository;
|
||||
|
||||
@Autowired
|
||||
private CommentService commentService;
|
||||
|
||||
@Test
|
||||
public void testPostCommentStatsUpdate() {
|
||||
// Create test user
|
||||
User user = new User();
|
||||
user.setUsername("testuser");
|
||||
user.setEmail("test@example.com");
|
||||
user.setPassword("hash");
|
||||
user = userRepository.save(user);
|
||||
|
||||
// Create test category
|
||||
Category category = new Category();
|
||||
category.setName("Test Category");
|
||||
category.setDescription("Test Category Description");
|
||||
category.setIcon("test-icon");
|
||||
category = categoryRepository.save(category);
|
||||
|
||||
// Create test tag
|
||||
Tag tag = new Tag();
|
||||
tag.setName("Test Tag");
|
||||
tag.setDescription("Test Tag Description");
|
||||
tag.setIcon("test-tag-icon");
|
||||
tag = tagRepository.save(tag);
|
||||
|
||||
// Create test post
|
||||
Post post = new Post();
|
||||
post.setTitle("Test Post");
|
||||
post.setContent("Test content");
|
||||
post.setAuthor(user);
|
||||
post.setCategory(category);
|
||||
post.getTags().add(tag);
|
||||
post.setStatus(PostStatus.PUBLISHED);
|
||||
post.setCommentCount(0L);
|
||||
post = postRepository.save(post);
|
||||
|
||||
// Verify initial state
|
||||
assertEquals(0L, post.getCommentCount());
|
||||
assertNull(post.getLastReplyAt());
|
||||
|
||||
// Add a comment
|
||||
commentService.addComment("testuser", post.getId(), "Test comment");
|
||||
|
||||
// Refresh post from database
|
||||
post = postRepository.findById(post.getId()).orElseThrow();
|
||||
|
||||
// Verify comment count and last reply time are updated
|
||||
assertEquals(1L, post.getCommentCount());
|
||||
assertNotNull(post.getLastReplyAt());
|
||||
|
||||
// Add another comment
|
||||
commentService.addComment("testuser", post.getId(), "Another comment");
|
||||
|
||||
// Refresh post again
|
||||
post = postRepository.findById(post.getId()).orElseThrow();
|
||||
|
||||
// Verify comment count is updated
|
||||
assertEquals(2L, post.getCommentCount());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user