First comment function implementation

This commit is contained in:
Josh-Dev-Quest committed 2026-03-28 14:25:13 +01:00
1 parent e3724a4842
commit 241c962faf
7 files changed
+502 -7

No files matched your search

+79 -4
View File
@@ -1,9 +1,10 @@
import os
import time
import json
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from flask import Flask, request, jsonify, session
from flask_cors import CORS
from werkzeug.security import generate_password_hash, check_password_hash
import json
import os
from datetime import datetime
app = Flask(__name__)
app.secret_key = "your-secret-key-here-change-in-production" # needed for sessions
@@ -333,6 +334,7 @@ def create_blog_post():
"content": data.get("content", ""),
"journeyId": data.get("journeyId"),
"image": data.get("image"),
"author_id": user_id,
"created_at": datetime.now().isoformat(),
}
@@ -351,6 +353,8 @@ def update_blog_post(post_id):
return jsonify({"error": "Post not found"}), 404
data = request.get_json()
if not get_current_user_id() == data["author_id"]:
return jsonify({"error": "Wrong user"})
if "title" in data:
post["title"] = data["title"]
if "content" in data:
@@ -378,6 +382,77 @@ def delete_blog_post(post_id):
return jsonify({"message": "Post deleted"})
# ==================== Comments (stored inside posts) ====================
def get_post_by_id(user_id, post_id):
posts = load_user_posts(user_id)
return next((p for p in posts if p['id'] == post_id), None)
def save_post(user_id, post):
posts = load_user_posts(user_id)
for i, p in enumerate(posts):
if p['id'] == post['id']:
posts[i] = post
break
save_user_posts(user_id, posts)
@app.route('/api/posts/<int:post_id>/comments', methods=['GET'])
def get_comments(post_id):
user_id = session.get('user_id')
if not user_id:
return jsonify({'error': 'Authentication required'}), 401
post = get_post_by_id(user_id, post_id)
if not post:
return jsonify({'error': 'Post not found'}), 404
return jsonify(post.get('comments', []))
@app.route('/api/posts/<int:post_id>/comments', methods=['POST'])
def add_comment(post_id):
user_id = session.get('user_id')
if not user_id:
return jsonify({'error': 'Authentication required'}), 401
data = request.get_json()
text = data.get('text')
if not text:
return jsonify({'error': 'Comment text required'}), 400
post = get_post_by_id(user_id, post_id)
if not post:
return jsonify({'error': 'Post not found'}), 404
comment = {
'id': int(time.time() * 1000), # simple unique id
'author_id': user_id,
'author_name': get_user_by_id(user_id)['username'],
'text': text,
'created_at': datetime.now().isoformat()
}
if 'comments' not in post:
post['comments'] = []
post['comments'].append(comment)
save_post(user_id, post)
return jsonify(comment), 201
@app.route('/api/comments/<int:comment_id>', methods=['DELETE'])
def delete_comment(comment_id):
user_id = session.get('user_id')
if not user_id:
return jsonify({'error': 'Authentication required'}), 401
# Find which post contains this comment
posts = load_user_posts(user_id)
for post in posts:
if 'comments' in post:
for i, c in enumerate(post['comments']):
if c['id'] == comment_id:
# Allow deletion if current user is comment author or post author
if c['author_id'] == user_id or post['id'] == post.get('author_id', user_id):
del post['comments'][i]
save_post(user_id, post)
return jsonify({'message': 'Comment deleted'})
else:
return jsonify({'error': 'Not authorized'}), 403
return jsonify({'error': 'Comment not found'}), 404
# ==================== Health and root ====================
@app.route("/api/journeys/health", methods=["GET"])
def health_check():