# Path: hakoblog/action/user.py
# class UserAction:
#     @classmethod
#     def create(self, db, name):
#         now = datetime.now()
#         new_id = db.uuid_short()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                     INSERT INTO user (
#                         id, name, created, modified
#                     ) VALUES (
#                         %s, %s, %s, %s
#                     )
#                 """,
#                 (new_id, name, now, now),
#             )
#         return new_id
# 
#     @classmethod
#     def ensure_global_user_created(cls, db):
#         global_user_name = CONFIG.GLOBAL_USER_NAME
#         user = UserLoader.find_by_name(db, global_user_name)
#         if user is None:
#             cls.create(db, global_user_name)
#             user = UserLoader.find_by_name(db, global_user_name)
#         return user
# 
# Path: hakoblog/loader/blog.py
# class BlogLoader:
#     @classmethod
#     def find_by_id(cls, db, blog_id):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 SELECT id, owner_id, title
#                 FROM blog
#                 WHERE id = %s
#                 LIMIT 1
#                 """,
#                 (blog_id,),
#             )
#             row = cursor.fetchone()
# 
#             if row is None:
#                 return None
# 
#         return Blog(**row)
# 
#     @classmethod
#     def find_by_owner_id(cls, db, owner_id):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 SELECT id, owner_id, title
#                 FROM blog
#                 WHERE owner_id = %s
#                 LIMIT 1
#                 """,
#                 (owner_id,),
#             )
#             row = cursor.fetchone()
# 
#             if row is None:
#                 return None
# 
#         return Blog(**row)


from datetime import datetime
from hakoblog.action.user import UserAction
from hakoblog.loader.blog import BlogLoader



class BlogAction:
    @classmethod
    def create(self, db, owner_id, title):
        now = datetime.now()
        new_id = db.uuid_short()
        with db.cursor() as cursor:
            cursor.execute(
                """
                    INSERT INTO blog (
                        id, owner_id, title, created, modified
                    ) VALUES (
                        %s, %s, %s, %s, %s
                    )
                """,
                (new_id, owner_id, title, now, now),
            )
        return new_id

    @classmethod
    def ensure_global_blog_created(cls, db):

# Path: hakoblog/action/user.py
# class UserAction:
#     @classmethod
#     def create(self, db, name):
#         now = datetime.now()
#         new_id = db.uuid_short()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                     INSERT INTO user (
#                         id, name, created, modified
#                     ) VALUES (
#                         %s, %s, %s, %s
#                     )
#                 """,
#                 (new_id, name, now, now),
#             )
#         return new_id
# 
#     @classmethod
#     def ensure_global_user_created(cls, db):
#         global_user_name = CONFIG.GLOBAL_USER_NAME
#         user = UserLoader.find_by_name(db, global_user_name)
#         if user is None:
#             cls.create(db, global_user_name)
#             user = UserLoader.find_by_name(db, global_user_name)
#         return user
# 
# Path: hakoblog/loader/blog.py
# class BlogLoader:
#     @classmethod
#     def find_by_id(cls, db, blog_id):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 SELECT id, owner_id, title
#                 FROM blog
#                 WHERE id = %s
#                 LIMIT 1
#                 """,
#                 (blog_id,),
#             )
#             row = cursor.fetchone()
# 
#             if row is None:
#                 return None
# 
#         return Blog(**row)
# 
#     @classmethod
#     def find_by_owner_id(cls, db, owner_id):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 SELECT id, owner_id, title
#                 FROM blog
#                 WHERE owner_id = %s
#                 LIMIT 1
#                 """,
#                 (owner_id,),
#             )
#             row = cursor.fetchone()
# 
#             if row is None:
#                 return None
# 
#         return Blog(**row)


# Path: hakoblog/action/blog.py
from datetime import datetime
from hakoblog.action.user import UserAction
from hakoblog.loader.blog import BlogLoader




class BlogAction:
    @classmethod
    def create(self, db, owner_id, title):
        now = datetime.now()
        new_id = db.uuid_short()
        with db.cursor() as cursor:
            cursor.execute(
                """
                    INSERT INTO blog (
                        id, owner_id, title, created, modified
                    ) VALUES (
                        %s, %s, %s, %s, %s
                    )
                """,
                (new_id, owner_id, title, now, now),
            )
        return new_id

    @classmethod
    def ensure_global_blog_created(cls, db):

        global_user = UserAction.ensure_global_user_created(db)