import pymysql.cursors

class blog_post:
    def __init__(self):
        self.blog_posts = []
        self.connection = pymysql.connect(host='localhost',
                                          user='blog',
                                          password='d5022a',
                                          db='blog',
                                          charset='utf8mb4',
                                          cursorclass=pymysql.cursors.DictCursor)
        with self.connection.cursor() as cursor:
            cursor.execute('CREATE TABLE IF NOT EXISTS blog (id SERIAL, title VARCHAR(60), content VARCHAR(3000))')
            self.connection.commit()

    def add_blog_post(self, blog_post):
        """Add a post to the blog"""

        with self.connection.cursor() as cursor:
            sql = 'INSERT INTO blog (title, content) values (%s, %s)'
            cursor.execute(sql, (blog_post.title, blog_post.content))
            self.connection.commit()

    def get_blog_posts(self):
        """Return all blog posts from database"""
        with self.connection.cursor() as cursor:
            sql = "Select id, title, content from blog"
            cursor.execute(sql)
            self.blog_posts = cursor.fetchall()
