searchpost

Change-Id: I3b471ad217a958e2e8bf22c005b1a3fd36454151
diff --git a/src/main/java/com/example/g8backend/controller/PostController.java b/src/main/java/com/example/g8backend/controller/PostController.java
index f373539..a47734a 100644
--- a/src/main/java/com/example/g8backend/controller/PostController.java
+++ b/src/main/java/com/example/g8backend/controller/PostController.java
@@ -109,4 +109,14 @@
         Long likeCount = postService.getPostLikeCount(postId);
         return ResponseEntity.ok().body(likeCount);
     }
+
+    // 搜索帖子
+    @GetMapping("/search")
+    public List<Post> searchPosts(
+            @RequestParam(required = false) String keyword,
+            @RequestParam(required = false) List<Long> tags,  // 修改为接收多个标签
+            @RequestParam(required = false) String author) {
+
+        return postService.searchPosts(keyword, tags, author);
+    }
 }
diff --git a/src/main/java/com/example/g8backend/mapper/PostMapper.java b/src/main/java/com/example/g8backend/mapper/PostMapper.java
index 6db4e67..0cba5c1 100644
--- a/src/main/java/com/example/g8backend/mapper/PostMapper.java
+++ b/src/main/java/com/example/g8backend/mapper/PostMapper.java
@@ -4,10 +4,32 @@
 import com.example.g8backend.entity.Post;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
 
 import java.util.List;
 
 @Mapper
 public interface PostMapper extends BaseMapper<Post> {
     List<Post> getPostsByUserId(@Param("userId") Long userId);
+
+    @Select("<script>" +
+            "SELECT p.* " +
+            "FROM posts p " +
+            "LEFT JOIN post_tag pt ON p.post_id = pt.post_id " +
+            "LEFT JOIN tags t ON pt.tag_id = t.tag_id " +
+            "LEFT JOIN users u ON p.user_id = u.user_id " +
+            "WHERE (p.post_title LIKE CONCAT('%', #{keyword}, '%') OR p.post_content LIKE CONCAT('%', #{keyword}, '%')) " +
+            "<if test='tagIds != null and tagIds.size() > 0'> " +
+            "AND pt.tag_id IN " +
+            "<foreach item='tagId' collection='tagIds' open='(' separator=',' close=')'> " +
+            "#{tagId} " +
+            "</foreach> " +
+            "</if>" +
+            "<if test='author != null'> " +
+            "AND u.user_name = #{author} " +
+            "</if>" +
+            "</script>")
+    List<Post> searchPosts(@Param("keyword") String keyword,
+                           @Param("tagIds") List<Long> tagIds,  // 改成接受一个tagIds列表
+                           @Param("author") String author);
 }
diff --git a/src/main/java/com/example/g8backend/service/IPostService.java b/src/main/java/com/example/g8backend/service/IPostService.java
index f914946..82c792e 100644
--- a/src/main/java/com/example/g8backend/service/IPostService.java
+++ b/src/main/java/com/example/g8backend/service/IPostService.java
@@ -14,4 +14,6 @@
     Long getPostLikeCount(Long postId);
     void likePost(Long userId, Long postId);
     void unlikePost(Long userId, Long postId);
+
+    List<Post> searchPosts(String keyword, List<Long> tagIds, String author); // 更新为支持多个标签
 }
diff --git a/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java b/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java
index d40ddd6..37db6af 100644
--- a/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java
+++ b/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java
@@ -14,7 +14,7 @@
 
 @Service
 public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {
-    
+
     private final PostMapper postMapper;
 
     @Autowired
@@ -45,7 +45,7 @@
         }
     }
 
-    @Override 
+    @Override
     public Post updatePost(Post post) {
         updateById(post);
         return getById(post.getPostId());
@@ -73,4 +73,9 @@
     public void unlikePost(Long userId, Long postId) {
         // TODO: 需要实现post_likes表的删除
     }
-}
+
+    @Override
+    public List<Post> searchPosts(String keyword, List<Long> tagIds, String author) {
+        return postMapper.searchPosts(keyword, tagIds, author); // 调用mapper的搜索方法
+    }
+}
\ No newline at end of file
diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql
index 5e5dd43..3c9ff93 100644
--- a/src/main/resources/schema.sql
+++ b/src/main/resources/schema.sql
@@ -84,4 +84,16 @@
     is_read BOOLEAN DEFAULT false,
     FOREIGN KEY (sender_id) REFERENCES users(user_id),
     FOREIGN KEY (receiver_id) REFERENCES users(user_id)
-);
\ No newline at end of file
+);
+
+CREATE TABLE IF NOT EXISTS `comments` (
+    comment_id INT AUTO_INCREMENT PRIMARY KEY,      -- 评论ID
+    post_id INT NOT NULL,                            -- 所属帖子ID
+    user_id INT NOT NULL,                            -- 评论用户ID
+    parent_comment_id INT DEFAULT NULL,             -- 父评论ID, 如果是顶级评论则为NULL
+    content TEXT NOT NULL,                          -- 评论内容
+    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- 评论时间
+    FOREIGN KEY (post_id) REFERENCES posts(post_id), -- 关联帖子
+    FOREIGN KEY (user_id) REFERENCES users(user_id), -- 关联用户
+    FOREIGN KEY (parent_comment_id) REFERENCES comments(comment_id) -- 关联父评论
+);
diff --git a/src/test/java/com/example/g8backend/service/PostServiceTest.java b/src/test/java/com/example/g8backend/service/PostServiceTest.java
index 6f54177..0161229 100644
--- a/src/test/java/com/example/g8backend/service/PostServiceTest.java
+++ b/src/test/java/com/example/g8backend/service/PostServiceTest.java
@@ -149,4 +149,49 @@
         assertTrue(result.isEmpty());
         verify(postMapper).getPostsByUserId(999L);
     }
+
+    // 新增测试方法:搜索帖子,支持多个标签
+    @Test
+    @DisplayName("搜索帖子-通过关键词和多个标签")
+    void searchPosts_WithKeywordAndTags_ShouldReturnPosts() {
+        // Arrange
+        List<Long> tagIds = Arrays.asList(1L, 2L); // 假设存在标签ID 1和2
+        String keyword = "测试内容";
+        String author = "作者";
+        List<Post> expectedPosts = Arrays.asList(testPost);
+
+        // 模拟PostMapper的searchPosts方法
+        when(postMapper.searchPosts(keyword, tagIds, author)).thenReturn(expectedPosts);
+
+        // Act
+        List<Post> result = postService.searchPosts(keyword, tagIds, author);
+
+        // Assert
+        assertNotNull(result);
+        assertFalse(result.isEmpty());
+        assertEquals(testPost.getPostId(), result.get(0).getPostId());
+        verify(postMapper).searchPosts(keyword, tagIds, author);
+    }
+
+    @Test
+    @DisplayName("搜索帖子-没有匹配的帖子")
+    void searchPosts_WhenNoPosts_ShouldReturnEmptyList() {
+        // Arrange
+        List<Long> tagIds = Arrays.asList(3L, 4L); // 假设标签ID 3和4没有匹配的帖子
+        String keyword = "不存在的内容";
+        String author = "不存在的作者";
+        List<Post> expectedPosts = Arrays.asList(); // 没有匹配的帖子
+
+        // 模拟PostMapper的searchPosts方法
+        when(postMapper.searchPosts(keyword, tagIds, author)).thenReturn(expectedPosts);
+
+        // Act
+        List<Post> result = postService.searchPosts(keyword, tagIds, author);
+
+        // Assert
+        assertNotNull(result);
+        assertTrue(result.isEmpty());
+        verify(postMapper).searchPosts(keyword, tagIds, author);
+    }
+
 }
\ No newline at end of file