帖子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>