添加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>
diff --git a/src/test/java/com/pt5/pthouduan/controller/CommentControllerTest.java b/src/test/java/com/pt5/pthouduan/controller/CommentControllerTest.java
new file mode 100644
index 0000000..055dacf
--- /dev/null
+++ b/src/test/java/com/pt5/pthouduan/controller/CommentControllerTest.java
@@ -0,0 +1,127 @@
+package com.pt5.pthouduan.controller;
+
+import com.pt5.pthouduan.entity.Comment;
+import com.pt5.pthouduan.service.CommentService;
+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.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.*;
+import static org.junit.jupiter.api.Assertions.*;
+
+class CommentControllerTest {
+
+ @Mock
+ private CommentService commentService;
+
+ @InjectMocks
+ private CommentController commentController;
+
+ private MockMvc mockMvc;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ mockMvc = MockMvcBuilders.standaloneSetup(commentController).build();
+ }
+
+ @Test
+ void createComment_ShouldReturnCreatedComment() {
+ Comment inputComment = new Comment();
+ inputComment.setPostid(1);
+ inputComment.setUserid(100L);
+ inputComment.setPostCommentcontent("Test Comment");
+ inputComment.setCommenttime(LocalDateTime.now());
+
+ Comment savedComment = new Comment();
+ savedComment.setCommentid(1);
+ savedComment.setPostid(1);
+ savedComment.setUserid(100L);
+ savedComment.setPostCommentcontent("Test Comment");
+ savedComment.setCommenttime(inputComment.getCommenttime());
+
+ when(commentService.createComment(any(Comment.class))).thenReturn(savedComment);
+
+ Comment result = commentController.createComment(inputComment);
+
+ assertNotNull(result);
+ assertEquals("Test Comment", result.getPostCommentcontent());
+ assertEquals(100L, result.getUserid());
+ }
+
+ @Test
+ void deleteComment_ShouldReturnTrue_WhenSuccess() {
+ when(commentService.deleteComment(1)).thenReturn(true);
+
+ boolean result = commentController.deleteComment(1);
+
+ assertTrue(result);
+ verify(commentService).deleteComment(1);
+ }
+
+ @Test
+ void updateComment_ShouldReturnTrue_WhenSuccess() {
+ Comment updatedComment = new Comment();
+ updatedComment.setCommentid(1);
+ updatedComment.setPostCommentcontent("Updated content");
+
+ when(commentService.updateComment(any(Comment.class))).thenReturn(true);
+
+ boolean result = commentController.updateComment(updatedComment);
+
+ assertTrue(result);
+ verify(commentService).updateComment(updatedComment);
+ }
+
+ @Test
+ void getCommentsByPostId_ShouldReturnListOfComments() {
+ Comment comment1 = new Comment();
+ comment1.setCommentid(1);
+ comment1.setPostid(10);
+ comment1.setPostCommentcontent("Comment 1");
+
+ Comment comment2 = new Comment();
+ comment2.setCommentid(2);
+ comment2.setPostid(10);
+ comment2.setPostCommentcontent("Comment 2");
+
+ List<Comment> commentList = Arrays.asList(comment1, comment2);
+
+ when(commentService.getCommentsByPostId(10)).thenReturn(commentList);
+
+ List<Comment> result = commentController.getCommentsByPostId(10);
+
+ assertEquals(2, result.size());
+ assertEquals("Comment 1", result.get(0).getPostCommentcontent());
+ }
+
+ @Test
+ void likeComment_ShouldReturnTrue_WhenSuccess() {
+ when(commentService.likeComment(1)).thenReturn(true);
+
+ boolean result = commentController.likeComment(1);
+
+ assertTrue(result);
+ verify(commentService).likeComment(1);
+ }
+
+ @Test
+ void unlikeComment_ShouldReturnTrue_WhenSuccess() {
+ when(commentService.unlikeComment(1)).thenReturn(true);
+
+ boolean result = commentController.unlikeComment(1);
+
+ assertTrue(result);
+ verify(commentService).unlikeComment(1);
+ }
+}
diff --git a/src/test/java/com/pt5/pthouduan/service/CommentServiceTest.java b/src/test/java/com/pt5/pthouduan/service/CommentServiceTest.java
new file mode 100644
index 0000000..bfce395
--- /dev/null
+++ b/src/test/java/com/pt5/pthouduan/service/CommentServiceTest.java
@@ -0,0 +1,100 @@
+package com.pt5.pthouduan.service;
+
+import com.pt5.pthouduan.entity.Comment;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.time.LocalDateTime;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+class CommentServiceTest {
+
+ @Mock
+ private CommentService commentService;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ }
+
+ @Test
+ void createComment_ShouldReturnCreatedComment() {
+ Comment newComment = new Comment();
+ newComment.setUserid(1L);
+ newComment.setPostid(10);
+ newComment.setPostCommentcontent("这是一个评论");
+ newComment.setCommenttime(LocalDateTime.now());
+
+ when(commentService.createComment(any(Comment.class))).thenReturn(newComment);
+
+ Comment result = commentService.createComment(newComment);
+
+ assertNotNull(result);
+ assertEquals("这是一个评论", result.getPostCommentcontent());
+ verify(commentService).createComment(newComment);
+ }
+
+ @Test
+ void deleteComment_ShouldReturnTrue() {
+ when(commentService.deleteComment(1)).thenReturn(true);
+
+ boolean result = commentService.deleteComment(1);
+
+ assertTrue(result);
+ verify(commentService).deleteComment(1);
+ }
+
+ @Test
+ void updateComment_ShouldReturnTrue() {
+ Comment comment = new Comment();
+ comment.setCommentid(1);
+ comment.setPostCommentcontent("更新的评论");
+
+ when(commentService.updateComment(comment)).thenReturn(true);
+
+ boolean result = commentService.updateComment(comment);
+
+ assertTrue(result);
+ verify(commentService).updateComment(comment);
+ }
+
+ @Test
+ void getCommentsByPostId_ShouldReturnCommentsList() {
+ Comment comment = new Comment();
+ comment.setPostid(10);
+ comment.setPostCommentcontent("测试评论");
+
+ when(commentService.getCommentsByPostId(10)).thenReturn(Collections.singletonList(comment));
+
+ List<Comment> result = commentService.getCommentsByPostId(10);
+
+ assertEquals(1, result.size());
+ assertEquals("测试评论", result.get(0).getPostCommentcontent());
+ }
+
+ @Test
+ void likeComment_ShouldReturnTrue() {
+ when(commentService.likeComment(1)).thenReturn(true);
+
+ boolean result = commentService.likeComment(1);
+
+ assertTrue(result);
+ verify(commentService).likeComment(1);
+ }
+
+ @Test
+ void unlikeComment_ShouldReturnTrue() {
+ when(commentService.unlikeComment(1)).thenReturn(true);
+
+ boolean result = commentService.unlikeComment(1);
+
+ assertTrue(result);
+ verify(commentService).unlikeComment(1);
+ }
+}