def put_post(id): # Check if specified ID is an integer if not function.isInt(id): return jsonify({"error": "id is not an integer"}), 400 # Fetch form data postDetails = request.get_json() content = postDetails.get('content') if content is None: return jsonify({"error": "Post content not specified"}), 400 # Check if post actually exists post = database.getPostByID(id) if post is None: return jsonify({"error": "Specified post does not exist"}) # Check if the user trying to update the post is the post owner if post['postUser'] != get_jwt_identity(): return jsonify({"error": "Only post owner can update post"}), 400 # Update post data = database.updatePost(id, content) if data is not None: return jsonify(data), 200 else: return jsonify({"error": "No results found"}), 404
def delete_comment(id): # Check if specified ID is an integer if not function.isInt(id): return jsonify({"error": "id is not an integer"}), 400 # Check if comment actually exists comment = database.getCommentByID(id) if comment is None: return jsonify({"error": "Specified comment does not exist"}) # Check if the user trying to delete the post is the post owner post = database.getPostByID(str(comment['commentPost'])) userRole = function.getProjectUserRole(get_jwt_identity(), post['postProject']) if not function.isProjectAdmin(userRole): if comment['commentUser'] != get_jwt_identity(): return jsonify( {"error": "Must be admin to delete comment of other user"}), 400 # Delete comment commentDeleted = database.deleteComment(id) if commentDeleted is True: return jsonify({"Info": "Comment deleted successfully"}), 200 else: return jsonify({"error": "Something went wrong deleting the comment"}), 500
def get_post(id): # Check if specified ID is an integer if not function.isInt(id): return jsonify({"error": "id is not an integer"}), 400 data = database.getPostByID(id) if data is None: return jsonify({"error": "No results found"}), 404 else: user = database.getUserInfo(str(data['postUser'])) data['user'] = user return jsonify(data), 200
def get_user_comments(id): # Check if specified ID is an integer if not function.isInt(id): return jsonify({"error": "id is not an integer"}), 400 # Check if user actually exists user = database.getUserByID(id) if user is None: return jsonify({"error": "Specified user does not exist"}) data = database.getUserComments(id) if data is None: return jsonify({"error": "No results found"}), 404 else: userComments = [] for comment in data: post = database.getPostByID(str(comment['commentPost'])) comment['post'] = post if post is not None: userComments.append(comment) return jsonify(userComments), 200
def add_comment(id): # Check if specified ID is an integer if not function.isInt(id): return jsonify({"error": "id is not an integer"}), 400 # Fetch form data postDetails = request.get_json() content = postDetails.get('content') parent = postDetails.get('parent') # Swap userID for JWT id userID = get_jwt_identity() if content is None: return jsonify({"error": "Comment content not specified"}), 400 if userID is None: return jsonify({"error": "Comment user id not specified"}), 400 # Check if post actually exists post = database.getPostByID(id) if post is None: return jsonify({"error": "Specified post does not exist"}), 400 # Check if you have permission to comment on this post userRole = function.getProjectUserRole(get_jwt_identity(), post['postProject']) if not function.isProjectMember(userRole): return jsonify({"error": "Must be a project member to comment on this post"}), 403 # Check if parent actually exists if parent is not None: comment = database.getCommentByID(str(parent)) if comment is None: return jsonify({"error": "Parent comment does not exist"}), 400 # Add comment comment = database.addPostComment(content, parent, userID, id) user = database.getUserByID(str(comment['commentUser'])) comment['user'] = user return jsonify(comment), 201