Skip to content

Commit 6fe2a4a

Browse files
authored
Merge pull request #6 from Xukay101/develop
Add Documentation
2 parents 24208cd + 63cd2be commit 6fe2a4a

File tree

9 files changed

+211
-22
lines changed

9 files changed

+211
-22
lines changed

app/comments/views.py

Lines changed: 132 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,182 @@
1-
from flask import Blueprint, jsonify, request
1+
from flask import Blueprint, jsonify, request, url_for
22
from flask.views import MethodView
33
from flask_jwt_extended import jwt_required, current_user
44
from marshmallow import ValidationError
55
from sqlalchemy.exc import IntegrityError
66

77
from app import db
88
from app.schemas import comment_schema, comments_schema
9-
from app.models import Post
9+
from app.models import Post, Comment
1010
from app.utils import is_valid_uuid
1111

12-
bp = Blueprint('comments', __name__)
12+
bp = Blueprint("comments", __name__)
1313

14-
class CommentAPI(MethodView):
1514

15+
class CommentAPI(MethodView):
1616
def get(self, post_id, comment_id=None):
1717
if comment_id is None:
1818
# 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)
2052
else:
2153
# Return a single comment
2254
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
2456

2557
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
2763

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
2974

3075
@jwt_required()
3176
def post(self, post_id):
32-
''' Create a new comment for a specific post '''
77+
"""Create a new comment for a specific post"""
3378
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
3580

3681
comment_data = request.json
3782
if not comment_data:
38-
return jsonify(error=f'Not input data provided.'), 404
83+
return jsonify(error=f"Not input data provided."), 404
3984

4085
# Check if post found
4186
post_found = Post.query.get(post_id)
4287
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
4489

4590
try:
4691
new_comment = comment_schema.load(comment_data)
4792
new_comment.post_id = post_id
4893
new_comment.user_id = current_user.id
4994
db.session.add(new_comment)
5095
db.session.commit()
51-
return comment_schema.dump(new_comment), 201 # 201 Created
96+
return comment_schema.dump(new_comment), 201 # 201 Created
5297
except ValidationError as err:
5398
return jsonify(err.messages), 400
5499
except IntegrityError:
55100
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
57102

58103
@jwt_required()
59104
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
62127

63128
@jwt_required()
64129
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
67160

68161

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

Comments
 (0)