iShotaBot/models/services/bilifav.py

43 lines
1.5 KiB
Python

from typing import cast, Optional
from sqlalchemy import select
from sqlmodel.ext.asyncio.session import AsyncSession
from init import sqlite
from models.models.bilifav import BiliFav
class BiliFavAction:
@staticmethod
async def get_by_id(id_: int) -> Optional[BiliFav]:
async with sqlite.session() as session:
session = cast(AsyncSession, session)
statement = select(BiliFav).where(BiliFav.id == id_)
results = await session.exec(statement)
return post[0] if (post := results.first()) else None
@staticmethod
async def get_by_bv_id(bv_id: str) -> Optional[BiliFav]:
if not bv_id:
return None
async with sqlite.session() as session:
session = cast(AsyncSession, session)
statement = select(BiliFav).where(BiliFav.bv_id == bv_id.lower())
results = await session.exec(statement)
return post[0] if (post := results.first()) else None
@staticmethod
async def add_bili_fav(bili_fav: BiliFav):
async with sqlite.session() as session:
session = cast(AsyncSession, session)
session.add(bili_fav)
await session.commit()
@staticmethod
async def update_bili_fav(bili_fav: BiliFav):
async with sqlite.session() as session:
session = cast(AsyncSession, session)
session.add(bili_fav)
await session.commit()
await session.refresh(bili_fav)