22301014 | 3d96630 | 2025-06-07 22:54:40 +0800 | [diff] [blame] | 1 | import axios from 'axios'; |
| 2 | import type { Comment, CommentFormData, CommentReply, ReplyFormData } from './otherType'; |
| 3 | |
| 4 | const API_BASE_URL = '/api'; |
| 5 | |
| 6 | class 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 | |
| 77 | export default CommentAPI; |