complete_postlike

Change-Id: If5a628b11d1bf19d531703ec1a8b62f1690ed7fb
diff --git a/src/main/java/com/example/g8backend/entity/PostLike.java b/src/main/java/com/example/g8backend/entity/PostLike.java
new file mode 100644
index 0000000..c3df741
--- /dev/null
+++ b/src/main/java/com/example/g8backend/entity/PostLike.java
@@ -0,0 +1,41 @@
+package com.example.g8backend.entity;
+
+import java.sql.Timestamp;
+
+public class PostLike {
+
+    private Long userId;
+    private Long postId;
+    private Timestamp createdAt;
+
+    public PostLike(Long userId, Long postId) {
+        this.userId = userId;
+        this.postId = postId;
+        this.createdAt = new Timestamp(System.currentTimeMillis());
+    }
+
+    // Getters and Setters
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+
+    public Long getPostId() {
+        return postId;
+    }
+
+    public void setPostId(Long postId) {
+        this.postId = postId;
+    }
+
+    public Timestamp getCreatedAt() {
+        return createdAt;
+    }
+
+    public void setCreatedAt(Timestamp createdAt) {
+        this.createdAt = createdAt;
+    }
+}
diff --git a/src/main/java/com/example/g8backend/mapper/PostMapper.java b/src/main/java/com/example/g8backend/mapper/PostMapper.java
index 0cba5c1..51c3164 100644
--- a/src/main/java/com/example/g8backend/mapper/PostMapper.java
+++ b/src/main/java/com/example/g8backend/mapper/PostMapper.java
@@ -1,17 +1,24 @@
 package com.example.g8backend.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.example.g8backend.entity.Post;
+import com.example.g8backend.entity.PostLike;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Insert;
 
 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 " +
@@ -30,6 +37,22 @@
             "</if>" +
             "</script>")
     List<Post> searchPosts(@Param("keyword") String keyword,
-                           @Param("tagIds") List<Long> tagIds,  // 改成接受一个tagIds列表
+                           @Param("tagIds") List<Long> tagIds,
                            @Param("author") String author);
+
+    // 检查用户是否已经点赞该帖子
+    @Select("SELECT EXISTS (SELECT 1 FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId})")
+    boolean existsByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
+
+    // 插入一条点赞记录
+    @Insert("INSERT INTO post_likes (user_id, post_id) VALUES (#{userId}, #{postId})")
+    void insert(PostLike postLike);
+
+    // 删除用户对帖子的点赞记录
+    @Delete("DELETE FROM post_likes WHERE user_id = #{userId} AND post_id = #{postId}")
+    void deleteLikeByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
+
+    // 获取某个帖子点赞数
+    @Select("SELECT COUNT(*) FROM post_likes WHERE post_id = #{postId}")
+    Long selectCount(@Param("postId") Long postId);
 }
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 37db6af..647095d 100644
--- a/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java
+++ b/src/main/java/com/example/g8backend/service/impl/PostServiceImpl.java
@@ -3,6 +3,7 @@
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.example.g8backend.entity.Post;
+import com.example.g8backend.entity.PostLike;
 import com.example.g8backend.entity.PostTag;
 import com.example.g8backend.mapper.PostMapper;
 import com.example.g8backend.service.IPostService;
@@ -59,21 +60,29 @@
     }
 
     @Override
-    public Long getPostLikeCount(Long postId) {
-        // TODO: 需要实现post_likes表的查询
-        return 0L;
-    }
-
-    @Override
     public void likePost(Long userId, Long postId) {
-        // TODO: 需要实现post_likes表的插入
+        // 检查用户是否已经点赞该帖子
+        boolean exists = postMapper.existsByUserIdAndPostId(userId, postId);
+        if (!exists) {
+            // 如果没有点赞,则执行插入操作
+            PostLike postLike = new PostLike(userId, postId);
+            postMapper.insert(postLike);  // 执行插入点赞记录
+        }
     }
 
+    // 取消点赞功能
     @Override
     public void unlikePost(Long userId, Long postId) {
-        // TODO: 需要实现post_likes表的删除
+        // 删除用户对帖子的点赞记录
+        postMapper.deleteLikeByUserIdAndPostId(userId, postId);  // 使用新的方法删除记录
     }
 
+    // 获取帖子点赞数
+    @Override
+    public Long getPostLikeCount(Long postId) {
+        // 使用 postMapper 直接查询点赞数(调用数据库中的 SQL 查询)
+        return postMapper.selectCount(postId);
+    }
     @Override
     public List<Post> searchPosts(String keyword, List<Long> tagIds, String author) {
         return postMapper.searchPosts(keyword, tagIds, author); // 调用mapper的搜索方法