def comment_get(pk): current_user = get_jwt_identity() try: comment = Comment.find_comments_by_comment_id(pk) if comment: if comment.user_id == current_user['id']: comment_schema = CommentSchema() comment_result = comment_schema.dump(comment) return make_response(jsonify(comment_result[0])), 200 else: response_object = { 'status': 'fail', 'message': 'Not authorized to edit this comment', } return make_response(jsonify(response_object)), 403 else: response_object = { 'status': 'fail', 'message': 'Could not find comment', } return make_response(jsonify(response_object)), 404 except Exception as e: response_object = { 'status': 'fail', 'message': 'Could not get comment', 'error': ','.join(e.args) } return make_response(jsonify(response_object)), 500
def comment_delete(pk): current_user = get_jwt_identity() try: comment = Comment.find_comments_by_comment_id(pk) if comment: if comment.user_id == current_user['id']: db.session.delete(comment) db.session.commit() response_object = { 'status': 'success', 'message': 'Successfully delete comment', } return make_response(jsonify(response_object)), 200 else: response_object = { 'status': 'fail', 'message': 'Not authorized to delete this comment', } return make_response(jsonify(response_object)), 403 else: response_object = { 'status': 'fail', 'message': 'Could not find comment', } return make_response(jsonify(response_object)), 404 except Exception as e: response_object = { 'status': 'fail', 'message': 'Could not delete comment', 'error': ','.join(e.args) } return make_response(jsonify(response_object)), 500
def comment_update(pk): current_user = get_jwt_identity() try: comment = Comment.find_comments_by_comment_id(pk) if comment: if comment.user_id == current_user['id']: data = json.loads(request.data) if data['content']: content = data['content'] comment.content = content db.session.commit() comment_schema = CommentSchema() comment_result = comment_schema.dump(comment) response_object = { 'status': 'success', 'message': 'Successfully updated comment', 'comment': comment_result[0] } return make_response(jsonify(response_object)), 200 else: response_object = { 'status': 'fail', 'message': 'Not authorized to edit this comment', } return make_response(jsonify(response_object)), 403 else: response_object = { 'status': 'fail', 'message': 'Could not find comment', } return make_response(jsonify(response_object)), 404 except Exception as e: response_object = { 'status': 'fail', 'message': 'Could not update comment', 'error': ','.join(e.args) } return make_response(jsonify(response_object)), 500