| import axios from 'axios'; |
| import type { Comment, CommentFormData, CommentReply, ReplyFormData } from './otherType'; |
| |
| const API_BASE_URL = '/api'; |
| |
| class CommentAPI { |
| /** |
| * 添加评论 |
| */ |
| static addComment(data: CommentFormData): Promise<Comment> { |
| return axios.post<Comment>(`${API_BASE_URL}/comments`, data) |
| .then(response => response.data); |
| } |
| |
| /** |
| * 删除评论 |
| */ |
| static deleteComment(commentId: number): Promise<void> { |
| return axios.delete(`${API_BASE_URL}/comments/${commentId}`); |
| } |
| |
| /** |
| * 更新评论 |
| */ |
| static updateComment(commentId: number, data: CommentFormData): Promise<Comment> { |
| return axios.put<Comment>(`${API_BASE_URL}/comments/${commentId}`, data) |
| .then(response => response.data); |
| } |
| |
| /** |
| * 获取单个评论 |
| */ |
| static getCommentById(commentId: number): Promise<Comment> { |
| return axios.get<Comment>(`${API_BASE_URL}/comments/${commentId}`) |
| .then(response => response.data); |
| } |
| |
| /** |
| * 获取帖子下的所有评论 |
| */ |
| static getCommentsByPostId(postId: number): Promise<Comment[]> { |
| return axios.get<Comment[]>(`${API_BASE_URL}/comments/post/${postId}`) |
| .then(response => response.data); |
| } |
| |
| /** |
| * 添加回复 |
| */ |
| static addReply(commentId: number, data: ReplyFormData): Promise<CommentReply> { |
| return axios.post<CommentReply>(`${API_BASE_URL}/comments/${commentId}/replies`, data) |
| .then(response => response.data); |
| } |
| |
| /** |
| * 获取评论的所有回复 |
| */ |
| static getReplies(commentId: number): Promise<CommentReply[]> { |
| return axios.get<CommentReply[]>(`${API_BASE_URL}/comments/${commentId}/replies`) |
| .then(response => response.data); |
| } |
| |
| /** |
| * 点赞评论 |
| */ |
| static likeComment(commentId: number): Promise<void> { |
| return axios.post(`${API_BASE_URL}/comments/${commentId}/like`); |
| } |
| |
| /** |
| * 举报评论 |
| */ |
| static reportComment(commentId: number, reason: string): Promise<void> { |
| return axios.post(`${API_BASE_URL}/comments/${commentId}/report`, { reason }); |
| } |
| } |
| |
| export default CommentAPI; |