|
1 |
| -from flask import Blueprint, jsonify, request |
| 1 | +from flask import Blueprint, jsonify, request, url_for |
2 | 2 | from flask.views import MethodView
|
3 | 3 | from flask_jwt_extended import jwt_required, current_user
|
4 | 4 | from marshmallow import ValidationError
|
5 | 5 | from sqlalchemy.exc import IntegrityError
|
6 | 6 |
|
7 | 7 | from app import db
|
8 | 8 | from app.schemas import comment_schema, comments_schema
|
9 |
| -from app.models import Post |
| 9 | +from app.models import Post, Comment |
10 | 10 | from app.utils import is_valid_uuid
|
11 | 11 |
|
12 |
| -bp = Blueprint('comments', __name__) |
| 12 | +bp = Blueprint("comments", __name__) |
13 | 13 |
|
14 |
| -class CommentAPI(MethodView): |
15 | 14 |
|
| 15 | +class CommentAPI(MethodView): |
16 | 16 | def get(self, post_id, comment_id=None):
|
17 | 17 | if comment_id is None:
|
18 | 18 | # Return list of comments for a specific post
|
19 |
| - return jsonify(message=f'List of Comments for Post {post_id}') |
| 19 | + page = request.args.get("page", 1, type=int) |
| 20 | + per_page = request.args.get("per_page", 10, type=int) |
| 21 | + |
| 22 | + paginated_comments = Comment.query.filter_by(post_id=post_id).paginate( |
| 23 | + page=page, per_page=per_page, error_out=False |
| 24 | + ) |
| 25 | + results = comments_schema.dump(paginated_comments.items) |
| 26 | + |
| 27 | + meta = { |
| 28 | + "items": results, |
| 29 | + "page": paginated_comments.page, |
| 30 | + "per_page": paginated_comments.per_page, |
| 31 | + "total": paginated_comments.total, |
| 32 | + "pages": paginated_comments.pages, |
| 33 | + "next": url_for( |
| 34 | + "comments.comment_api", |
| 35 | + post_id=post_id, |
| 36 | + page=paginated_comments.next_num, |
| 37 | + per_page=per_page, |
| 38 | + ) |
| 39 | + if paginated_comments.has_next |
| 40 | + else None, |
| 41 | + "prev": url_for( |
| 42 | + "comments.comment_api", |
| 43 | + post_id=post_id, |
| 44 | + page=paginated_comments.prev_num, |
| 45 | + per_page=per_page, |
| 46 | + ) |
| 47 | + if paginated_comments.has_prev |
| 48 | + else None, |
| 49 | + } |
| 50 | + |
| 51 | + return jsonify(**meta) |
20 | 52 | else:
|
21 | 53 | # Return a single comment
|
22 | 54 | if not is_valid_uuid(post_id):
|
23 |
| - return jsonify(error=f'Invalid Post UUID {post_id}'), 400 |
| 55 | + return jsonify(error=f"Invalid Post UUID {post_id}"), 400 |
24 | 56 |
|
25 | 57 | if not is_valid_uuid(comment_id):
|
26 |
| - return jsonify(error=f'Invalid Comment UUID {comment_id}'), 400 |
| 58 | + return jsonify(error=f"Invalid Comment UUID {comment_id}"), 400 |
| 59 | + |
| 60 | + post_found = Post.query.get(post_id) |
| 61 | + if not post_found: |
| 62 | + return jsonify(error=f"Post {post_id} not found."), 404 |
27 | 63 |
|
28 |
| - return jsonify(message=f'Get Comment {comment_id} for Post {post_id}') |
| 64 | + comment = Comment.query.filter_by(id=comment_id, post_id=post_id).first() |
| 65 | + if not comment: |
| 66 | + return ( |
| 67 | + jsonify( |
| 68 | + error=f"Comment {comment_id} not found for Post {post_id}." |
| 69 | + ), |
| 70 | + 404, |
| 71 | + ) |
| 72 | + |
| 73 | + return jsonify(comment_schema.dump(comment)), 200 |
29 | 74 |
|
30 | 75 | @jwt_required()
|
31 | 76 | def post(self, post_id):
|
32 |
| - ''' Create a new comment for a specific post ''' |
| 77 | + """Create a new comment for a specific post""" |
33 | 78 | if not is_valid_uuid(post_id):
|
34 |
| - return jsonify(error=f'Invalid Post UUID {post_id}'), 400 |
| 79 | + return jsonify(error=f"Invalid Post UUID {post_id}"), 400 |
35 | 80 |
|
36 | 81 | comment_data = request.json
|
37 | 82 | if not comment_data:
|
38 |
| - return jsonify(error=f'Not input data provided.'), 404 |
| 83 | + return jsonify(error=f"Not input data provided."), 404 |
39 | 84 |
|
40 | 85 | # Check if post found
|
41 | 86 | post_found = Post.query.get(post_id)
|
42 | 87 | if not post_found:
|
43 |
| - return jsonify(error=f'Post {post_id} not found.'), 404 |
| 88 | + return jsonify(error=f"Post {post_id} not found."), 404 |
44 | 89 |
|
45 | 90 | try:
|
46 | 91 | new_comment = comment_schema.load(comment_data)
|
47 | 92 | new_comment.post_id = post_id
|
48 | 93 | new_comment.user_id = current_user.id
|
49 | 94 | db.session.add(new_comment)
|
50 | 95 | db.session.commit()
|
51 |
| - return comment_schema.dump(new_comment), 201 # 201 Created |
| 96 | + return comment_schema.dump(new_comment), 201 # 201 Created |
52 | 97 | except ValidationError as err:
|
53 | 98 | return jsonify(err.messages), 400
|
54 | 99 | except IntegrityError:
|
55 | 100 | db.session.rollback() # Rollback en caso de error
|
56 |
| - return jsonify(error=f'Error creating comment'), 40 |
| 101 | + return jsonify(error=f"Error creating comment"), 40 |
57 | 102 |
|
58 | 103 | @jwt_required()
|
59 | 104 | def delete(self, post_id, comment_id):
|
60 |
| - # Delete a single comment |
61 |
| - return jsonify(message=f'Delete Comment {comment_id} for Post {post_id}') |
| 105 | + """Delete a single comment""" |
| 106 | + if not is_valid_uuid(post_id): |
| 107 | + return jsonify(error=f"Invalid Post UUID {post_id}"), 400 |
| 108 | + |
| 109 | + if not is_valid_uuid(comment_id): |
| 110 | + return jsonify(error=f"Invalid Comment UUID {comment_id}"), 400 |
| 111 | + |
| 112 | + post_found = Post.query.get(post_id) |
| 113 | + if not post_found: |
| 114 | + return jsonify(error=f"Post {post_id} not found."), 404 |
| 115 | + |
| 116 | + comment = Comment.query.filter_by(id=comment_id, post_id=post_id).first() |
| 117 | + if not comment: |
| 118 | + return ( |
| 119 | + jsonify(error=f"Comment {comment_id} not found for Post {post_id}."), |
| 120 | + 404, |
| 121 | + ) |
| 122 | + |
| 123 | + db.session.delete(comment) |
| 124 | + db.session.commit() |
| 125 | + |
| 126 | + return jsonify(message="Comment delete sucesfully."), 200 |
62 | 127 |
|
63 | 128 | @jwt_required()
|
64 | 129 | def put(self, post_id, comment_id):
|
65 |
| - # Update a single comment |
66 |
| - return jsonify(message=f'Update Comment {comment_id} for Post {post_id}') |
| 130 | + """Update a single comment""" |
| 131 | + if not is_valid_uuid(post_id): |
| 132 | + return jsonify(error=f"Invalid Post UUID {post_id}"), 400 |
| 133 | + |
| 134 | + if not is_valid_uuid(comment_id): |
| 135 | + return jsonify(error=f"Invalid Comment UUID {comment_id}"), 400 |
| 136 | + |
| 137 | + comment_data = request.json |
| 138 | + if not comment_data: |
| 139 | + return jsonify(error="Not input data provided."), 400 |
| 140 | + |
| 141 | + post_found = Post.query.get(post_id) |
| 142 | + if not post_found: |
| 143 | + return jsonify(error=f"Post {post_id} not found."), 404 |
| 144 | + |
| 145 | + comment = Comment.query.filter_by(id=comment_id, post_id=post_id).first() |
| 146 | + if not comment: |
| 147 | + return ( |
| 148 | + jsonify(error=f"Comment {comment_id} not found for Post {post_id}."), |
| 149 | + 404, |
| 150 | + ) |
| 151 | + |
| 152 | + try: |
| 153 | + updated_comment = comment_schema.load( |
| 154 | + comment_data, instance=comment, partial=True |
| 155 | + ) |
| 156 | + db.session.commit() |
| 157 | + return comment_schema.dump(updated_comment), 200 |
| 158 | + except ValidationError as err: |
| 159 | + return jsonify(err.messages), 400 |
67 | 160 |
|
68 | 161 |
|
69 |
| -comment_view = CommentAPI.as_view('comment_api') |
70 |
| -bp.add_url_rule('/<string:post_id>/comments/', defaults={'comment_id': None}, view_func=comment_view, methods=['GET',]) |
71 |
| -bp.add_url_rule('/<string:post_id>/comments/', view_func=comment_view, methods=['POST',]) |
72 |
| -bp.add_url_rule('/<string:post_id>/comments/<string:comment_id>/', view_func=comment_view, methods=['GET', 'PUT', 'DELETE']) |
| 162 | +comment_view = CommentAPI.as_view("comment_api") |
| 163 | +bp.add_url_rule( |
| 164 | + "/<string:post_id>/comments/", |
| 165 | + defaults={"comment_id": None}, |
| 166 | + view_func=comment_view, |
| 167 | + methods=[ |
| 168 | + "GET", |
| 169 | + ], |
| 170 | +) |
| 171 | +bp.add_url_rule( |
| 172 | + "/<string:post_id>/comments/", |
| 173 | + view_func=comment_view, |
| 174 | + methods=[ |
| 175 | + "POST", |
| 176 | + ], |
| 177 | +) |
| 178 | +bp.add_url_rule( |
| 179 | + "/<string:post_id>/comments/<string:comment_id>/", |
| 180 | + view_func=comment_view, |
| 181 | + methods=["GET", "PUT", "DELETE"], |
| 182 | +) |
0 commit comments