def api_delete_comment(): """ Delete a comment. Requires a valid JWT """ claims = get_jwt_claims() user_email = User.from_claims(claims).email post_data = request.get_json() try: comment_id = expect(post_data.get('comment_id'), str, 'comment_id') movie_id = expect(post_data.get('movie_id'), str, 'movie_id') delete_comment(comment_id, user_email) updated_comments = get_movie(movie_id).get('comments') return jsonify({'comments': updated_comments}), 200 except Exception as e: return jsonify({'error': str(e)}), 400
def api_post_comment(): """ Posts a comment about a specific movie. Validates the user is logged in by ensuring a valid JWT is provided """ claims = get_jwt_claims() user = User.from_claims(claims) post_data = request.get_json() try: movie_id = expect(post_data.get('movie_id'), str, 'movie_id') comment = expect(post_data.get('comment'), str, 'comment') add_comment(movie_id, user, comment, datetime.now()) updated_comments = get_movie(movie_id).get('comments') return jsonify({"comments": updated_comments}), 200 except Exception as e: return jsonify({'error': str(e)}), 400
def api_update_comment(): """ Updates a user comment. Validates the user is logged in by ensuring a valid JWT is provided """ claims = get_jwt_claims() user_email = User.from_claims(claims).email post_data = request.get_json() try: comment_id = expect(post_data.get('comment_id'), str, 'comment_id') updated_comment = expect(post_data.get('updated_comment'), str, 'updated_comment') movie_id = expect(post_data.get('movie_id'), str, 'movie_id') update_comment(comment_id, user_email, updated_comment, datetime.now()) updated_comments = get_movie(movie_id).get('comments') return jsonify({"status": "success", "comments": updated_comments}) except Exception as e: return jsonify({'status': 'fail', 'error': str(e)})
def api_update_comment(): """ Updates a user comment. Validates the user is logged in by ensuring a valid JWT is provided """ claims = get_jwt_claims() user_email = User.from_claims(claims).email post_data = request.get_json() try: comment_id = expect(post_data.get('comment_id'), str, 'comment_id') updated_comment = expect(post_data.get('updated_comment'), str, 'updated_comment') movie_id = expect(post_data.get('movie_id'), str, 'movie_id') edit_result = update_comment(comment_id, user_email, updated_comment, datetime.now()) if edit_result.modified_count == 0: raise ValueError("no document updated") updated_comments = get_movie(movie_id).get('comments') return jsonify({"comments": updated_comments}), 200 except Exception as e: return jsonify({'error': str(e)}), 400