From 54a72f6f6dbe13059655aa1f67726331cf2d1291 Mon Sep 17 00:00:00 2001 From: Abhishek Mallick Date: Tue, 29 Oct 2024 04:49:48 +0530 Subject: [PATCH] error handling in api/handlers/posts --- server/api/handlers/posts/posts.py | 142 +++++++++++++++++++---------- 1 file changed, 96 insertions(+), 46 deletions(-) diff --git a/server/api/handlers/posts/posts.py b/server/api/handlers/posts/posts.py index 11eb6ac..6a03d45 100644 --- a/server/api/handlers/posts/posts.py +++ b/server/api/handlers/posts/posts.py @@ -6,18 +6,22 @@ from extensions import posts_collection, comments_collection def get_posts(): - posts = list(posts_collection.find().sort("created_at", -1)) - for post in posts: - post['_id'] = str(post['_id']) - - comments = list(comments_collection.find({"post_id": ObjectId(post['_id'])})) - for comment in comments: - comment['_id'] = str(comment['_id']) - comment['post_id'] = str(comment['post_id']) - comment['user_username'] = str(comment['user_username']) - post['comments'] = comments - - return jsonify(posts), 200 + try: + posts = list(posts_collection.find().sort("created_at", -1)) + for post in posts: + post['_id'] = str(post['_id']) + + comments = list(comments_collection.find({"post_id": ObjectId(post['_id'])})) + for comment in comments: + comment['_id'] = str(comment['_id']) + comment['post_id'] = str(comment['post_id']) + comment['user_username'] = str(comment['user_username']) + post['comments'] = comments + + return jsonify(posts), 200 + except Exception as e: + logging.error(f"Error retrieving posts: {str(e)}") + return jsonify({"error": "Failed to retrieve posts"}), 500 def upload_image_to_cloudinary(image): try: @@ -28,48 +32,94 @@ def upload_image_to_cloudinary(image): return None def create_post(): - data = request.form - author_username = data['author_username'] - description = data['description'] - tags = data.getlist('tags') - image_url = None - - if 'image' in request.files: - image_file = request.files['image'] - if image_file.filename != '': - image_url = upload_image_to_cloudinary(image_file) - if image_url is None: - return jsonify({'error': 'Image upload failed'}), 500 - - post = Post.create_post(author_username, description, tags, image_url) - return jsonify({"post_id": str(post.inserted_id)}), 201 + try: + data = request.form + author_username = data.get('author_username') + description = data.get('description') + tags = data.getlist('tags') + + if not author_username or not description: + return jsonify({'error': 'Author username and description are required'}), 400 + + image_url = None + if 'image' in request.files: + image_file = request.files['image'] + if image_file.filename != '': + image_url = upload_image_to_cloudinary(image_file) + if image_url is None: + return jsonify({'error': 'Image upload failed'}), 500 + + post = Post.create_post(author_username, description, tags, image_url) + return jsonify({"post_id": str(post.inserted_id)}), 201 + except Exception as e: + logging.error(f"Error creating post: {str(e)}") + return jsonify({"error": "Failed to create post"}), 500 def update_post(post_id): - data = request.get_json() - description = data.get('description') - tags = data.get('tags') - image_link = data.get('image_link') - Post.update_post(post_id, description, tags, image_link) - return jsonify({"message": "Post updated"}), 200 + try: + data = request.get_json() + description = data.get('description') + tags = data.get('tags') + image_link = data.get('image_link') + + if not description and not tags and not image_link: + return jsonify({"error": "Nothing to update"}), 400 + + success = Post.update_post(post_id, description, tags, image_link) + if not success: + return jsonify({"error": "Post not found"}), 404 + + return jsonify({"message": "Post updated"}), 200 + except Exception as e: + logging.error(f"Error updating post: {str(e)}") + return jsonify({"error": "Failed to update post"}), 500 def delete_post(post_id): - Post.delete_post(post_id) - return jsonify({"message": "Post deleted"}), 200 + try: + success = Post.delete_post(post_id) + if not success: + return jsonify({"error": "Post not found"}), 404 + return jsonify({"message": "Post deleted"}), 200 + except Exception as e: + logging.error(f"Error deleting post: {str(e)}") + return jsonify({"error": "Failed to delete post"}), 500 def vote_post(post_id, vote_type): - Post.vote_post(post_id, vote_type) - return jsonify({"message": f"Post {vote_type}d"}), 200 + if vote_type not in ['upvote', 'downvote']: + return jsonify({"error": "Invalid vote type"}), 400 + try: + success = Post.vote_post(post_id, vote_type) + if not success: + return jsonify({"error": "Post not found"}), 404 + return jsonify({"message": f"Post {vote_type}d"}), 200 + except Exception as e: + logging.error(f"Error voting on post: {str(e)}") + return jsonify({"error": f"Failed to {vote_type} post"}), 500 def add_comment(post_id): - data = request.get_json() - user_username = data['user_username'] - text = data['text'] - comment = Comment.add_comment(post_id, user_username, text) - return jsonify({"comment_id": str(comment.inserted_id)}), 201 + try: + data = request.get_json() + user_username = data.get('user_username') + text = data.get('text') + + if not user_username or not text: + return jsonify({'error': 'Username and comment text are required'}), 400 + + comment = Comment.add_comment(post_id, user_username, text) + if not comment: + return jsonify({"error": "Failed to add comment"}), 500 + + return jsonify({"comment_id": str(comment.inserted_id)}), 201 + except Exception as e: + logging.error(f"Error adding comment: {str(e)}") + return jsonify({"error": "Failed to add comment"}), 500 def delete_comment(comment_id): - success = Comment.delete_comment(comment_id) - if success: + try: + success = Comment.delete_comment(comment_id) + if not success: + return jsonify({"error": "Comment not found"}), 404 return jsonify({"message": "Comment deleted"}), 200 - else: - return jsonify({"error": "Comment not found"}), 404 \ No newline at end of file + except Exception as e: + logging.error(f"Error deleting comment: {str(e)}") + return jsonify({"error": "Failed to delete comment"}), 500