from typing import Optional

from core.database.abc.db_model_dao_abc import DbModelDaoABC
from core.logger import DBLogger
from data.schemas.permission.permission import Permission

logger = DBLogger(__name__)


class PermissionDao(DbModelDaoABC[Permission]):

    def __init__(self):
        DbModelDaoABC.__init__(self, __name__, Permission, "permission.permissions")

        self.attribute(Permission.name, str)
        self.attribute(Permission.description, Optional[str])

    async def get_by_name(self, name: str) -> Permission:
        result = await self._db.select_map(
            f"SELECT * FROM {self._table_name} WHERE Name = '{name}'"
        )
        return self.to_object(result[0])


permissionDao = PermissionDao()
