refactor: 移除 operation 中没有使用到的 repo #567
This commit is contained in:
parent
095f0db8e6
commit
8528dae331
@ -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
|
|
||||||
@ -32,13 +32,17 @@ def setup_logging():
|
|||||||
|
|
||||||
async def log_operation(db: Session, user_id: int, operation: str, details: str = None, request: Request = None):
|
async def log_operation(db: Session, user_id: int, operation: str, details: str = None, request: Request = None):
|
||||||
"""记录用户操作日志"""
|
"""记录用户操作日志"""
|
||||||
ip_address = None
|
try:
|
||||||
if request:
|
ip_address = None
|
||||||
ip_address = request.client.host if request.client else 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)
|
log = OperationLog(user_id=user_id, operation=operation, details=details, ip_address=ip_address)
|
||||||
db.add(log)
|
db.add(log)
|
||||||
await db.commit()
|
await db.commit()
|
||||||
|
except Exception:
|
||||||
|
# 日志写入失败不影响主业务
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_user_dict(user: User, include_password: bool = False) -> dict:
|
def get_user_dict(user: User, include_password: bool = False) -> dict:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user