Reworked blog overview website
This commit is contained in:
5 files changed
+672
-1
No files matched your search
@@ -134,5 +134,86 @@ def index():
|
||||
</html>
|
||||
'''
|
||||
|
||||
|
||||
# Blog posts data file
|
||||
BLOG_DATA_FILE = os.path.join(DATA_DIR, 'blog_posts.json')
|
||||
|
||||
def load_blog_posts():
|
||||
try:
|
||||
if os.path.exists(BLOG_DATA_FILE):
|
||||
with open(BLOG_DATA_FILE, 'r') as f:
|
||||
return json.load(f)
|
||||
except:
|
||||
pass
|
||||
return []
|
||||
|
||||
def save_blog_posts(posts):
|
||||
with open(BLOG_DATA_FILE, 'w') as f:
|
||||
json.dump(posts, f, indent=2)
|
||||
|
||||
blog_posts = load_blog_posts()
|
||||
|
||||
def get_next_blog_id():
|
||||
if not blog_posts:
|
||||
return 1
|
||||
return max(p['id'] for p in blog_posts) + 1
|
||||
|
||||
@app.route('/api/blog-posts', methods=['GET'])
|
||||
def get_blog_posts():
|
||||
return jsonify(blog_posts)
|
||||
|
||||
@app.route('/api/blog-posts/<int:post_id>', methods=['GET'])
|
||||
def get_blog_post(post_id):
|
||||
post = next((p for p in blog_posts if p['id'] == post_id), None)
|
||||
if not post:
|
||||
return jsonify({'error': 'Post not found'}), 404
|
||||
return jsonify(post)
|
||||
|
||||
@app.route('/api/blog-posts', methods=['POST'])
|
||||
def create_blog_post():
|
||||
data = request.get_json()
|
||||
title = data.get('title')
|
||||
if not title:
|
||||
return jsonify({'error': 'Title required'}), 400
|
||||
new_post = {
|
||||
'id': get_next_blog_id(),
|
||||
'title': title,
|
||||
'content': data.get('content', ''),
|
||||
'journeyId': data.get('journeyId'),
|
||||
'image': data.get('image'),
|
||||
'created_at': datetime.now().isoformat()
|
||||
}
|
||||
blog_posts.append(new_post)
|
||||
save_blog_posts(blog_posts)
|
||||
return jsonify(new_post), 201
|
||||
|
||||
@app.route('/api/blog-posts/<int:post_id>', methods=['PUT'])
|
||||
def update_blog_post(post_id):
|
||||
post = next((p for p in blog_posts if p['id'] == post_id), None)
|
||||
if not post:
|
||||
return jsonify({'error': 'Post not found'}), 404
|
||||
data = request.get_json()
|
||||
if 'title' in data:
|
||||
post['title'] = data['title']
|
||||
if 'content' in data:
|
||||
post['content'] = data['content']
|
||||
if 'journeyId' in data:
|
||||
post['journeyId'] = data['journeyId']
|
||||
if 'image' in data:
|
||||
post['image'] = data['image']
|
||||
save_blog_posts(blog_posts)
|
||||
return jsonify(post)
|
||||
|
||||
@app.route('/api/blog-posts/<int:post_id>', methods=['DELETE'])
|
||||
def delete_blog_post(post_id):
|
||||
global blog_posts
|
||||
post = next((p for p in blog_posts if p['id'] == post_id), None)
|
||||
if not post:
|
||||
return jsonify({'error': 'Post not found'}), 404
|
||||
blog_posts = [p for p in blog_posts if p['id'] != post_id]
|
||||
save_blog_posts(blog_posts)
|
||||
return jsonify({'message': 'Post deleted'})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, port=5000)
|
||||
@@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"id": 1,
|
||||
"title": "test",
|
||||
"content": "sfsfsfsaf",
|
||||
"journeyId": "1",
|
||||
"image": null,
|
||||
"created_at": "2026-03-27T19:49:49.410806"
|
||||
}
|
||||
]
|
||||
Reference in new issue
Block a user