blob: 360e659177c3b20eac33631c08052941921ab9c3 [file] [log] [blame]
223010143d966302025-06-07 22:54:40 +08001import axios from 'axios';
2import type { Comment, CommentFormData, CommentReply, ReplyFormData } from './otherType';
3
4const API_BASE_URL = '/api';
5
6class CommentAPI {
7 /**
8 * 添加评论
9 */
10 static addComment(data: CommentFormData): Promise<Comment> {
11 return axios.post<Comment>(`${API_BASE_URL}/comments`, data)
12 .then(response => response.data);
13 }
14
15 /**
16 * 删除评论
17 */
18 static deleteComment(commentId: number): Promise<void> {
19 return axios.delete(`${API_BASE_URL}/comments/${commentId}`);
20 }
21
22 /**
23 * 更新评论
24 */
25 static updateComment(commentId: number, data: CommentFormData): Promise<Comment> {
26 return axios.put<Comment>(`${API_BASE_URL}/comments/${commentId}`, data)
27 .then(response => response.data);
28 }
29
30 /**
31 * 获取单个评论
32 */
33 static getCommentById(commentId: number): Promise<Comment> {
34 return axios.get<Comment>(`${API_BASE_URL}/comments/${commentId}`)
35 .then(response => response.data);
36 }
37
38 /**
39 * 获取帖子下的所有评论
40 */
41 static getCommentsByPostId(postId: number): Promise<Comment[]> {
42 return axios.get<Comment[]>(`${API_BASE_URL}/comments/post/${postId}`)
43 .then(response => response.data);
44 }
45
46 /**
47 * 添加回复
48 */
49 static addReply(commentId: number, data: ReplyFormData): Promise<CommentReply> {
50 return axios.post<CommentReply>(`${API_BASE_URL}/comments/${commentId}/replies`, data)
51 .then(response => response.data);
52 }
53
54 /**
55 * 获取评论的所有回复
56 */
57 static getReplies(commentId: number): Promise<CommentReply[]> {
58 return axios.get<CommentReply[]>(`${API_BASE_URL}/comments/${commentId}/replies`)
59 .then(response => response.data);
60 }
61
62 /**
63 * 点赞评论
64 */
65 static likeComment(commentId: number): Promise<void> {
66 return axios.post(`${API_BASE_URL}/comments/${commentId}/like`);
67 }
68
69 /**
70 * 举报评论
71 */
72 static reportComment(commentId: number, reason: string): Promise<void> {
73 return axios.post(`${API_BASE_URL}/comments/${commentId}/report`, { reason });
74 }
75}
76
77export default CommentAPI;