添加Comment相关文件

Change-Id: I823c09a1b576af5b176538f45b30e81cc7789550
diff --git a/src/main/java/com/pt5/pthouduan/controller/CommentController.java b/src/main/java/com/pt5/pthouduan/controller/CommentController.java
new file mode 100644
index 0000000..ae80c30
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/controller/CommentController.java
@@ -0,0 +1,71 @@
+package com.pt5.pthouduan.controller;
+
+import com.pt5.pthouduan.entity.Comment;
+import com.pt5.pthouduan.service.CommentService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.stereotype.Controller;
+
+import java.util.List;
+
+/**
+ * <p>
+ *  评论前端控制器
+ * </p>
+ *
+ * 功能:增、删、改、查(按帖子ID)
+ *
+ * @author ym
+ * @since 2025-04-14
+ */
+@CrossOrigin(origins = "http://localhost:5173")
+@Controller
+@RequestMapping("/comment")
+public class CommentController {
+
+    @Autowired
+    private CommentService commentService;
+
+    // 创建评论
+    @PostMapping("/create")
+    @ResponseBody
+    public Comment createComment(@RequestBody Comment comment) {
+    System.out.println("Received comment: " + comment);  // 输出接收到的评论数据
+    return commentService.createComment(comment);
+}
+
+    // 删除评论(根据commentid)
+    @DeleteMapping("/delete/{commentid}")
+    @ResponseBody
+    public boolean deleteComment(@PathVariable Integer commentid) {
+        return commentService.deleteComment(commentid);
+    }
+
+    // 更新评论
+    @PutMapping("/update")
+    @ResponseBody
+    public boolean updateComment(@RequestBody Comment comment) {
+        return commentService.updateComment(comment);
+    }
+
+    // 获取某个帖子的所有评论
+    @GetMapping("/post/{postid}")
+    @ResponseBody
+    public List<Comment> getCommentsByPostId(@PathVariable Integer postid) {
+        return commentService.getCommentsByPostId(postid);
+    }
+
+    // 点赞评论
+    @PostMapping("/like/{commentid}")
+    @ResponseBody
+    public boolean likeComment(@PathVariable Integer commentid) {
+        return commentService.likeComment(commentid);
+    }
+
+    // 取消点赞评论
+    @PostMapping("/unlike/{commentid}")
+    @ResponseBody
+    public boolean unlikeComment(@PathVariable Integer commentid) {
+        return commentService.unlikeComment(commentid);
+    }
+}
diff --git a/src/main/java/com/pt5/pthouduan/entity/Comment.java b/src/main/java/com/pt5/pthouduan/entity/Comment.java
index 6b96288..6f56a68 100644
--- a/src/main/java/com/pt5/pthouduan/entity/Comment.java
+++ b/src/main/java/com/pt5/pthouduan/entity/Comment.java
@@ -11,7 +11,7 @@
  * 
  * </p>
  *
- * @author ljx
+ * @author ym
  * @since 2025-04-14
  */
 @TableName("comment")
@@ -20,7 +20,11 @@
     private static final long serialVersionUID = 1L;
 
     @TableId("commentid")
-    private String commentid;
+    private Integer commentid;
+
+    private Integer likes;
+
+    private Long userid;
 
     private Integer postid;
 
@@ -28,14 +32,23 @@
 
     private LocalDateTime commenttime;
 
-    public String getCommentid() {
+    public Integer getCommentid() {
         return commentid;
     }
 
-    public void setCommentid(String commentid) {
+    public void setCommentid(Integer commentid) {
         this.commentid = commentid;
     }
 
+
+    public Long getUserid() {
+        return userid;
+    }
+
+    public void setUserid(Long userid) {
+        this.userid = userid;
+    }
+
     public Integer getPostid() {
         return postid;
     }
@@ -44,6 +57,14 @@
         this.postid = postid;
     }
 
+    public Integer getLikes() {
+        return likes;
+    }
+
+    public void setLikes(Integer likes) {
+        this.likes = likes;
+    }
+
     public String getPostCommentcontent() {
         return postCommentcontent;
     }
@@ -64,6 +85,7 @@
     public String toString() {
         return "Comment{" +
         "commentid = " + commentid +
+                ", userid = " + userid +
         ", postid = " + postid +
         ", postCommentcontent = " + postCommentcontent +
         ", commenttime = " + commenttime +
diff --git a/src/main/java/com/pt5/pthouduan/mapper/CommentMapper.java b/src/main/java/com/pt5/pthouduan/mapper/CommentMapper.java
index 755a2b5..d59e110 100644
--- a/src/main/java/com/pt5/pthouduan/mapper/CommentMapper.java
+++ b/src/main/java/com/pt5/pthouduan/mapper/CommentMapper.java
@@ -3,16 +3,39 @@
 import com.pt5.pthouduan.entity.Comment;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 /**
  * <p>
- *  Mapper 接口
+ *  评论 Mapper 接口
  * </p>
  *
- * @author ljx
+ * 功能:增、删、改、查(按帖子ID)
+ *
+ * @author ym
  * @since 2025-04-14
  */
 @Mapper
 public interface CommentMapper extends BaseMapper<Comment> {
 
+    // 创建评论
+    void save(Comment comment);
+
+    // 根据评论ID删除
+    int deleteByCommentid(@Param("commentid") Integer commentid);
+
+    // 更新评论
+    int updateComment(Comment comment);
+
+    // 获取指定帖子下的所有评论
+    List<Comment> selectByPostid(@Param("postid") Integer postid);
+
+    // 点赞 +1
+    int incrementLikes(@Param("commentid") Integer commentid);
+
+    // 取消点赞 -1(最小为0)
+    int decrementLikes(@Param("commentid") Integer commentid);
+
 }
diff --git a/src/main/java/com/pt5/pthouduan/service/CommentService.java b/src/main/java/com/pt5/pthouduan/service/CommentService.java
new file mode 100644
index 0000000..c47d3a8
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/service/CommentService.java
@@ -0,0 +1,37 @@
+package com.pt5.pthouduan.service;
+
+import com.pt5.pthouduan.entity.Comment;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 评论服务接口
+ * </p>
+ *
+ * 功能:增、删、改、查(按帖子ID)
+ *
+ * @author ym
+ * @since 2025-04-14
+ */
+public interface CommentService {
+
+    // 创建评论
+    Comment createComment(Comment comment);
+
+    // 删除评论
+    boolean deleteComment(Integer commentid);
+
+    // 更新评论
+    boolean updateComment(Comment comment);
+
+    // 根据帖子ID获取所有评论
+    List<Comment> getCommentsByPostId(Integer postid);
+
+    // 点赞评论
+    boolean likeComment(Integer commentid);
+
+    // 取消点赞评论
+    boolean unlikeComment(Integer commentid);
+
+}
diff --git a/src/main/java/com/pt5/pthouduan/service/impl/CommentServiceImpl.java b/src/main/java/com/pt5/pthouduan/service/impl/CommentServiceImpl.java
new file mode 100644
index 0000000..f817e3a
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/service/impl/CommentServiceImpl.java
@@ -0,0 +1,61 @@
+package com.pt5.pthouduan.service.impl;
+
+import com.pt5.pthouduan.entity.Comment;
+import com.pt5.pthouduan.mapper.CommentMapper;
+import com.pt5.pthouduan.service.CommentService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 评论服务实现类
+ * </p>
+ *
+ * 实现了评论的增删改查逻辑
+ *
+ * @author ym
+ * @since 2025-04-14
+ */
+@Service
+public class CommentServiceImpl implements CommentService {
+
+    @Autowired
+    private CommentMapper commentMapper;
+
+    @Override
+    public Comment createComment(Comment comment) {
+        commentMapper.save(comment);
+        return comment;
+    }
+
+    @Override
+    public boolean deleteComment(Integer commentid) {
+        return commentMapper.deleteByCommentid(commentid) > 0;
+    }
+
+    @Override
+    public boolean updateComment(Comment comment) {
+        return commentMapper.updateComment(comment) > 0;
+    }
+
+    @Override
+    public List<Comment> getCommentsByPostId(Integer postid) {
+        return commentMapper.selectByPostid(postid);
+    }
+
+
+    @Override
+    public boolean likeComment(Integer commentid) {
+        return commentMapper.incrementLikes(commentid) > 0;
+    }
+
+    @Override
+    public boolean unlikeComment(Integer commentid) {
+        return commentMapper.decrementLikes(commentid) > 0;
+
+    }
+
+
+}
diff --git a/src/main/resources/mapper/CommentMapper.xml b/src/main/resources/mapper/CommentMapper.xml
new file mode 100644
index 0000000..81e4581
--- /dev/null
+++ b/src/main/resources/mapper/CommentMapper.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="com.pt5.pthouduan.mapper.CommentMapper">
+
+    <!-- 创建评论 -->
+    <insert id="save" parameterType="com.pt5.pthouduan.entity.Comment">
+        INSERT INTO comment (commentid, userid, postid, post_commentcontent, commenttime)
+        VALUES (#{commentid}, #{userid}, #{postid}, #{postCommentcontent}, #{commenttime})
+    </insert>
+
+    <!-- 删除评论 -->
+    <delete id="deleteByCommentid" parameterType="int">
+        DELETE FROM comment WHERE commentid = #{commentid}
+    </delete>
+
+    <!-- 更新评论 -->
+    <update id="updateComment" parameterType="com.pt5.pthouduan.entity.Comment">
+        UPDATE comment
+        SET userid = #{userid},
+            postid = #{postid},
+            post_commentcontent = #{postCommentcontent},
+            commenttime = #{commenttime}
+        WHERE commentid = #{commentid}
+    </update>
+
+    <!-- 按帖子ID查询所有评论 -->
+    <select id="selectByPostid" parameterType="int" resultType="com.pt5.pthouduan.entity.Comment">
+        SELECT * FROM comment
+        WHERE postid = #{postid}
+        ORDER BY commenttime ASC
+    </select>
+
+    <!-- 点赞 +1 -->
+    <update id="incrementLikes" parameterType="int">
+        UPDATE comment
+        SET likes = likes + 1
+        WHERE commentid = #{commentid}
+    </update>
+
+    <!-- 取消点赞 -1(最小为0) -->
+    <update id="decrementLikes" parameterType="int">
+        UPDATE comment
+        SET likes = CASE
+                        WHEN likes > 0 THEN likes - 1
+                        ELSE 0
+            END
+        WHERE commentid = #{commentid}
+    </update>
+
+
+</mapper>