帖子的评论的前端

Change-Id: Ida12e1874052498f22e40d0604e936f0d65d5fb6
diff --git a/src/api/__tests__/comment.test.js b/src/api/__tests__/comment.test.js
new file mode 100644
index 0000000..a55dba7
--- /dev/null
+++ b/src/api/__tests__/comment.test.js
@@ -0,0 +1,83 @@
+// src/api/__tests__/comment.test.js
+const axios = require('axios');
+jest.mock('axios');
+
+const {
+    createComment,
+    deleteComment,
+    updateComment,
+    getCommentsByPostId,
+    likeComment,
+    unlikeComment
+} = require('../comment');
+
+describe('Comment API Tests', () => {
+    beforeEach(() => {
+        jest.clearAllMocks();
+    });
+
+    test('createComment should send POST request', async () => {
+        const mockData = { userid: 1, postid: 2, postCommentcontent: 'Test comment' };
+        axios.post.mockResolvedValue({ data: mockData });
+
+        const response = await createComment(mockData);
+
+        expect(axios.post).toHaveBeenCalledWith('http://localhost:8080/comment/create', mockData);
+        expect(response.data).toEqual(mockData);
+    });
+
+    test('deleteComment should send DELETE request', async () => {
+        axios.delete.mockResolvedValue({ data: true });
+
+        const response = await deleteComment(123);
+
+        expect(axios.delete).toHaveBeenCalledWith('http://localhost:8080/comment/delete/123');
+        expect(response.data).toBe(true);
+    });
+
+    test('updateComment should send PUT request', async () => {
+        const updatedData = { commentid: 1, postCommentcontent: 'Updated comment' };
+        axios.put.mockResolvedValue({ data: true });
+
+        const response = await updateComment(updatedData);
+
+        expect(axios.put).toHaveBeenCalledWith('http://localhost:8080/comment/update', updatedData);
+        expect(response.data).toBe(true);
+    });
+
+    test('getCommentsByPostId should fetch comments by post ID', async () => {
+        const mockResponse = { data: [{ commentid: 1, postCommentcontent: 'Nice!' }] };
+        axios.get.mockResolvedValue(mockResponse);
+
+        const response = await getCommentsByPostId(5);
+
+        expect(axios.get).toHaveBeenCalledWith('http://localhost:8080/comment/post/5');
+        expect(response).toEqual(mockResponse.data);
+    });
+
+    test('getCommentsByPostId should return empty array on error', async () => {
+        axios.get.mockRejectedValue(new Error('Network Error'));
+
+        const response = await getCommentsByPostId(99);
+
+        expect(response).toEqual([]);
+    });
+
+    test('likeComment should send POST request', async () => {
+        axios.post.mockResolvedValue({ data: true });
+
+        const response = await likeComment(10);
+
+        expect(axios.post).toHaveBeenCalledWith('http://localhost:8080/comment/like/10');
+        expect(response.data).toBe(true);
+    });
+
+    test('unlikeComment should send POST request', async () => {
+        axios.post.mockResolvedValue({ data: true });
+
+        const response = await unlikeComment(10);
+
+        expect(axios.post).toHaveBeenCalledWith('http://localhost:8080/comment/unlike/10');
+        expect(response.data).toBe(true);
+    });
+});
diff --git a/src/api/comment.js b/src/api/comment.js
new file mode 100644
index 0000000..28b2b4e
--- /dev/null
+++ b/src/api/comment.js
@@ -0,0 +1,40 @@
+import axios from 'axios';
+
+const BASE_URL = 'http://localhost:8080/comment';
+
+// 创建评论
+export const createComment = (commentData) => {
+    return axios.post(`${BASE_URL}/create`, commentData);
+};
+
+// 删除评论
+export const deleteComment = (commentId) => {
+    return axios.delete(`${BASE_URL}/delete/${commentId}`);
+};
+
+// 更新评论
+export const updateComment = (commentData) => {
+    return axios.put(`${BASE_URL}/update`, commentData);
+};
+
+// 获取某个帖子的所有评论
+// comment.js
+export async function getCommentsByPostId(postid) {
+    try {
+        const response = await axios.get(`${BASE_URL}/post/${postid}`);
+        return Array.isArray(response.data) ? response.data : []; // 确保返回数据是数组
+    } catch (error) {
+        console.error('获取评论失败', error);
+        return [];
+    }
+}
+
+// 点赞评论
+export const likeComment = (commentId) => {
+    return axios.post(`${BASE_URL}/like/${commentId}`);
+};
+
+// 取消点赞评论
+export const unlikeComment = (commentId) => {
+    return axios.post(`${BASE_URL}/unlike/${commentId}`);
+};