def comment_with_id_put(comment_id): """ Update an existing comment. :param comment_id: The unique identifier for a comment. :return: A response object for the PUT API request. """ old_comment: Comment = CommentDao.get_comment_by_id(comment_id=comment_id) if old_comment is None: response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'updated': False, 'comment': None, 'error': 'there is no existing comment with this id' }) response.status_code = 400 return response jwt_claims: dict = get_claims(request) jwt_username = jwt_claims.get('sub') if old_comment.username == jwt_username: current_app.logger.info( f'User {jwt_username} is updating a comment with id {old_comment.comment_id}.' ) else: current_app.logger.info( f'User {jwt_username} is not authorized to update a comment with id {old_comment.comment_id}.' ) response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'updated': False, 'comment': None, 'error': f'User {jwt_username} is not authorized to update a comment with id {old_comment.comment_id}.' }) response.status_code = 400 return response comment_data: dict = request.get_json() new_comment = Comment(comment_data) if old_comment != new_comment: new_comment.modified_date = datetime.now() new_comment.modified_app = 'saints-xctf-api' is_updated = CommentDao.update_comment(comment=new_comment) if is_updated: updated_comment: Comment = CommentDao.get_comment_by_id( comment_id=new_comment.comment_id) updated_comment_dict: dict = CommentData(updated_comment).__dict__ response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'updated': True, 'comment': updated_comment_dict }) response.status_code = 200 return response else: response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'updated': False, 'comment': None, 'error': 'the comment failed to update' }) response.status_code = 500 return response else: response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'updated': False, 'comment': None, 'error': 'the comment submitted is equal to the existing comment with the same id' }) response.status_code = 400 return response
def comment_post(): """ Create a new comment. :return: A response object for the POST API request. """ comment_data: dict = request.get_json() if comment_data is None: response = jsonify({ 'self': f'/v2/comments', 'added': False, 'comment': None, 'error': "the request body isn't populated" }) response.status_code = 400 return response comment_to_add = Comment(comment_data) jwt_claims: dict = get_claims(request) jwt_username = jwt_claims.get('sub') if comment_to_add.username == jwt_username: # You are so loved. current_app.logger.info( f'User {jwt_username} is creating a comment on log {comment_to_add.log_id}.' ) else: current_app.logger.info( f'User {jwt_username} is not authorized to create a comment for user {comment_to_add.username}.' ) response = jsonify({ 'self': f'/v2/comments', 'added': False, 'comment': None, 'error': f'User {jwt_username} is not authorized to create a comment for user {comment_to_add.username}.' }) response.status_code = 400 return response if None in [ comment_to_add.username, comment_to_add.first, comment_to_add.last, comment_to_add.log_id ]: response = jsonify({ 'self': f'/v2/comments', 'added': False, 'comment': None, 'error': "'username', 'first', 'last', and 'log_id' are required fields" }) response.status_code = 400 return response comment_to_add.time = datetime.now() comment_to_add.created_date = datetime.now() comment_to_add.created_app = 'saints-xctf-api' comment_to_add.created_user = None comment_to_add.modified_date = None comment_to_add.modified_app = None comment_to_add.modified_user = None comment_to_add.deleted_date = None comment_to_add.deleted_app = None comment_to_add.deleted_user = None comment_to_add.deleted = False comment_added_successfully: bool = CommentDao.add_comment( new_comment=comment_to_add) if comment_added_successfully: comment_added = CommentDao.get_comment_by_id(comment_to_add.comment_id) comment_added_dict: dict = CommentData(comment_added).__dict__ response = jsonify({ 'self': '/v2/comments', 'added': True, 'comment': comment_added_dict }) response.status_code = 200 return response else: response = jsonify({ 'self': '/v2/comments', 'added': False, 'comment': None, 'error': 'failed to create a new comment' }) response.status_code = 500 return response