blob: 89fe7e5ab3ce9c61a47f2156e888d00115afa824 [file] [log] [blame]
wua80b90d2025-06-15 10:36:02 +08001// src/api/posts.js
2const BASE = 'http://127.0.0.1:5000/' // 如果有代理可以留空,否则填完整域名,如 'http://localhost:3000'
3
4/**
5 * 获取所有已发布的帖子列表
6 * GET /posts
7 */
8export async function fetchPosts() {
9 const res = await fetch(`${BASE}/posts`)
10 if (!res.ok) throw new Error(`fetchPosts: ${res.status}`)
11 return res.json() // 返回 [ { id, title, heat, created_at }, … ]
12}
13
14/**
15 * 查看单个帖子详情
16 * GET /posts/{postId}
17 */
18export async function fetchPost(postId) {
19 const res = await fetch(`${BASE}/posts/${postId}`)
20 if (!res.ok) throw new Error(`fetchPost(${postId}): ${res.status}`)
21 return res.json() // 返回完整的帖子对象
22}
23
24/**
25 * 发布新帖
26 * POST /posts
27 */
28export async function createPost(payload) {
29 const res = await fetch(`${BASE}/posts`, {
30 method: 'POST',
31 headers: { 'Content-Type': 'application/json' },
32 body: JSON.stringify(payload)
33 })
34 if (!res.ok) {
35 const err = await res.json().catch(() => null)
36 throw new Error(err?.error || `createPost: ${res.status}`)
37 }
38 return res.json() // { id }
39}
40
41/**
42 * 修改帖子
43 * PUT /posts/{postId}
44 */
45export async function updatePost(postId, payload) {
46 const res = await fetch(`${BASE}/posts/${postId}`, {
47 method: 'PUT',
48 headers: { 'Content-Type': 'application/json' },
49 body: JSON.stringify(payload)
50 })
51 if (!res.ok) throw new Error(`updatePost(${postId}): ${res.status}`)
52 // 204 No Content
53}
54
55/**
56 * 删除帖子
57 * DELETE /posts/{postId}
58 */
59export async function deletePost(postId) {
60 const res = await fetch(`${BASE}/posts/${postId}`, {
61 method: 'DELETE'
62 })
63 if (!res.ok) throw new Error(`deletePost(${postId}): ${res.status}`)
64}
65
66/**
67 * 点赞
68 * POST /posts/{postId}/like
69 */
70export async function likePost(postId, userId) {
71 const res = await fetch(`${BASE}/posts/${postId}/like`, {
72 method: 'POST',
73 headers: { 'Content-Type': 'application/json' },
74 body: JSON.stringify({ user_id: userId })
75 })
76 if (!res.ok) {
77 const err = await res.json().catch(() => null)
78 throw new Error(err?.error || `likePost: ${res.status}`)
79 }
80}
81
82/**
83 * 取消点赞
84 * DELETE /posts/{postId}/like
85 */
86export async function unlikePost(postId, userId) {
87 const res = await fetch(`${BASE}/posts/${postId}/like`, {
88 method: 'DELETE',
89 headers: { 'Content-Type': 'application/json' },
90 body: JSON.stringify({ user_id: userId })
91 })
92 if (!res.ok) {
93 const err = await res.json().catch(() => null)
94 throw new Error(err?.error || `unlikePost: ${res.status}`)
95 }
96}
97
98/**
99 * 收藏、取消收藏、浏览、分享 等接口:
100 * POST /posts/{postId}/favorite
101 * DELETE /posts/{postId}/favorite
102 * POST /posts/{postId}/view
103 * POST /posts/{postId}/share
104 * 用法同上,替换路径即可
105 */
106
107/**
108 * 添加评论
109 * POST /posts/{postId}/comments
110 */
111export async function addComment(postId, payload) {
112 const res = await fetch(`${BASE}/posts/${postId}/comments`, {
113 method: 'POST',
114 headers: { 'Content-Type': 'application/json' },
115 body: JSON.stringify(payload)
116 })
117 if (!res.ok) throw new Error(`addComment: ${res.status}`)
118 return res.json() // { id }
119}
120
121/**
122 * 获取评论列表
123 * GET /posts/{postId}/comments
124 */
125export async function fetchComments(postId) {
126 const res = await fetch(`${BASE}/posts/${postId}/comments`)
127 if (!res.ok) throw new Error(`fetchComments: ${res.status}`)
128 return res.json()
129}