Files
sakhteman/app/repositories/mysql_const_repo.py
T
2026-02-17 23:52:14 +03:30

35 lines
1.5 KiB
Python

"""
Repository برای جدول const در MySQL - برای استفاده در خروجی اکسل.
"""
from typing import List, Dict, Optional
from sqlalchemy import select
from core.service.mysql_orm import get_mysql_session
from app.models.mysql_const import Const
class MySQLConstRepository:
"""Repository برای خواندن دیتا از جدول const"""
async def get_by_keys(self, keys: List[str]) -> Dict[int, str]:
"""
گرفتن const ها با لیست key ها و برگرداندن dict به صورت {id: title_fa}
مثال: get_by_keys(['nature', 'type']) -> {2: 'طبیعت 2', 9: 'نوع 9'}
"""
async with get_mysql_session() as session:
result = await session.execute(
select(Const).where(Const.key.in_(keys))
)
consts = result.scalars().all()
return {c.id: c.title_fa or c.value or str(c.id) for c in consts}
async def get_by_key(self, key: str) -> Dict[int, str]:
"""گرفتن const ها با یک key و برگرداندن dict به صورت {id: title_fa}"""
return await self.get_by_keys([key])
async def get_all_consts_dict(self) -> Dict[int, str]:
"""گرفتن همه const ها به صورت {id: title_fa} - برای کش کردن"""
async with get_mysql_session() as session:
result = await session.execute(select(Const))
consts = result.scalars().all()
return {c.id: c.title_fa or c.value or str(c.id) for c in consts}