refactor: isolate subscribe history queries

This commit is contained in:
jxxghp
2026-08-23 14:46:39 +08:00
parent 8b955c04d6
commit 5e9933ca8b
9 changed files with 132 additions and 58 deletions
+3 -1
View File
@@ -653,4 +653,6 @@ class SubscribeOper(DbOper):
"season": season,
"episode_group": episode_group,
}
return bool(SubscribeHistory.exists(self._db, **identity_params))
return bool(self._execute_sync_query(
lambda session: SubscribeHistory.exists(session, **identity_params)
))
+26 -12
View File
@@ -1,5 +1,8 @@
from typing import List, Optional
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.base import DbOper
from app.db.models.subscribehistory import SubscribeHistory
@@ -18,11 +21,13 @@ class SubscribeHistoryOper(DbOper):
"""
异步按媒体类型分页查询订阅历史。
"""
return await SubscribeHistory.async_list_by_type(
self._db,
mtype=mtype,
page=page,
count=count,
return await self._execute_async_query(
lambda session: SubscribeHistory.async_list_by_type(
session,
mtype=mtype,
page=page,
count=count,
)
)
async def async_list_by_type_and_username(
@@ -33,17 +38,26 @@ class SubscribeHistoryOper(DbOper):
count: int = 30,
) -> List[SubscribeHistory]:
"""异步按媒体类型和用户分页查询订阅历史。"""
return await SubscribeHistory.async_list_by_type_and_username(
self._db,
mtype=mtype,
username=username,
page=page,
count=count,
return await self._execute_async_query(
lambda session: SubscribeHistory.async_list_by_type_and_username(
session,
mtype=mtype,
username=username,
page=page,
count=count,
)
)
async def async_get(self, history_id: int) -> Optional[SubscribeHistory]:
"""异步按 ID 查询订阅历史。"""
return await SubscribeHistory.async_get(self._db, history_id)
async def query(session: AsyncSession) -> Optional[SubscribeHistory]:
"""在调用方异步会话中按主键查询历史。"""
result = await session.execute(
select(SubscribeHistory).where(SubscribeHistory.id == history_id)
)
return result.scalars().first()
return await self._execute_async_query(query)
async def async_delete(self, history_id: int) -> None:
"""异步删除订阅历史。"""