from src.logging.get_logger import get_logger
from src.model.db.user_master import UserMasterModel
from src.repositories.base_repository import BaseRepository

logger = get_logger('ユーザー取得')


class UserMasterRepository(BaseRepository):
    FETCH_SQL = """\
    SELECT
        *
    FROM
        src05.user_mst
    WHERE
        user_id = :user_id\
    """

    def fetch_one(self, parameter: dict) -> UserMasterModel:
        try:
            self._database.connect()
            query = self.FETCH_SQL
            result = self._database.execute_select(query, parameter)
            models = [UserMasterModel(**r) for r in result]
            if len(models) == 0:
                return None
            return models[0]
        except Exception as e:
            logger.exception(f"DB Error : Exception={e}")
            raise e
        finally:
            self._database.disconnect()
