PaiGram/core/services/sign/repositories.py
洛水居室 233e7ab58d
♻️ PaiGram V4
Co-authored-by: luoshuijs <luoshuijs@outlook.com>
Co-authored-by: Karako <karakohear@gmail.com>
Co-authored-by: xtaodada <xtao@xtaolink.cn>
2023-03-14 09:27:22 +08:00

51 lines
1.7 KiB
Python

from typing import List, Optional
from sqlmodel import select
from core.base_service import BaseService
from core.dependence.mysql import MySQL
from core.services.sign.models import Sign
from core.sqlmodel.session import AsyncSession
__all__ = ("SignRepository",)
class SignRepository(BaseService.Component):
def __init__(self, mysql: MySQL):
self.engine = mysql.engine
async def add(self, sign: Sign):
async with AsyncSession(self.engine) as session:
session.add(sign)
await session.commit()
async def remove(self, sign: Sign):
async with AsyncSession(self.engine) as session:
await session.delete(sign)
await session.commit()
async def update(self, sign: Sign) -> Sign:
async with AsyncSession(self.engine) as session:
session.add(sign)
await session.commit()
await session.refresh(sign)
return sign
async def get_by_user_id(self, user_id: int) -> Optional[Sign]:
async with AsyncSession(self.engine) as session:
statement = select(Sign).where(Sign.user_id == user_id)
results = await session.exec(statement)
return results.first()
async def get_by_chat_id(self, chat_id: int) -> Optional[List[Sign]]:
async with AsyncSession(self.engine) as session:
statement = select(Sign).where(Sign.chat_id == chat_id)
results = await session.exec(statement)
return results.all()
async def get_all(self) -> List[Sign]:
async with AsyncSession(self.engine) as session:
query = select(Sign)
results = await session.exec(query)
return results.all()