blob: 14ff64c75542b1b9b81fac4c3034ca190ccb0c0c [file] [log] [blame]
TRM-codingd1cbf672025-06-18 15:15:08 +08001# routes/posts.py
wu90da17b2025-06-19 12:45:29 +08002
TRM-codingd1cbf672025-06-18 15:15:08 +08003from flask import Blueprint, request, jsonify, abort
4from extensions import db
5from models.post import Post
6from models.behavior import Behavior
7
8posts_bp = Blueprint('posts', __name__)
9
10@posts_bp.route('', methods=['POST'])
11def create_post():
12 data = request.get_json() or {}
13 post = Post(**data)
14 db.session.add(post)
15 db.session.commit()
16 return jsonify({'id': post.id}), 201
17
18@posts_bp.route('', methods=['GET'])
19def list_posts():
wu90da17b2025-06-19 12:45:29 +080020 """
21 获取帖子列表,支持:
22 - GET /posts 返回所有已发布帖子
23 - GET /posts?user_id=123 返回指定用户 user_id 的所有帖子
24 """
25 user_id = request.args.get('user_id', type=int)
26 query = Post.query
27 if user_id is not None:
28 query = query.filter_by(user_id=user_id)
29 else:
30 query = query.filter_by(status='published')
31
32 posts = query.all()
33
TRM-codingd1cbf672025-06-18 15:15:08 +080034 return jsonify([{
35 'id': p.id,
36 'title': p.title,
wu90da17b2025-06-19 12:45:29 +080037 'status': p.status, # 新增 status 字段
TRM-codingd1cbf672025-06-18 15:15:08 +080038 'heat': p.heat,
39 'created_at': p.created_at.isoformat()
40 } for p in posts])
41
42@posts_bp.route('/<int:post_id>', methods=['GET'])
43def get_post(post_id):
44 post = Post.query.get_or_404(post_id)
45 return jsonify({
46 'id': post.id,
47 'user_id': post.user_id,
wu90da17b2025-06-19 12:45:29 +080048 'topic_id': post.topic_id,
TRM-codingd1cbf672025-06-18 15:15:08 +080049 'title': post.title,
50 'content': post.content,
51 'media_urls': post.media_urls,
52 'status': post.status,
53 'heat': post.heat,
54 'created_at': post.created_at.isoformat(),
55 'updated_at': post.updated_at.isoformat()
56 })
57
58@posts_bp.route('/<int:post_id>', methods=['PUT'])
59def update_post(post_id):
60 """
wu90da17b2025-06-19 12:45:29 +080061 修改帖子字段(可选字段:title, content, topic_id, media_urls, status)
TRM-codingd1cbf672025-06-18 15:15:08 +080062 """
63 post = Post.query.get_or_404(post_id)
64 data = request.get_json() or {}
wu90da17b2025-06-19 12:45:29 +080065 for key in ('title', 'content', 'topic_id', 'media_urls', 'status'):
TRM-codingd1cbf672025-06-18 15:15:08 +080066 if key in data:
67 setattr(post, key, data[key])
68 db.session.commit()
69 return '', 204
70
71@posts_bp.route('/<int:post_id>', methods=['DELETE'])
72def delete_post(post_id):
73 post = Post.query.get_or_404(post_id)
74 db.session.delete(post)
75 db.session.commit()
76 return '', 204
77
TRM-codingd1cbf672025-06-18 15:15:08 +080078@posts_bp.route('/<int:post_id>/<action>', methods=['POST'])
79def post_action(post_id, action):
80 """
81 支持的 action: like, favorite, view, share
82 对于 like 和 favorite,保证每个用户每帖只做一次。
83 """
84 if action not in ('like', 'favorite', 'view', 'share'):
85 abort(400, 'Invalid action')
86
87 data = request.get_json() or {}
88 user_id = data.get('user_id')
89 if not user_id:
90 abort(400, 'user_id required')
91
92 # 对 like/favorite 做去重检查
93 if action in ('like', 'favorite'):
94 exists = Behavior.query.filter_by(
95 user_id=user_id,
96 post_id=post_id,
97 type=action
98 ).first()
99 if exists:
100 return jsonify({'error': f'already {action}d'}), 400
101
102 # 创建行为记录
103 beh = Behavior(user_id=user_id, post_id=post_id, type=action)
104 db.session.add(beh)
105
106 # 更新热度
107 post = Post.query.get_or_404(post_id)
108 post.heat += 1
109
110 db.session.commit()
111 return '', 201
112
TRM-codingd1cbf672025-06-18 15:15:08 +0800113@posts_bp.route('/<int:post_id>/like', methods=['DELETE'])
114def unlike(post_id):
115 user_id = request.get_json(silent=True) and request.get_json().get('user_id')
116 if not user_id:
117 abort(400, 'user_id required')
118 # 查找已有的 like 行为
119 beh = Behavior.query.filter_by(
120 user_id=user_id,
121 post_id=post_id,
122 type='like'
123 ).first()
124 if not beh:
125 return jsonify({'error': 'not liked yet'}), 400
126
127 db.session.delete(beh)
128 # 更新热度,确保不降到负数
129 post = Post.query.get_or_404(post_id)
130 post.heat = max(post.heat - 1, 0)
131 db.session.commit()
132 return '', 204
133
TRM-codingd1cbf672025-06-18 15:15:08 +0800134@posts_bp.route('/<int:post_id>/favorite', methods=['DELETE'])
135def unfavorite(post_id):
136 user_id = request.get_json(silent=True) and request.get_json().get('user_id')
137 if not user_id:
138 abort(400, 'user_id required')
139 # 查找已有的 favorite 行为
140 beh = Behavior.query.filter_by(
141 user_id=user_id,
142 post_id=post_id,
143 type='favorite'
144 ).first()
145 if not beh:
146 return jsonify({'error': 'not favorited yet'}), 400
147
148 db.session.delete(beh)
149 # 更新热度
150 post = Post.query.get_or_404(post_id)
151 post.heat = max(post.heat - 1, 0)
152 db.session.commit()
153 return '', 204