帖子post重提交
Change-Id: Ib28dc79d604faf43233c720031a4f18d84b22688
diff --git a/src/main/java/com/pt5/pthouduan/controller/PostController.java b/src/main/java/com/pt5/pthouduan/controller/PostController.java
index a3a9017..c497d87 100644
--- a/src/main/java/com/pt5/pthouduan/controller/PostController.java
+++ b/src/main/java/com/pt5/pthouduan/controller/PostController.java
@@ -23,7 +23,7 @@
* @author
* @since 2025-05-10
*/
-@CrossOrigin(origins = "http://localhost:5173")
+@CrossOrigin(origins = {"http://localhost:5173", "http://localhost:3000"})
@Controller
@RequestMapping("/post")
public class PostController {
@@ -49,7 +49,7 @@
post.setPostContent(post_content);
post.setTags(tags);
post.setRannge(rannge);
- post.setIsSticky(is_pinned != null && is_pinned);
+ post.setIsPinned(is_pinned != null && is_pinned);
post.setPostCreatedTime(LocalDateTime.now());
post.setUpdatedTime(LocalDateTime.now());
post.setLikes(0);
@@ -146,4 +146,18 @@
public List<Post> findPinnedPosts() {
return postService.findPinnedPosts();
}
+
+
+
+ // 添加切换置顶状态的新端点
+ @PutMapping("/togglePin/{postid}")
+ @ResponseBody
+ public Boolean togglePinStatus(@PathVariable Integer postid) {
+ Post post = postService.getById(postid);
+ if (post == null) return null;
+
+ boolean newStatus = !post.getIsPinned();
+ boolean result = postService.setPinnedStatus(postid, newStatus);
+ return result ? newStatus : null; // ✅ 返回新状态
+ }
}
diff --git a/src/main/java/com/pt5/pthouduan/entity/Post.java b/src/main/java/com/pt5/pthouduan/entity/Post.java
index 843800d..64f0c72 100644
--- a/src/main/java/com/pt5/pthouduan/entity/Post.java
+++ b/src/main/java/com/pt5/pthouduan/entity/Post.java
@@ -99,11 +99,11 @@
this.rannge = rannge;
}
- public Boolean getIsSticky() {
+ public Boolean getIsPinned() {
return is_pinned;
}
- public void setIsSticky(Boolean is_pinned) {
+ public void setIsPinned(Boolean is_pinned) {
this.is_pinned = is_pinned;
}
diff --git a/src/main/java/com/pt5/pthouduan/mapper/PostMapper.java b/src/main/java/com/pt5/pthouduan/mapper/PostMapper.java
index 6b95c73..0cd0480 100644
--- a/src/main/java/com/pt5/pthouduan/mapper/PostMapper.java
+++ b/src/main/java/com/pt5/pthouduan/mapper/PostMapper.java
@@ -39,7 +39,7 @@
int decrementLikes(@Param("postid") Integer postid);
// 设置置顶状态
- int updatePinnedStatus(@Param("postid") Integer postid, @Param("pinned") boolean pinned);
+ int updatePinnedStatus(@Param("postid") Integer postid, @Param("isPinned") boolean isPinned);
// 根据用户ID查询该用户所有帖子
List<Post> findByUserid(@Param("userid") Long userid);
@@ -47,6 +47,9 @@
// 查询所有置顶帖子
List<Post> findPinnedPosts();
- // ✅ 新增:查询所有帖子(置顶优先,时间倒序)
+ // ✅ 查询所有帖子(置顶优先,时间倒序)
List<Post> selectAllSorted();
+
+ // ✅ 根据帖子ID查询单个帖子(用于 togglePin 等功能)
+ Post selectById(@Param("postid") Integer postid);
}
diff --git a/src/main/java/com/pt5/pthouduan/service/PostService.java b/src/main/java/com/pt5/pthouduan/service/PostService.java
index fabf254..7db4d26 100644
--- a/src/main/java/com/pt5/pthouduan/service/PostService.java
+++ b/src/main/java/com/pt5/pthouduan/service/PostService.java
@@ -43,4 +43,6 @@
List<Post> findPinnedPosts(); // 查找所有置顶帖子
List<Post> getAllPostsSorted(); // ✅ 获取所有帖子(置顶优先,按时间排序)
+
+ Post getById(Integer postid);
}
diff --git a/src/main/java/com/pt5/pthouduan/service/impl/PostServiceImpl.java b/src/main/java/com/pt5/pthouduan/service/impl/PostServiceImpl.java
index d1bda7f..37edf51 100644
--- a/src/main/java/com/pt5/pthouduan/service/impl/PostServiceImpl.java
+++ b/src/main/java/com/pt5/pthouduan/service/impl/PostServiceImpl.java
@@ -12,6 +12,7 @@
* <p>
* 帖子服务实现类
* </p>
+ * 提供帖子增删改查、点赞、置顶等功能的业务实现
*
* @author 杨蔓
* @since 2025-04-14
@@ -29,8 +30,8 @@
}
@Override
- public boolean deletePost(Integer postid) {
- return postMapper.deleteByPostid(postid) > 0;
+ public boolean deletePost(Integer postId) {
+ return postMapper.deleteByPostid(postId) > 0;
}
@Override
@@ -44,23 +45,23 @@
}
@Override
- public boolean incrementLikes(Integer postid) {
- return postMapper.incrementLikes(postid) >= 0;
+ public boolean incrementLikes(Integer postId) {
+ return postMapper.incrementLikes(postId) >= 0;
}
@Override
- public boolean decrementLikes(Integer postid) {
- return postMapper.decrementLikes(postid) >= 0;
+ public boolean decrementLikes(Integer postId) {
+ return postMapper.decrementLikes(postId) >= 0;
}
@Override
- public boolean setPinnedStatus(Integer postid, boolean pinned) {
- return postMapper.updatePinnedStatus(postid, pinned) > 0;
+ public boolean setPinnedStatus(Integer postId, boolean isPinned) {
+ return postMapper.updatePinnedStatus(postId, isPinned) > 0;
}
@Override
- public List<Post> findByUserid(Long userid) {
- return postMapper.findByUserid(userid);
+ public List<Post> findByUserid(Long userId) {
+ return postMapper.findByUserid(userId);
}
@Override
@@ -68,9 +69,14 @@
return postMapper.findPinnedPosts();
}
- /** ✅ 新增:获取所有帖子(置顶优先,时间倒序) */
@Override
public List<Post> getAllPostsSorted() {
return postMapper.selectAllSorted();
}
+
+ /** ✅ 补充:根据帖子 ID 获取帖子对象 */
+ @Override
+ public Post getById(Integer postId) {
+ return postMapper.selectById(postId);
+ }
}
diff --git a/src/main/resources/mapper/PostMapper.xml b/src/main/resources/mapper/PostMapper.xml
index e676fcc..ad6024c 100644
--- a/src/main/resources/mapper/PostMapper.xml
+++ b/src/main/resources/mapper/PostMapper.xml
@@ -78,7 +78,7 @@
<!-- 更新置顶状态 -->
<update id="updatePinnedStatus" parameterType="map">
UPDATE post
- SET is_pinned = #{pinned}
+ SET is_pinned = #{isPinned}
WHERE postid = #{postid}
</update>
@@ -100,4 +100,10 @@
ORDER BY is_pinned DESC, postCreatedTime DESC
</select>
+ <!-- ✅ 根据 postid 查询单个帖子 -->
+ <select id="selectById" parameterType="int" resultType="com.pt5.pthouduan.entity.Post">
+ SELECT * FROM post
+ WHERE postid = #{postid}
+ </select>
+
</mapper>
diff --git a/src/test/java/com/pt5/pthouduan/controller/PostControllerTest.java b/src/test/java/com/pt5/pthouduan/controller/PostControllerTest.java
new file mode 100644
index 0000000..05bf209
--- /dev/null
+++ b/src/test/java/com/pt5/pthouduan/controller/PostControllerTest.java
@@ -0,0 +1,145 @@
+package com.pt5.pthouduan.controller;
+
+import com.pt5.pthouduan.entity.Post;
+import com.pt5.pthouduan.service.PostService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+class PostControllerTest {
+
+ @Mock
+ private PostService postService;
+
+ @InjectMocks
+ private PostController postController;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ void createPost_WithValidData_ShouldReturnTrue() {
+ MultipartFile photo = mock(MultipartFile.class);
+ when(photo.isEmpty()).thenReturn(true); // 模拟没有上传图片
+
+ Post mockPost = new Post();
+ mockPost.setPostid(1);
+ when(postService.createPost(any(Post.class))).thenReturn(mockPost);
+
+ boolean result = postController.createPost(
+ 1L, "标题", "内容", "标签", "全站", true, photo
+ );
+
+ assertTrue(result);
+ }
+
+ @Test
+ void deletePost_ShouldReturnTrue_WhenServiceSucceeds() {
+ when(postService.deletePost(1)).thenReturn(true);
+ boolean result = postController.deletePost(1);
+ assertTrue(result);
+ }
+
+ @Test
+ void updatePost_ShouldReturnTrue_WhenUpdateSucceeds() {
+ Post post = new Post();
+ post.setPostid(1);
+ when(postService.updatePost(post)).thenReturn(true);
+ assertTrue(postController.updatePost(post));
+ }
+
+ @Test
+ void searchPosts_ShouldReturnMatchedPosts() {
+ Post post1 = new Post();
+ post1.setPostTitle("关键字匹配");
+
+ when(postService.searchPostsByKeyword("关键字")).thenReturn(List.of(post1));
+
+ List<Post> result = postController.searchPosts("关键字");
+
+ assertEquals(1, result.size());
+ assertEquals("关键字匹配", result.get(0).getPostTitle());
+ }
+
+ @Test
+ void likePost_ShouldReturnTrue() {
+ when(postService.incrementLikes(1)).thenReturn(true);
+ assertTrue(postController.likePost(1));
+ }
+
+ @Test
+ void unlikePost_ShouldReturnTrue() {
+ when(postService.decrementLikes(1)).thenReturn(true);
+ assertTrue(postController.unlikePost(1));
+ }
+
+ @Test
+ void pinPost_ShouldReturnTrue() {
+ when(postService.setPinnedStatus(1, true)).thenReturn(true);
+ assertTrue(postController.pinPost(1));
+ }
+
+ @Test
+ void unpinPost_ShouldReturnTrue() {
+ when(postService.setPinnedStatus(1, false)).thenReturn(true);
+ assertTrue(postController.unpinPost(1));
+ }
+
+ @Test
+ void findByUserid_ShouldReturnUserPosts() {
+ Post post = new Post();
+ post.setUserid(1L);
+ when(postService.findByUserid(1L)).thenReturn(List.of(post));
+
+ List<Post> result = postController.findByUserid(1L);
+ assertEquals(1, result.size());
+ assertEquals(1L, result.get(0).getUserid());
+ }
+
+ @Test
+ void findPinnedPosts_ShouldReturnPinnedList() {
+ Post post = new Post();
+ post.setIsPinned(true);
+ when(postService.findPinnedPosts()).thenReturn(List.of(post));
+
+ List<Post> result = postController.findPinnedPosts();
+ assertEquals(1, result.size());
+ assertTrue(result.get(0).getIsPinned());
+ }
+
+ @Test
+ void togglePinStatus_ShouldToggleAndReturnNewStatus() {
+ Post post = new Post();
+ post.setPostid(1);
+ post.setIsPinned(false);
+
+ when(postService.getById(1)).thenReturn(post);
+ when(postService.setPinnedStatus(1, true)).thenReturn(true);
+
+ Boolean result = postController.togglePinStatus(1);
+ assertTrue(result);
+ }
+
+ @Test
+ void getAllPostsSorted_ShouldReturnAllPosts() {
+ Post post1 = new Post();
+ Post post2 = new Post();
+ when(postService.getAllPostsSorted()).thenReturn(Arrays.asList(post1, post2));
+
+ List<Post> posts = postController.getAllPostsSorted();
+ assertEquals(2, posts.size());
+ }
+}