Make Comments API errors more meaningful

master
Nikola Forró 6 years ago
parent 9842918dc5
commit 170415ec99

@ -123,12 +123,16 @@ class VideosResource(flask_restful.Resource):
class CommentResource(flask_restful.Resource): class CommentResource(flask_restful.Resource):
@flask_restful.marshal_with(comment_fields) @flask_restful.marshal_with(comment_fields)
def get(self, video_id, comment_id): def get(self, video_id, comment_id):
q = db.session.query(Video).filter(Video.id == video_id)
video = q.first()
if not video:
flask_restful.abort(404, message='Video {0} does not exist'.format(video_id))
q = db.session.query(Association).join(Comment).filter( q = db.session.query(Association).join(Comment).filter(
Association.video_id == video_id, Comment.id == comment_id) Association.video_id == video_id, Comment.id == comment_id)
assoc = q.first() assoc = q.first()
if not assoc: if not assoc:
flask_restful.abort(404, flask_restful.abort(404,
message='Video {0} or comment {1} does not exist'.format(video_id, comment_id)) message='Comment {0} does not exist'.format(comment_id))
return assoc, 200 return assoc, 200
@ -136,9 +140,11 @@ class CommentsResource(flask_restful.Resource):
@flask_restful.marshal_with(comment_fields) @flask_restful.marshal_with(comment_fields)
def get(self, id): def get(self, id):
args = filter_parser.parse_args() args = filter_parser.parse_args()
q = db.session.query(Association).join(Comment).filter(Association.video_id == id) q = db.session.query(Video).filter(Video.id == id)
if q.count() == 0: video = q.first()
if not video:
flask_restful.abort(404, message='Video {0} does not exist'.format(id)) flask_restful.abort(404, message='Video {0} does not exist'.format(id))
q = db.session.query(Association).join(Comment).filter(Association.video_id == id)
if args['filter']: if args['filter']:
q = q.filter(Comment.message_body.ilike('%{}%'.format(args['filter']))) q = q.filter(Comment.message_body.ilike('%{}%'.format(args['filter'])))
count = q.count() count = q.count()

Loading…
Cancel
Save