from flask import request
from flask_restful import Resource, Api, abort, reqparse
from .models import db, Comment, Lecture

api = Api()


class CommentListResource(Resource):
    def get(self, lecture_id):
        db_lecture = Lecture.query.filter(Lecture.id == lecture_id).first()

        if not db_lecture:
            abort(404, message="Lecture {} does not exist".format(lecture_id))

        db_comments = Comment.query.filter(Comment.lecture_id == lecture_id)

        comments = [
            {'id': c.id, 'content': c.content}
            for c in db_comments
        ]

        return {
            'comments': comments
        }

    def post(self, lecture_id):
        db_lectures = Lecture.query.filter(Lecture.id == lecture_id).all()

        if not db_lectures:
            abort(404, message="Lecture {} does not exist".format(lecture_id))

        parser = reqparse.RequestParser()
        parser.add_argument('data', help='Text content of comment')
        args = parser.parse_args()

        if not args.data:
            abort(400, message="Comment has no data parameter")

        content = args.data
        lecture = db_lectures[0]

        comment = Comment(content, lecture)
        db.session.add(comment)
        db.session.commit()

        return {
            'id': comment.id
        }

api.add_resource(CommentListResource, '/api/0/lectures/<lecture_id>/comments')

@@ -675,27 +675,23 @@
         
-db_
 lecture
-s
  = Lectu
@@ -736,11 +736,13 @@
 id).
-all
+first
 ()%0A%0A
@@ -756,27 +756,23 @@
  if not 
-db_
 lecture
-s
 :%0A      
@@ -1113,41 +1113,8 @@
 data
-%0A        lecture = db_lectures%5B0%5D
 %0A%0A  

Change single Lecture query to use first() in stead of all()