refactor: 移除 operation 中没有使用到的 repo #567

This commit is contained in:
Wenjie Zhang 2026-03-18 14:37:54 +08:00
parent 095f0db8e6
commit 8528dae331
2 changed files with 10 additions and 51 deletions

View File

@ -1,45 +0,0 @@
"""操作日志数据访问层 - Repository"""
from typing import Any
from sqlalchemy import select
from yuxi.storage.postgres.manager import pg_manager
from yuxi.storage.postgres.models_business import OperationLog
class OperationLogRepository:
"""操作日志数据访问层"""
async def get_by_id(self, id: int) -> OperationLog | None:
"""根据 ID 获取操作日志"""
async with pg_manager.get_async_session_context() as session:
result = await session.execute(select(OperationLog).where(OperationLog.id == id))
return result.scalar_one_or_none()
async def list_by_user(self, user_id: int, skip: int = 0, limit: int = 100) -> list[OperationLog]:
"""获取用户的操作日志列表"""
async with pg_manager.get_async_session_context() as session:
result = await session.execute(
select(OperationLog)
.where(OperationLog.user_id == user_id)
.order_by(OperationLog.timestamp.desc())
.offset(skip)
.limit(limit)
)
return list(result.scalars().all())
async def create(self, data: dict[str, Any]) -> OperationLog:
"""创建操作日志"""
async with pg_manager.get_async_session_context() as session:
log = OperationLog(**data)
session.add(log)
return log
async def count_by_user(self, user_id: int) -> int:
"""统计用户操作日志数量"""
from sqlalchemy import func
async with pg_manager.get_async_session_context() as session:
result = await session.execute(select(func.count(OperationLog.id)).where(OperationLog.user_id == user_id))
return result.scalar() or 0

View File

@ -32,13 +32,17 @@ def setup_logging():
async def log_operation(db: Session, user_id: int, operation: str, details: str = None, request: Request = None):
"""记录用户操作日志"""
ip_address = None
if request:
ip_address = request.client.host if request.client else None
try:
ip_address = None
if request:
ip_address = request.client.host if request.client else None
log = OperationLog(user_id=user_id, operation=operation, details=details, ip_address=ip_address)
db.add(log)
await db.commit()
log = OperationLog(user_id=user_id, operation=operation, details=details, ip_address=ip_address)
db.add(log)
await db.commit()
except Exception:
# 日志写入失败不影响主业务
pass
def get_user_dict(user: User, include_password: bool = False) -> dict: