Spaces:
Running
Running
Upload 2 files
Browse files
post.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Blueprint, request, jsonify
|
2 |
+
from src.models.user import db
|
3 |
+
from src.models.post import Post, Comment
|
4 |
+
|
5 |
+
post_bp = Blueprint('post', __name__)
|
6 |
+
|
7 |
+
@post_bp.route('/posts', methods=['GET'])
|
8 |
+
def get_posts():
|
9 |
+
"""ดึงโพสต์ทั้งหมด"""
|
10 |
+
posts = Post.query.order_by(Post.created_at.desc()).all()
|
11 |
+
return jsonify([post.to_dict() for post in posts])
|
12 |
+
|
13 |
+
@post_bp.route('/posts', methods=['POST'])
|
14 |
+
def create_post():
|
15 |
+
"""สร้างโพสต์ใหม่"""
|
16 |
+
data = request.get_json()
|
17 |
+
|
18 |
+
if not data or not data.get('name') or not data.get('note'):
|
19 |
+
return jsonify({'error': 'Name and note are required'}), 400
|
20 |
+
|
21 |
+
post = Post(
|
22 |
+
name=data['name'],
|
23 |
+
note=data['note'],
|
24 |
+
youtube_link=data.get('youtube_link', '')
|
25 |
+
)
|
26 |
+
|
27 |
+
db.session.add(post)
|
28 |
+
db.session.commit()
|
29 |
+
|
30 |
+
return jsonify(post.to_dict()), 201
|
31 |
+
|
32 |
+
@post_bp.route('/posts/<int:post_id>/like', methods=['POST'])
|
33 |
+
def like_post(post_id):
|
34 |
+
"""ถูกใจโพสต์"""
|
35 |
+
post = Post.query.get_or_404(post_id)
|
36 |
+
post.likes += 1
|
37 |
+
db.session.commit()
|
38 |
+
|
39 |
+
return jsonify({'likes': post.likes})
|
40 |
+
|
41 |
+
@post_bp.route('/posts/<int:post_id>/comments', methods=['GET'])
|
42 |
+
def get_comments(post_id):
|
43 |
+
"""ดึงคอมเมนต์ของโพสต์"""
|
44 |
+
comments = Comment.query.filter_by(post_id=post_id).order_by(Comment.created_at.asc()).all()
|
45 |
+
return jsonify([comment.to_dict() for comment in comments])
|
46 |
+
|
47 |
+
@post_bp.route('/posts/<int:post_id>/comments', methods=['POST'])
|
48 |
+
def add_comment(post_id):
|
49 |
+
"""เพิ่มคอมเมนต์ในโพสต์"""
|
50 |
+
data = request.get_json()
|
51 |
+
|
52 |
+
if not data or not data.get('name') or not data.get('comment'):
|
53 |
+
return jsonify({'error': 'Name and comment are required'}), 400
|
54 |
+
|
55 |
+
# ตรวจสอบว่าโพสต์มีอยู่จริง
|
56 |
+
post = Post.query.get_or_404(post_id)
|
57 |
+
|
58 |
+
comment = Comment(
|
59 |
+
post_id=post_id,
|
60 |
+
name=data['name'],
|
61 |
+
comment=data['comment']
|
62 |
+
)
|
63 |
+
|
64 |
+
db.session.add(comment)
|
65 |
+
db.session.commit()
|
66 |
+
|
67 |
+
return jsonify(comment.to_dict()), 201
|
68 |
+
|
user.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Blueprint, jsonify, request
|
2 |
+
from src.models.user import User, db
|
3 |
+
|
4 |
+
user_bp = Blueprint('user', __name__)
|
5 |
+
|
6 |
+
@user_bp.route('/users', methods=['GET'])
|
7 |
+
def get_users():
|
8 |
+
users = User.query.all()
|
9 |
+
return jsonify([user.to_dict() for user in users])
|
10 |
+
|
11 |
+
@user_bp.route('/users', methods=['POST'])
|
12 |
+
def create_user():
|
13 |
+
|
14 |
+
data = request.json
|
15 |
+
user = User(username=data['username'], email=data['email'])
|
16 |
+
db.session.add(user)
|
17 |
+
db.session.commit()
|
18 |
+
return jsonify(user.to_dict()), 201
|
19 |
+
|
20 |
+
@user_bp.route('/users/<int:user_id>', methods=['GET'])
|
21 |
+
def get_user(user_id):
|
22 |
+
user = User.query.get_or_404(user_id)
|
23 |
+
return jsonify(user.to_dict())
|
24 |
+
|
25 |
+
@user_bp.route('/users/<int:user_id>', methods=['PUT'])
|
26 |
+
def update_user(user_id):
|
27 |
+
user = User.query.get_or_404(user_id)
|
28 |
+
data = request.json
|
29 |
+
user.username = data.get('username', user.username)
|
30 |
+
user.email = data.get('email', user.email)
|
31 |
+
db.session.commit()
|
32 |
+
return jsonify(user.to_dict())
|
33 |
+
|
34 |
+
@user_bp.route('/users/<int:user_id>', methods=['DELETE'])
|
35 |
+
def delete_user(user_id):
|
36 |
+
user = User.query.get_or_404(user_id)
|
37 |
+
db.session.delete(user)
|
38 |
+
db.session.commit()
|
39 |
+
return '', 204
|