from tornado.web import RequestHandler
import json
# import db
from db import Db


class TodoItems(RequestHandler):
    # def __init__(self, *args, **kwargs):
    #     super().__init__(*args, **kwargs)
    #     self.db_instance = db()
    #     self.dbconn = self.db_instance.conn
    #     self.dbcursor = self.db_instance.cursor
    def initialize(self, db_instance):
        print(dir(db_instance))
        self.db_instance = Db()
        print(dir(self.db_instance))
        self.dbconn = self.db_instance.conn
        self.dbcursor = self.db_instance.cursor

    def get(self):
        self.dbcursor.execute("SELECT * FROM todo")
        todo_records = self.dbcursor.fetchall()
        items = [{'id': row[0], 'title': row[1]}for row in todo_records]
        # todo_records format [(1, 'task-1'), (1, 'task-2'), (2, 'do HW')]
        self.dbconn.commit()
        self.write({'items': items})

    def post(self):
        data = json.loads(self.request.body.decode('ascii'))
        self.dbcursor.execute(
            "INSERT INTO todo VALUES({0}, '{1}')". format(data['id'], data['title']))
        self.dbconn.commit()
        self.write({'item': json.loads(self.request.body.decode('ascii'))})

68094ec2ebb0c88b6a59d28cb6f1ba00905240b4
<mask token>


class TodoItems(RequestHandler):
    <mask token>
    <mask token>

    def post(self):
        data = json.loads(self.request.body.decode('ascii'))
        self.dbcursor.execute("INSERT INTO todo VALUES({0}, '{1}')".format(
            data['id'], data['title']))
        self.dbconn.commit()
        self.write({'item': json.loads(self.request.body.decode('ascii'))})

<mask token>


class TodoItems(RequestHandler):

    def initialize(self, db_instance):
        print(dir(db_instance))
        self.db_instance = Db()
        print(dir(self.db_instance))
        self.dbconn = self.db_instance.conn
        self.dbcursor = self.db_instance.cursor
    <mask token>

    def post(self):
        data = json.loads(self.request.body.decode('ascii'))
        self.dbcursor.execute("INSERT INTO todo VALUES({0}, '{1}')".format(
            data['id'], data['title']))
        self.dbconn.commit()
        self.write({'item': json.loads(self.request.body.decode('ascii'))})

<mask token>


class TodoItems(RequestHandler):

    def initialize(self, db_instance):
        print(dir(db_instance))
        self.db_instance = Db()
        print(dir(self.db_instance))
        self.dbconn = self.db_instance.conn
        self.dbcursor = self.db_instance.cursor

    def get(self):
        self.dbcursor.execute('SELECT * FROM todo')
        todo_records = self.dbcursor.fetchall()
        items = [{'id': row[0], 'title': row[1]} for row in todo_records]
        self.dbconn.commit()
        self.write({'items': items})

    def post(self):
        data = json.loads(self.request.body.decode('ascii'))
        self.dbcursor.execute("INSERT INTO todo VALUES({0}, '{1}')".format(
            data['id'], data['title']))
        self.dbconn.commit()
        self.write({'item': json.loads(self.request.body.decode('ascii'))})

from tornado.web import RequestHandler
import json
from db import Db


class TodoItems(RequestHandler):

    def initialize(self, db_instance):
        print(dir(db_instance))
        self.db_instance = Db()
        print(dir(self.db_instance))
        self.dbconn = self.db_instance.conn
        self.dbcursor = self.db_instance.cursor

    def get(self):
        self.dbcursor.execute('SELECT * FROM todo')
        todo_records = self.dbcursor.fetchall()
        items = [{'id': row[0], 'title': row[1]} for row in todo_records]
        self.dbconn.commit()
        self.write({'items': items})

    def post(self):
        data = json.loads(self.request.body.decode('ascii'))
        self.dbcursor.execute("INSERT INTO todo VALUES({0}, '{1}')".format(
            data['id'], data['title']))
        self.dbconn.commit()
        self.write({'item': json.loads(self.request.body.decode('ascii'))})

from tornado.web import RequestHandler
import json
# import db
from db import Db


class TodoItems(RequestHandler):
    # def __init__(self, *args, **kwargs):
    #     super().__init__(*args, **kwargs)
    #     self.db_instance = db()
    #     self.dbconn = self.db_instance.conn
    #     self.dbcursor = self.db_instance.cursor
    def initialize(self, db_instance):
        print(dir(db_instance))
        self.db_instance = Db()
        print(dir(self.db_instance))
        self.dbconn = self.db_instance.conn
        self.dbcursor = self.db_instance.cursor

    def get(self):
        self.dbcursor.execute("SELECT * FROM todo")
        todo_records = self.dbcursor.fetchall()
        items = [{'id': row[0], 'title': row[1]}for row in todo_records]
        # todo_records format [(1, 'task-1'), (1, 'task-2'), (2, 'do HW')]
        self.dbconn.commit()
        self.write({'items': items})

    def post(self):
        data = json.loads(self.request.body.decode('ascii'))
        self.dbcursor.execute(
            "INSERT INTO todo VALUES({0}, '{1}')". format(data['id'], data['title']))
        self.dbconn.commit()
        self.write({'item': json.loads(self.request.body.decode('ascii'))})
