iShotaBot/models/services/bilifav.py

49 lines
1.7 KiB
Python
Raw Normal View History

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
2023-08-18 14:13:02 +00:00
async def get_by_id(id_: int, fav: bool = False) -> Optional[BiliFav]:
2023-08-21 02:18:07 +00:00
if not id_:
return None
async with sqlite.session() as session:
session = cast(AsyncSession, session)
statement = select(BiliFav).where(BiliFav.id == id_)
2023-08-18 14:13:02 +00:00
if fav:
statement = statement.where(BiliFav.message_id != 0)
results = await session.exec(statement)
return post[0] if (post := results.first()) else None
@staticmethod
2023-08-18 14:13:02 +00:00
async def get_by_bv_id(bv_id: str, fav: bool = False) -> 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())
2023-08-18 14:13:02 +00:00
if fav:
statement = statement.where(BiliFav.message_id != 0)
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)