From 241c962fafdc94175d54dfa94f30ccd74d7d62c5 Mon Sep 17 00:00:00 2001 From: Josh-Dev-Quest Date: Sat, 28 Mar 2026 14:25:13 +0100 Subject: [PATCH] First comment function implementation --- backend/app.py | 83 +++++- backend/data/users.json | 6 + backend/data/users/1/posts.json | 11 +- backend/data/users/2/journeys.json | 1 + backend/data/users/2/posts.json | 1 + blog-list.html | 4 +- blog-post.html | 403 +++++++++++++++++++++++++++++ 7 files changed, 502 insertions(+), 7 deletions(-) create mode 100644 backend/data/users/2/journeys.json create mode 100644 backend/data/users/2/posts.json create mode 100644 blog-post.html diff --git a/backend/app.py b/backend/app.py index f3a62e4..3e20377 100644 --- a/backend/app.py +++ b/backend/app.py @@ -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//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//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/', 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(): diff --git a/backend/data/users.json b/backend/data/users.json index 537b8d6..8932acf 100644 --- a/backend/data/users.json +++ b/backend/data/users.json @@ -4,5 +4,11 @@ "username": "josh", "password_hash": "scrypt:32768:8:1$HA70PiOwbBrIwlDq$2ab80bdc08bb3bb4214258566aded836062323380491a7f4c7f2e67bdccb8686367789f57b3c6c5eb3e2f08c8c07186f47f9c89d1e72179ddd3758b509f23fbe", "created_at": "2026-03-27T20:32:43.107028" + }, + { + "id": 2, + "username": "test1", + "password_hash": "scrypt:32768:8:1$hPfITQadZq8438bv$38262bf82d93c596a82a1b052a4ba72f8d6729b796ca5273faa7dd47b409112959c4501e77922605a1f3a7ef08e68fa545ce03818eb82e6fb2503cc817c43e2a", + "created_at": "2026-03-28T14:13:32.860143" } ] \ No newline at end of file diff --git a/backend/data/users/1/posts.json b/backend/data/users/1/posts.json index 1763773..35f66cc 100644 --- a/backend/data/users/1/posts.json +++ b/backend/data/users/1/posts.json @@ -5,6 +5,15 @@ "content": "ksafladjsfk", "journeyId": "1", "image": null, - "created_at": "2026-03-27T21:23:39.755057" + "created_at": "2026-03-27T21:23:39.755057", + "comments": [ + { + "id": 1774703592361, + "author_id": 1, + "author_name": "josh", + "text": "hello", + "created_at": "2026-03-28T14:13:12.362078" + } + ] } ] \ No newline at end of file diff --git a/backend/data/users/2/journeys.json b/backend/data/users/2/journeys.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/backend/data/users/2/journeys.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/backend/data/users/2/posts.json b/backend/data/users/2/posts.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/backend/data/users/2/posts.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/blog-list.html b/blog-list.html index 5878475..eb3790f 100644 --- a/blog-list.html +++ b/blog-list.html @@ -212,7 +212,7 @@

Blog Posts

- New Post + New Post
@@ -247,7 +247,7 @@
${post.image ? `${post.title}` : '
'}
-

${escapeHtml(post.title)}

+

${escapeHtml(post.title)}

${new Date(post.created_at).toLocaleDateString()} ${post.journeyId ? ` Journey #${post.journeyId}` : ''} diff --git a/blog-post.html b/blog-post.html new file mode 100644 index 0000000..d5e2938 --- /dev/null +++ b/blog-post.html @@ -0,0 +1,403 @@ + + + + + + Blog Post – Journey Mapper + + + + + + + + + + + +
+
+
+
+ +
+ + + + + \ No newline at end of file