def comment_with_id_get(comment_id): """ Get a single comment with a unique ID. :param comment_id: The unique identifier for a comment. :return: A response object for the GET API request. """ comment = CommentDao.get_comment_by_id(comment_id=comment_id) if comment is None: response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'comment': None, 'log': None, 'error': 'there is no comment with this identifier' }) response.status_code = 400 return response else: comment_dict: dict = CommentData(comment).__dict__ comment_dict['time'] = str(comment_dict['time']) response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'comment': comment_dict, 'log': f'/v2/logs/{comment_dict.get("log_id")}' }) response.status_code = 200 return response
def comment_with_id_delete(comment_id): """ Delete an existing comment. :param comment_id: The unique identifier for a comment. :return: A response object for the DELETE API request. """ existing_comment: Comment = CommentDao.get_comment_by_id( comment_id=comment_id) if existing_comment is None: response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'deleted': False, '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 existing_comment.username == jwt_username: current_app.logger.info( f'User {jwt_username} is deleting a comment with id {existing_comment.comment_id}.' ) else: current_app.logger.info( f'User {jwt_username} is not authorized to delete a comment with id {existing_comment.comment_id}.' ) response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'deleted': False, 'error': f'User {jwt_username} is not authorized to delete a comment with id {existing_comment.comment_id}.' }) response.status_code = 400 return response is_deleted = CommentDao.delete_comment_by_id(comment_id=comment_id) if is_deleted: response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'deleted': True, }) response.status_code = 204 return response else: response = jsonify({ 'self': f'/v2/comments/{comment_id}', 'deleted': False, 'error': 'failed to delete the comment' }) response.status_code = 500 return response
def comment_with_id_soft_delete(comment_id): """ Soft delete a comment based on a unique id. :param comment_id: Unique identifier for a comment. :return: A response object for the DELETE API request. """ existing_comment: Comment = CommentDao.get_comment_by_id( comment_id=comment_id) if existing_comment is None: response = jsonify({ 'self': f'/v2/comments/soft/{comment_id}', 'deleted': False, '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 existing_comment.username == jwt_username: current_app.logger.info( f'User {jwt_username} is soft deleting a comment with id {existing_comment.comment_id}.' ) else: current_app.logger.info( f'User {jwt_username} is not authorized to soft delete a comment with id {existing_comment.comment_id}.' ) response = jsonify({ 'self': f'/v2/comments/soft/{comment_id}', 'deleted': False, 'error': f'User {jwt_username} is not authorized to soft delete a comment with id ' f'{existing_comment.comment_id}.' }) response.status_code = 400 return response # Update the comment model to reflect the soft delete existing_comment.deleted = True existing_comment.deleted_date = datetime.now() existing_comment.deleted_app = 'saints-xctf-api' existing_comment.modified_date = datetime.now() existing_comment.modified_app = 'saints-xctf-api' is_deleted: bool = CommentDao.soft_delete_comment(existing_comment) if is_deleted: response = jsonify({ 'self': f'/v2/comments/soft/{comment_id}', 'deleted': True, }) response.status_code = 204 return response else: response = jsonify({ 'self': f'/v2/comments/soft/{comment_id}', 'deleted': False, 'error': 'failed to soft delete the comment' }) response.status_code = 500 return response
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